B.
TECH
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING-AI
PYTHON PROGRAMMING LAB
(BCC - 302)
LAB FILE
GL BAJAJ Institute of Technology and Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
2025-26
Submitted By: Submitted To:
Name:Avesh Maurya [Link] Singh
Roll No.:2401921520069 Assistant Professor
Branch:CS-AI Department of CSE-AI
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
Semester:III
INDEX
Date of Date of
[Link]. Name of Experiment Sign.
Experiment Submission
Write a Python program that:
• Stores your name, age, and whether you're a student in separate
1.
variables.
• Prints them all with clear labels.
Write a Python program that:
• Takes two numbers (you can assign them manually)
2.
• Performs and prints the result of addition, subtraction,
multiplication, division, and modulus
Write a Python program that asks the user to enter a number. (Use
3.
if, elif, and else to check if the number is positive, negative, or zero)
Write a Python program that does the following :
4. • Use a for loop with range to print numbers from 1 to 10.
• Inside the loop, skip printing even numbers using continue.
Write a Python program to perform basic operations on strings,
lists, and tuples.
• Accept a sentence and display various string manipulation
results like length, slicing, case conversion, finding substrings,
5. etc.
• Create a list of numbers or items, perform list slicing,
appending, inserting, and removing items.
• Define a tuple with at least 5 elements and demonstrate access
using indexing and slicing.
Write a Python program that uses dictionaries and functions to store
and process student information.
• Use a dictionary to store student names and their marks.
• Perform dictionary operations like adding a new student,
6.
updating marks, deleting entries, and displaying all records.
• Define and use functions for organizing code, such as: function
to add a student, function to calculate average marks, function
to display student details
Write a Python program to open a text file and demonstrate the use
7.
of read(), readline(), and readlines() functions.
Write a Python program to create a text file and write multiple lines
8.
into it using write() and writelines().
Data Visualization, Analysis using Matplotlib, Pandas,
9.
and NumPy.
10. Implement Tkinter and Python programming for calculators.
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. – 1
OBJECTIVE: Write a Python program that:
• Stores your name, age, and whether you're a student in separate variables.
• Prints them all with clear labels.
PROGRAM:
studname=input("Enter Your Name:")
studage=input("Enter Your Age:")
ques=input("Are You A Student?(Y/N):")
print()
print("===Student Deatils===")
print("Student Name:",studname)
print("Student Age:",studage)
if ques=="Y":
print(studname,"Is A Student")
else:
print(studname,"Is Not A Student")
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
OUTPUT:
EXPERIMENT NO. – 2
OBJECTIVE: Write a Python program that:
• Takes two numbers (you can assign them manually)
• Performs and prints the result of addition, subtraction, multiplication, division, and modulus
PROGRAM:
num1=int(input("Enter The First NUmber:"))
num2=int(input("Enter The Second NUmber: "))
print("Additon\n")
print(num1+num2)
print("Subtraction\n")
print(num1-num2)
print("Multiplication\n")
print(num1*num2)
print("Division\n")
print(num1/num2)
OUTPUT:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. – 3
OBJECTIVE:-
Write a Python program that asks the user to enter a number. (Use if, elif, and else to check if the number is positive,
negative, or zero)
PROGRAM:-
num = float(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
OUTPUT:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. – 4
OBJECTIVE
OBJECTIVE:- Write a Python program that does the following :
• Use a for loop with range to print numbers from 1 to 10.
• Inside the loop, skip printing even numbers using continue.
PROGRAM:-
PROGRAM
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
OUTPUT:-
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. – 5
OBJECTIVE:-
Write a Python program to perform basic operations on strings, lists, and tuples.
• Accept a sentence and display various string manipulation results like length, slicing, case conversion, finding
substrings, etc.
• Create a list of numbers or items, perform list slicing, appending, inserting, and removing items.
• Define a tuple with at least 5 elements and demonstrate access using indexing and slicing.
:- PROGRAM: -
#string operations
sentence = input("Enter a sentence: ")
print("\n--- String Operations ---")
print("Original Sentence:", sentence)
print("Length of sentence:", len(sentence))
print("First 5 characters:", sentence[:5])
print("Last 5 characters:", sentence[-5:])
print("Uppercase:", [Link]())
print("Lowercase:", [Link]())
print("Title Case:", [Link]())
substr = input("Enter a word to find in the sentence: ")
if substr in sentence:
print(f"'{substr}' found at index:", [Link](substr))
else:
print(f"'{substr}' not found in the sentence.")
#list operations
numbers = [10, 20, 30, 40, 50]
print("\n--- List Operations ---")
print("Original List:", numbers)
print("First 3 elements:", numbers[:3])
print("Last 2 elements:", numbers[-2:])
[Link](60)
print("After appending 60:", numbers)
[Link](2, 25)
print("After inserting 25 at index 2:", numbers)
[Link](40)
print("After removing 40:", numbers)
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
# Tuple Operations
fruits = ("apple", "banana", "cherry", "mango", "orange")
print("\n--- Tuple Operations ---")
print("Tuple:", fruits)
print("First element:", fruits[0])
print("Last element:", fruits[-1])
print("Middle 3 elements:", fruits[1:4])
print("Length of tuple:", len(fruits))
OUTPUT:-
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. – 6
OBJECTIVE:-
O Write a Python program that uses dictionaries and functions to store and process student information.
• Use a dictionary to store student names and their marks.
• Perform dictionary operations like adding a new student, updating marks, deleting entries, and displaying all records.
• Define and use functions for organizing code, such as: function to add a student, function to calculate average
marks, function to display student details
:- PROGRAM: -
students = {}
def add_student(name, marks):
students[name] = marks
print(f"Added student: {name} with marks: {marks}")
def update_marks(name, new_marks):
if name in students:
students[name] = new_marks
print(f"Updated {name}'s marks to {new_marks}")
else:
print(f"Student '{name}' not found!")
def delete_student(name):
if name in students:
del students[name]
print(f"Deleted record of student: {name}")
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
else:
print(f"Student '{name}' not found!")
def display_students():
print("\n--- Student Records ---")
if not students:
print("No records found.")
else:
for name, marks in [Link]():
print(f"Name: {name}, Marks: {marks}")
def calculate_average():
if not students:
print("No students to calculate average.")
else:
avg = sum([Link]()) / len(students)
print(f"Average Marks of all students: {avg:.2f}")
while True:
print("\n===== Student Information Menu =====")
print("1. Add Student")
print("2. Update Marks")
print("3. Delete Student")
print("4. Display All Students")
print("5. Calculate Average Marks")
print("6. Exit")
choice = input("Enter your choice (1-6): ")
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
if choice == '1':
name = input("Enter student name: ")
marks = float(input("Enter marks: "))
add_student(name, marks)
elif choice == '2':
name = input("Enter student name to update: ")
marks = float(input("Enter new marks: "))
update_marks(name, marks)
elif choice == '3':
name = input("Enter student name to delete: ")
delete_student(name)
elif choice == '4':
display_students()
elif choice == '5':
calculate_average()
elif choice == '6':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please try again.")
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
OUTPUT:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. –7
OBJECTIVE:- Write a Python program to open a text file and demonstrate the use of read(), readline(), and readlines()
functions.
PROGRAM:
file_path = "[Link]"
file = open(file_path, "r") # Open file in read mode
print("Using read():")
content = [Link]() # Read the entire file print(content)
[Link]() # Close the file
print("-" * 40)
file = open(file_path, "r")
print("Using readline():")
line1 = [Link]() # Read the first line
print("Line 1:", [Link]())
line2 = [Link]() # Read the second line
print("Line 2:", [Link]())
[Link]()
print("-" * 40)
file = open(file_path, "r")
print("Using readlines():")
lines = [Link]() # Read all lines into a list
for i, line in enumerate(lines, start=1):
print(f"Line {i}: {[Link]()}")
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
[Link]()
OUTPUT:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. –8
OBJECTIVE:- Write a Python program to create a text file and write multiple lines into it using write() and
writelines().
PROGRAM:
file_path = "[Link]" # Name of the file to create
with open(file_path, "w") as file:
[Link]("This is the first line.\n")
[Link]("This is the second line.\n")
[Link]("This is the third line.\n")
print("Lines written to file using write().")
lines = [ "Fourth line using writelines.\n",
"Fifth line using writelines.\n",
"Sixth line using writelines.\n" ]
with open(file_path, "a") as file:
# 'a' mode appends to the file
[Link](lines)
print("Additional lines written to file using writelines().")
with open(file_path, "r") as file:
print("\nContent of the file:")
print([Link]())
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
OUTPUT:
OBJECTIVE
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. –9
OBJECTIVE:- Data Visualization, Analysis using Matplotlib, Pandas, and NumPy.
PROGRAM:
#import libraries
import numpy as np
import pandas as pd
import [Link] as plt
#STEP 1:Create Sample Data
[Link](42) # For reproducible results
#Generate 100 Random Students Marks (0-100)
marks_math = [Link](50, 100, 100)
marks_science = [Link](45, 100, 100)
marks_english = [Link](55, 100, 100)
#Create A Python Data Frame
data = [Link]({ "Math": marks_math,
"Science": marks_science,
"English": marks_english })
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
#STEP 2:Analyze Data
print("First 5 rows of data:")
print([Link]())
print("\nSummary Statistics:")
print([Link]()) # Mean, min, max, std, etc.
#Calculate Average Marks For Each Student
data["Average"] = [Link](axis=1)
#Count Students Which Have Marks More Than 75
high_math = (data["Math"] > 75).sum()
print(f"\nNumber of students scoring above 75 in Math: {high_math}")
#STEP 3:-Data Visulaization
#1. histogram of Math Marks
[Link](figsize=(8,5))
[Link](data["Math"], bins=10, color="skyblue", edgecolor="black")
[Link]("Distribution of Math Marks")
[Link]("Marks")
[Link]("Number of Students")
[Link]()
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
#2. Scatter Plot: Science VS English Marks
[Link](figsize=(8,5))
[Link](data["Science"], data["English"], color="green")
[Link]("Science vs English Marks")
[Link]("Science Marks")
[Link]("English Marks")
[Link]()
#3. Bar Plot: Average Marks Of First 10 Students
[Link](figsize=(10,5))
[Link]([Link][:10], data["Average"][:10], color="orange")
[Link]("Average Marks of First 10 Students")
[Link]("Student Index")
[Link]("Average Marks")
[Link]()
#[Link] PLot: Average Marks trend
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
[Link](figsize=(10,5))
[Link](data["Average"], marker='o', linestyle='-', color="purple")
[Link]("Average Marks Trend")
[Link]("Student Index")
[Link]("Average Marks")
[Link]()
OUTPUT:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
EXPERIMENT NO. –10
OBJECTIVE:- Implement Tkinter and Python programming for calculators.
PROGRAM:
import tkinter as tk
def click(event):
text = [Link]("text")
if text == "=":
try:
# Evaluate the expression in the entry widget
result = str(eval([Link]()))
[Link](0, [Link])
[Link]([Link], result)
except Exception as e:
[Link](0, [Link])
[Link]([Link], "Error")
elif text == "C":
# Clear the entry widget
[Link](0, [Link])
else:
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
# Insert the clicked button's text into the entry
[Link]([Link], text)
root = [Link]()
[Link]("Simple Calculator")
[Link]("300x400")
entry = [Link](root, font=("Arial", 20))
[Link](fill=[Link], ipadx=8, pady=10, padx=10)
button_frame = [Link](root) button_frame.pack()
buttons = [
['7', '8', '9', '/'],
['4', '5', '6', '*'],
['1', '2', '3', '-'],
['0', '.', '=', '+'],
['C']
for i, row in enumerate(buttons):
for j, btn_text in enumerate(row):
b = [Link](button_frame, text=btn_text, font=("Arial", 18), width=5, height=2)
[Link](row=i, column=j, padx=5, pady=5)
CSAI 2401921520069
GL BAJAJ [Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Institute of Technologies & Management Department of Computer Science & Engineering-AI
Greater Noida
[Link]("", click)
[Link]()#Run the GUI event loop
OUTPUT:
CSAI 2401921520069