Computer Programming Labs 24CE (English)

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:

  1. Print your name and age.
  2. 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:

  1. Take name and city as input and display a welcome message.
  2. Reverse a string and print it.
  3. 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:

  1. Take two numbers and check if both are positive.
  2. 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:

  1. Print the current date and time.
  2. 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:

  1. Check if a number is positive, negative, or zero.
  2. 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:

  1. Print numbers from 1 to 10.
  2. Print the table of 2.
  3. 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:

  1. Create a function that returns the square of a number.
  2. 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:

  1. Create a list of 5 items.
  2. Print the last item in the list.
  3. 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:

  1. Calculate the average of two numbers.
  2. 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:

  1. Create a file and write your name in it.
  2. 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:

  1. Create a class Car with model and year attributes.
  2. 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:

  1. Create a dictionary with student information.
  2. 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:

  1. Create a bar chart of prices of 3 fruits.
  2. 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:

  1. Check if a number is prime.
  2. Calculate the factorial of a number.
  3. Count the number of vowels in a string.
  4. 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:

  1. Create a division program that handles invalid inputs.
  2. Try to read a non-existing file and handle the error.

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon
wpChatIcon
Scroll to Top