PYTHON PROGRAMMING LAB MANUAL
DS4P1: Python Programming Lab
Mr. Jeevanandham Subramaniyam
Assistant Professor
[Link] Subramaniyam
Assistant Professor
Objective:
This lab manual is designed to provide hands-on experience with Python programming
concepts including basics, control structures, data structures, functions, object-oriented
programming, exception handling, GUI development, and data analysis libraries.
System Requirements
1. Computer System – Desktop or Laptop
2. Processor – Intel i3 / AMD equivalent or higher
3. RAM – Minimum 4 GB (8 GB recommended)
4. Hard Disk – Minimum 20 GB free space
5. Display – Monitor with 1024 × 768 resolution or higher
6. Input Devices – Keyboard and Mouse
Software Requirements
1. Operating System
o Windows 10 / Windows 11
o Linux (Ubuntu)
o macOS
2. Programming Language
o Python 3.x
3. Python IDE / Editor
o IDLE (Python default editor)
o VS Code / PyCharm / Jupyter Notebook
4. Python Libraries Required
o Tkinter – For GUI programs
o Matplotlib – For charts and graphs
o NumPy – For array operations
o Pandas – For data frame and Excel operations
5. Additional Software
o Microsoft Excel (for Excel sheet used in Pandas program)
[Link] Subramaniyam
Assistant Professor
Section-A
1. Write simple Python program to display message on screen
2. Write simple Python program using operators:
a) Arithmetic Operators b) Logical Operators c) Bitwise Operators
3. Write a Python program to create a calculator program.
4. Write simple Python program to demonstrate use of conditional statements:
a) if statement b) if ... else statement c) Nested if statement
5. Write python program to demonstrate use of looping statements:
a) while loop b) for loop c) Nested loops
6. Write python program to perform following operations on Lists:
a) Create list b) Access list c) Update list (Add item, Remove item) d) Delete list
7. Write python program to perform following operations on Tuples:
a) Create Tuple b) Access Tuple c) Update Tuple d) Delete Tuple
8. Write python program to perform following operations on Tuples:
a) Create Set b) Access Set elements c) Update Set d) Delete Set
9. Write python program to perform following operations on Dictionaries:
a) Create Dictionary b) Access Dictionary elements c) Update Dictionary d) Delete Set
10. Write Python program to demonstrate math built- in functions
Section-B
11. Write Python program to demonstrate string built — in functions _
12. Develop user defined Python function for given problem:
a) Function with minimum 2 arguments b) Function returning values
13. Write a program in Python to demonstrate following operations:
a) Method overloading b) Method overriding
14. Write a program in Python to demonstrate following operations:
a) Simple inheritance b) Multiple inheritance
15. Write a program in Python to handle user defined exception.
16. Create a GUI using Tkinter module.
17. Drawing Line chart and Bar chart using Matplotlib.
18. Drawing Histogram and Pie chart using Matplotlib.
19. Create Array using NumPy and Perform Operations on Array.
20. Create Data Frame from Excel sheet using Pandas and Perform Operations on Data Frames.
[Link] Subramaniyam
Assistant Professor
GUIDELINES TO STUDENTS
1. Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care. Any
damage is caused is punishable.
2. Students are instructed to come to lab in formal dresses only.
3. Students are supposed to occupy the systems allotted to them and are not supposed to
talk or make noise in the lab.
4. Students are required to carry their observation book and lab records with completed
exercises while entering the lab.
5. Lab records need to be submitted every week. 6. Students are not supposed to use pen
drives in the lab.
SECTION – A
Experiment 1: Display Message on Screen
Aim: To write a simple Python program to display a message.
Program:
print("\n Welcome to Python Programming Lab")
print(“\n I Love Python Programming”)
print("\n This is a simple program to display a message on the screen.")
Output:
Welcome to Python Programming Lab
I Love Python Programming
This is a simple program to display a message on the screen.
[Link] Subramaniyam
Assistant Professor
Experiment 2: Operators in Python
Aim: To demonstrate Arithmetic, Logical, and Bitwise operators.
Program:
print("\n Python Program using Menu with Operators ")
print("\n1. Arithmetic Operators")
print("\n2. Logical Operators")
print("\n3. Bitwise Operators")
choice = int(input("\nEnter your choice: "))
a = int(input("\nEnter first number: "))
b = int(input("\nEnter second number: "))
if choice == 1:
print("\nArithmetic Operations")
print("\nAddition:", a + b)
print("\n Subtraction:", a - b)
print("\n Multiplication:", a * b)
print("\n Division:", a / b)
print("\n Modulus:", a % b)
elif choice == 2:
print("\nLogical Operations")
print("\n AND:", (a > 0 and b > 0))
print("\n OR:", (a > 0 or b > 0))
print("\n NOT a:", not(a > 0))
elif choice == 3:
print("\nBitwise Operations")
print("\n Bitwise AND:", a & b)
print("\n Bitwise OR:", a | b)
print("\n Bitwise XOR:", a ^ b)
else:
print("\n Invalid Choice")
Output:
Python Program using Menu with Operators
1. Arithmetic Operators
2. Logical Operators
3. Bitwise Operators
Enter your choice: 1
Enter first number: 10
[Link] Subramaniyam
Assistant Professor
Enter second number: 5
Arithmetic Operations
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Experiment 3: Calculator Program
Aim: To create a simple calculator.
Program:
print("------ Simple Calculator ------")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = int(input("Enter your choice (1-4): "))
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == 1:
print("Result =", a + b)
elif choice == 2:
print("Result =", a - b)
elif choice == 3:
print("Result =", a * b)
elif choice == 4:
if b != 0:
print("Result =", a / b)
else:
print("Division by zero is not allowed")
else:
print("Invalid choice")
Output:
------ Simple Calculator ------
1. Addition
2. Subtraction
3. Multiplication
4. Division
[Link] Subramaniyam
Assistant Professor
Enter your choice (1-4): 1
Enter first number: 10
Enter second number: 5
Result = 15
Experiment 4: Conditional Statements
Aim: To demonstrate if, if-else, and nested if statements.
Program:
Print(“\nPython Program to demonstrate Conditional Statements”)
num = int(input("Enter a number: "))
print(“ \n a. if statement”)
if num > 0:
print("The number is Positive")
print(“ \n b. if...else statement”)
if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")
print(“ \n c. Nested if statement”)
if num >= 0:
if num == 0:
print("The number is Zero")
else:
print("The number is Positive")
else:
print("The number is Negative")
Output:
Enter a number: 5
The number is Positive
The number is Odd
The number is Positive
[Link] Subramaniyam
Assistant Professor
Experiment 5: Looping Statements
Aim: To demonstrate while loop, for loop, and nested loops.
Program:
Print(”\n Python Program to demonstrate Looping Statements”)
print("a) While Loop")
i=1
while i <= 5:
print(i)
i=i+1
print("\nb) For Loop")
for j in range(1, 6):
print(j)
print("\nc) Nested Loop")
for i in range(1, 4):
for j in range(1, 4):
print("i =", i, "j =", j)
Output:
a) While Loop
1
2
3
4
5
b) For Loop
1
2
3
4
5
c) Nested Loop
i=1j=1
i=1j=2
i=1j=3
i=2j=1
i=2j=2
i=2j=3
i=3j=1
[Link] Subramaniyam
Assistant Professor
i=3j=2
i=3j=3
Experiment 6: List Operations
Aim: To perform operations on lists.
Program:
Print(“\n Python Program for List Operations”)
mylist = [10, 20, 30, 40]
print("Created List:", mylist)
print("First element:", mylist[0])
print("Third element:", mylist[2])
[Link](50)
print("List after adding element:", mylist)
[Link](20)
print("List after removing element:", mylist)
del mylist
print("List deleted")
Output:
Created List: [10, 20, 30, 40]
First element: 10
Third element: 30
List after adding element: [10, 20, 30, 40, 50]
List after removing element: [10, 30, 40, 50]
List deleted
[Link] Subramaniyam
Assistant Professor
Experiment 7: Tuple Operations
Aim: To demonstrate tuple operations.
Program:
Print(“\n Python Program for Tuple Operations”)
t = (10, 20, 30, 40)
print("Created Tuple:", t)
print("First element:", t[0])
print("Third element:", t[2])
temp = list(t)
[Link](50)
t = tuple(temp)
print("Updated Tuple:", t)
del t
print("Tuple deleted")
Output:
Created Tuple: (10, 20, 30, 40)
First element: 10
Third element: 30
Updated Tuple: (10, 20, 30, 40, 50)
Tuple deleted
[Link] Subramaniyam
Assistant Professor
Experiment 8: Set Operations
Aim: To demonstrate set operations.
Program:
Print”\n Python Program for Set Operations”)
s = {10, 20, 30, 40}
print("Created Set:", s)
print("Accessing elements in the set:")
for i in s:
print(i)
[Link](50)
print("Set after adding element:", s)
[Link](20)
print("Set after removing element:", s)
del s
print("Set deleted")
Output:
Created Set: {10, 20, 30, 40}
Accessing elements in the set:
10
20
30
40
Set after adding element: {10, 20, 30, 40, 50}
[Link] Subramaniyam
Assistant Professor
Set after removing element: {10, 30, 40, 50}
Set deleted
Experiment 9: Dictionary Operations
Aim: To perform dictionary operations.
Program:
Print(”\n Python Program for Dictionary Operations”)
student = {
"name": "Ravi",
"age": 20,
"course": "BCA"
print("Created Dictionary:", student)
print("Student Name:", student["name"])
print("Student Age:", student["age"])
student["age"] = 21 # Update value
student["college"] = "ABC College" # Add new key-value pair
print("Updated Dictionary:", student)
del student["course"]
print("Dictionary after deleting element:", student)
Output:
Created Dictionary: {'name': 'Ravi', 'age': 20, 'course': 'BCA'}
Student Name: Ravi
Student Age: 20
Updated Dictionary: {'name': 'Ravi', 'age': 21, 'course': 'BCA', 'college':
'ABC College'}
[Link] Subramaniyam
Assistant Professor
Dictionary after deleting element: {'name': 'Ravi', 'age': 21, 'college':
'ABC College'}
Experiment 10: Math Built-in Functions
Aim: To demonstrate math functions.
Program:
Print(”\nPython Program to demonstrate Math Built-in Functions”)
import math
num = 16
print("Square Root:",[Link](num))
print("Power:", [Link](2, 3))
print("Ceiling Value:", [Link](4.3))
print("Floor Value:", [Link](4.8))
print("Factorial:", [Link](5))
print("Absolute Value:", abs(-10))
Output:
Square Root: 4.0
Power: 8.0
Ceiling Value: 5
Floor Value: 4
Factorial: 120
Absolute Value: 10
[Link] Subramaniyam
Assistant Professor
SECTION – B
Experiment 11: String Built-in Functions
Aim: To demonstrate string functions.
Program:
Print(“\nPython Program to demonstrate String Built-in Functions”)
s = "Python Programming"
print("Original String:", s)
print("Uppercase:", [Link]())
print("Lowercase:", [Link]())
print("Length of String:", len(s))
print("Replace:", [Link]("Python", "Java"))
print("Split:", [Link]())
print("Starts with Python:", [Link]("Python"))
Output:
Original String: Python Programming
Uppercase: PYTHON PROGRAMMING
Lowercase: python programming
Length of String: 18
Replace: Java Programming
Split: ['Python', 'Programming']
Starts with Python: True
[Link] Subramaniyam
Assistant Professor
Experiment 12: User Defined Functions
Aim: To create functions with arguments and return values.
Program:
Print(”\nPython Program for User Defined Function”)
def add(a, b):
result = a + b
return result # returning value
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
sum_value = add(x, y)
print("Sum of two numbers is:", sum_value)
Output:
Enter first number: 10
Enter second number: 5
Sum of two numbers is: 15
[Link] Subramaniyam
Assistant Professor
Experiment 13: Method Overloading and Overriding
Aim: To demonstrate method overloading and overriding.
Program:
Print(”\nProgram for demonstrate Method Overloading & Method Overriding”)
class Demo:
def add(self, a, b=0):
result = a + b
print("Sum =", result)
obj = Demo()
[Link](10)
[Link](10, 20)
class A:
def show(self):
print("This is Parent Class Method")
class B(A):
def show(self):
print("This is Child Class Method")
obj1 = B()
[Link]()
Output:
Sum = 10
Sum = 30
This is Child Class Method
[Link] Subramaniyam
Assistant Professor
Experiment 14: Inheritance
Aim: To demonstrate simple and multiple inheritance.
Program:
Print(“\nProgram to demonstrate of Simple Inheritance & Multiple Inhertance”)
class Parent:
def display(self):
print("This is Parent Class")
class Child(Parent):
def show(self):
print("This is Child Class")
obj = Child()
[Link]()
[Link]()
# Multiple Inheritance
class A:
def method1(self):
print("This is Class A")
class B:
def method2(self):
print("This is Class B")
class C(A, B):
def method3(self):
print("This is Class C")
[Link] Subramaniyam
Assistant Professor
obj2 = C()
obj2.method1()
obj2.method2()
obj2.method3()
Output:
This is Parent Class
This is Child Class
This is Class A
This is Class B
This is Class C
Experiment 15: User Defined Exception
Aim: To handle user defined exception.
Program:
Print(“\n Python Program for User Defined Exception”)
class NegativeNumberError(Exception):
pass
try:
num = int(input("Enter a number: "))
if num < 0:
raise NegativeNumberError("Negative numbers are not allowed")
print("You entered:", num)
except NegativeNumberError as e:
print("Exception:", e)
[Link] Subramaniyam
Assistant Professor
Output:
Enter a number: -5
Exception: Negative numbers are not allowed
Enter a number: 10
You entered: 10
Experiment 16: GUI using Tkinter
Aim: To create a simple GUI.
Program:
Print(“\n Python Program to create a GUI using Tkinter”)
from tkinter import *
root = Tk()
[Link]("Simple GUI")
[Link]("300x200")
label = Label(root, text="Welcome to Python GUI", font=("Arial", 14))
[Link](pady=20)
button = Button(root, text="Click Me")
[Link]()
[Link]()
Output:
[Link] Subramaniyam
Assistant Professor
Experiment 17: Line and Bar Chart using Matplotlib
Program:
Print(“\n Python Program to draw Line Chart and Bar Chart using Matplotlib”)
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]
[Link](1)
[Link](x, y, marker='o')
[Link]("Line Chart")
[Link]("X Values")
[Link]("Y Values")
[Link]()
# Bar Chart
[Link](2)
[Link](x, y)
[Link]("Bar Chart")
[Link]("X Values")
[Link]("Y Values")
[Link]()
Output:
[Link] Subramaniyam
Assistant Professor
Experiment 18: Histogram and Pie Chart
Program:
Print(“\n Python Program to draw Histogram and Pie Chart”)
import [Link] as plt
data = [10, 20, 20, 30, 30, 30, 40, 40, 50]
[Link](data)
[Link]("Histogram")
[Link]("Values")
[Link]("Frequency")
[Link]()
# Pie Chart Data
labels = ["A", "B", "C", "D"]
sizes = [30, 25, 20, 25]
[Link](sizes, labels=labels, autopct='%1.1f%%')
[Link]("Pie Chart")
[Link]()
Output:
[Link] Subramaniyam
Assistant Professor
Experiment 19: NumPy Array Operations
Program:
Print(“\n Python Program to Create NumPy Array and Perform Operations”)
import numpy as np
arr = [Link]([10, 20, 30, 40, 50])
print("Array Elements:", arr)
print("Sum of Array:", [Link](arr))
print("Mean of Array:", [Link](arr))
print("Maximum Value:", [Link](arr))
print("Minimum Value:", [Link](arr))
print("Array + 5:", arr + 5)
print("Array * 2:", arr * 2)
Output:
Array Elements: [10 20 30 40 50]
Sum of Array: 150
Mean of Array: 30.0
Maximum Value: 50
Minimum Value: 10
Array + 5: [15 25 35 45 55]
Array * 2: [20 40 60 80 100]
Experiment 20: Pandas DataFrame from Excel
Program:
Print(“\nPython Program to Create DataFrame from Excel using Pandas”)
import pandas as pd
df = pd.read_excel("[Link]")
print("Data from Excel File:")
print(df)
[Link] Subramaniyam
Assistant Professor
print("\nFirst 5 Rows:")
print([Link]())
print("\nColumn Names:")
print([Link])
print("\nSummary Statistics:")
print([Link]())
print("\nStudent Names:")
print(df["Name"])
Output:
Data from Excel File:
Name Age Marks
0 Ravi 20 85
1 Anu 21 90
2 John 19 75
First 5 Rows:
Name Age Marks
0 Ravi 20 85
1 Anu 21 90
2 John 19 75
[Link] Subramaniyam
Assistant Professor