Complete Python Lab Lecture Notes
Lecture 1: Introduction to Python
Objectives:
- Understand what Python is
- Learn about Python features and applications
- Set up Python environment
- Write and execute a simple Python program
What is Python?
Python is a high-level, interpreted, object-oriented programming language with dynamic semantics.
Features:
- Easy to learn and use
- Interpreted Language
- Dynamically typed
- Object-Oriented
- Large Standard Library
- Portable and Open Source
Applications:
- Web Development (e.g., Django, Flask)
- Data Science and Machine Learning (e.g., Pandas, NumPy, scikit-learn)
- Automation/Scripting
- Game Development
- Desktop Apps
Setting Up Python:
1. Install Python from [Link]
2. Use IDLE, Jupyter Notebook, or VS Code
3. Check installation: python --version
Example:
print("Hello, World!")
Lab Task 1:
- Print your name, college, and branch.
Lecture 2: Variables, Data Types, Input/Output
Variables and Data Types:
name = "John" # String
age = 20 # Integer
pi = 3.14 # Float
is_student = True # Boolean
Type Conversion:
int(), float(), str(), bool()
Input/Output:
name = input("Enter your name: ")
print("Hello", name)
Comments:
# Single-line comment
''' Multi-line comment '''
Lab Task 2:
- Take two numbers as input and print their sum.
- Calculate area of a rectangle.
Lecture 3: Control Statements and Loops
Conditional Statements:
if condition:
# code
elif condition:
# code
else:
# code
Loops:
for i in range(5):
print(i)
while i < 5:
print(i)
i += 1
break and continue can be used to control loop flow.
Lab Task 3:
- Find largest of 3 numbers.
- Check if a number is prime.
- Print multiplication table using loop.
Lecture 4: Functions
Defining a Function:
def greet():
print("Hello")
Parameters and Return:
def add(a, b):
return a + b
Default and Keyword Arguments
Lab Task 4:
- Function to find factorial of a number.
- Function to reverse a string.
- Function to check whether a string is palindrome.
Lecture 5: Lists, Tuples, Sets
Lists:
my_list = [1, 2, 3]
my_list.append(4)
my_list.remove(2)
Tuples (Immutable):
my_tuple = (1, 2, 3)
Sets (No duplicates):
my_set = {1, 2, 3, 2}
Lab Task 5:
- Create a list of student names and print using loop.
- Create a tuple of marks and calculate total.
- Use set to remove duplicates from a list.
Lecture 6: Dictionaries and Strings
Dictionaries:
student = {"name": "Alice", "age": 22}
student["name"] = "Bob"
String Operations:
s = "Python Programming"
[Link](), [Link](), [Link]("a"), [Link]("a", "@")
Lab Task 6:
- Count vowels in a string.
- Create a dictionary with employee details.
- Reverse a string using slicing.
Lecture 7: File Handling
Write to File:
f = open("[Link]", "w")
[Link]("Hello")
[Link]()
Read from File:
f = open("[Link]", "r")
print([Link]())
Append to File:
f = open("[Link]", "a")
[Link]("More text")
[Link]()
Lab Task 7:
- Write and read student data.
- Count lines, words in a file.
- Append content to a file.
Lecture 8: Exception Handling
Try-Except Block:
try:
a = int(input("Enter number: "))
result = 10 / a
except ZeroDivisionError:
print("Can't divide by zero")
except ValueError:
print("Invalid input")
Lab Task 8:
- Handle invalid inputs.
- Handle file not found.
Lecture 9: Object-Oriented Programming
Class and Object:
class Student:
def __init__(self, name):
[Link] = name
def display(self):
print("Name:", [Link])
s1 = Student("John")
[Link]()
Lab Task 9:
- Class for Employee with attributes and method to display.
- Class for BankAccount with deposit and withdraw methods.
Lecture 10: Modules and Libraries
Standard Modules:
import math
[Link](25), [Link](2, 3)
import random
[Link](1, 100)
Lab Task 10:
- Generate 5 random numbers.
- Use math module to calculate area and volume.
Lecture 11: Mini Projects and Summary
Mini Project Ideas:
1. Student Marks Management System
2. Library Management System
3. Quiz App
4. To-do List Application
5. File Analyzer (count words, lines, characters)
Lab Task 11:
- Pick any project idea and build a working program using Python basics.
Final Notes:
- Revise all concepts
- Prepare for viva based on functions, file handling, OOP, etc.