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 *

Scroll to Top