Of course! Here is the complete Python Programming Lab Series (Lab 1 to Lab 15) in English, including:
- Short explanation
- Example code
- Student exercises
✅ Lab 1: Introduction to Python Programming
📘 Explanation:
Python is a simple yet powerful high-level programming language. It’s great for beginners and widely used in web development, data science, and automation.
🧪 Example Code:
print("Hello, Python!")
📝 Exercises:
- Print your name and age.
- Check the version of Python using:
import sys
print(sys.version)
✅ Lab 2: Basic Input/Output & String Operations
📘 Explanation:
input()
takes input from the user.print()
displays output.- Strings store text data.
🧪 Example Code:
name = input("Enter your name: ")
print("Welcome " + name)
📝 Exercises:
- Take name and city as input and display a welcome message.
- Reverse a string and print it.
- Convert a string to upper and lower case.
✅ Lab 3: Boolean Logic & Logical Operators
📘 Explanation:
Boolean values are either True
or False
.
Logical operators: and
, or
, not
.
🧪 Example Code:
a = 5
b = 10
print(a < 10 and b > 5)
📝 Exercises:
- Take two numbers and check if both are positive.
- Check eligibility using age and marks.
✅ Lab 4: Dates and Time
📘 Explanation:
Use the datetime
module to work with date and time.
🧪 Example Code:
import datetime
now = datetime.datetime.now()
print("Current date and time:", now)
📝 Exercises:
- Print the current date and time.
- Ask user for birth year and calculate their age.
✅ Lab 5: Conditional Statements
📘 Explanation:
Use if
, elif
, and else
to make decisions in your program.
🧪 Example Code:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult")
else:
print("You are underage")
📝 Exercises:
- Check if a number is positive, negative, or zero.
- Calculate grade based on marks.
✅ Lab 6: Loops
📘 Explanation:
Loops are used to repeat code.
🧪 Example Code:
for i in range(1, 6):
print("Number:", i)
📝 Exercises:
- Print numbers from 1 to 10.
- Print the table of 2.
- Create a countdown using a while loop.
✅ Lab 7: Functions
📘 Explanation:
Functions are reusable blocks of code.
🧪 Example Code:
def greet(name):
print("Hello, " + name)
greet("Ali")
📝 Exercises:
- Create a function that returns the square of a number.
- Create a function that takes two numbers and returns their sum.
✅ Lab 8: Lists, Indexing, and Slicing
📘 Explanation:
Lists store multiple items. Indexing and slicing help access elements.
🧪 Example Code:
fruits = ["apple", "banana", "mango"]
print(fruits[1])
print(fruits[0:2])
📝 Exercises:
- Create a list of 5 items.
- Print the last item in the list.
- Print only even numbers from a list.
✅ Lab 9: Numerical Operations
📘 Explanation:
You can perform math using operators and the math
module.
🧪 Example Code:
import math
print(math.sqrt(16))
📝 Exercises:
- Calculate the average of two numbers.
- Use the math module to find factorial and power.
✅ Lab 10: File Handling
📘 Explanation:
Use open()
to read and write files in different modes.
🧪 Example Code:
file = open("data.txt", "w")
file.write("Hello, file!")
file.close()
📝 Exercises:
- Create a file and write your name in it.
- Read from a file and display its contents.
✅ Lab 11: Object-Oriented Programming (OOP)
📘 Explanation:
OOP is a style of programming based on classes and objects.
🧪 Example Code:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello " + self.name)
s1 = Student("Ahmed")
s1.greet()
📝 Exercises:
- Create a class
Car
with model and year attributes. - Add a method to print the car’s information.
✅ Lab 12: Dictionaries and Tuples
📘 Explanation:
- Dictionary: Stores data in key-value pairs.
- Tuple: A fixed (immutable) list.
🧪 Example Code:
student = {"name": "Ali", "age": 21}
print(student["name"])
point = (10, 20)
print(point[0])
📝 Exercises:
- Create a dictionary with student information.
- Create a tuple and calculate the sum of elements.
✅ Lab 13: Data Visualization (Matplotlib)
📘 Explanation:
Use the matplotlib
library to create graphs and charts.
🧪 Example Code:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 4, 1]
plt.plot(x, y)
plt.title("Simple Graph")
plt.show()
📝 Exercises:
- Create a bar chart of prices of 3 fruits.
- Create a pie chart showing marks distribution.
✅ Lab 14: Computational Problem Solving
📘 Explanation:
This lab focuses on building logic and solving algorithmic problems.
🧪 Example Code (Fibonacci):
n = int(input("How many Fibonacci numbers? "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
📝 Exercises:
- Check if a number is prime.
- Calculate the factorial of a number.
- Count the number of vowels in a string.
- Print even numbers from a list.
✅ Lab 15: Error Handling and Debugging
📘 Explanation:
Use try-except
to handle runtime errors in your code.
🧪 Example Code:
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("You can't divide by zero!")
except ValueError:
print("Please enter a valid number!")
📝 Exercises:
- Create a division program that handles invalid inputs.
- Try to read a non-existing file and handle the error.