0% found this document useful (0 votes)
4 views28 pages

Python Programming Lab Experiments

The document is a lab file for a Python Programming Lab course at GL Bajaj Institute of Technology and Management. It includes various programming experiments that cover topics such as variable storage, arithmetic operations, string manipulation, file handling, data visualization, and GUI programming using Tkinter. Each experiment outlines objectives, sample programs, and expected outputs.

Uploaded by

gufran1732326
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Python Programming Lab Experiments

The document is a lab file for a Python Programming Lab course at GL Bajaj Institute of Technology and Management. It includes various programming experiments that cover topics such as variable storage, arithmetic operations, string manipulation, file handling, data visualization, and GUI programming using Tkinter. Each experiment outlines objectives, sample programs, and expected outputs.

Uploaded by

gufran1732326
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:-GUFRAN Mr. Abhishek Singh
AHAMAD Assistant Professor
Roll No.:2401921520107 Department of CSE-AI
Branch:CS-AI 2
Semester:III
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

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 2401921520107
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. – 01
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: student_name=input("Enter your name: ")

student_age=int(input("Enter your age: "))

ques=input("Are you a student? (Y/N): ")

print()

print("-- Student Information ---")


print("Name:", student_name)

print("Age:", student_age)

if ques=="Y" or ques=="y":

print(student_name, "is a student.")


else:

print(student_name, "is not a student.")

OUTPUT:

CSAI 2401921520107
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. – 02
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: num_1=int(input("Enter first number: "))

num_2=int(input("Enter second number: "))

print()

print("Addition:", num_1 + num_2)


print("Subtraction:", num_1 - num_2)

print("Multiplication:", num_1 * num_2)

print("Division:", num_1 / num_2)

OUTPUT:

CSAI 2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
#2. LIST OPERATIONS

numbers = [10, 20, 30, 40, 50]

print("\n- List Operations -")

print("Original list:", numbers)

# List slicing

print("First 3 elements:", numbers[:3])

#Appending an item

[Link](60)

print("After appending 60:", numbers)

#Inserting an item at index 2

[Link](2, 25)

print("After inserting 25 at index 2:", numbers)

# Removing an item

[Link](40)

print("After removing 40:", numbers)

#Accessing elements using a loop

print("List elements:")

for n in numbers:

print(n)

2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Department of Computer Science & Engineering-AI
Greater Noida

[Link]()

OUTPUT:

CSAI 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Department of Computer Science & Engineering-AI
Greater Noida

OUTPUT:

CSAI 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 SamleData

[Link](42)#For reproducible results

#Generate 100RandomStudents Marks (0-100)

marks_math =[Link](50, 100, 100)

marks_science=[Link](45, 100, 100)

marks_english=[Link](55, 100, 100)

#CreateAPythonDataFrame

data=[Link]({"Math": marks_math,

"Science": marks_science,

"English": marks_english })

CSAI 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Department of Computer Science & Engineering-AI
Greater Noida

EXPERIMENT NO. –10


OBJECTIVE:- ImplementTkinterandPythonprogramming for calculators.

PROGRAM:
import tkinteras tk

def click(event):

text=[Link]("text")

iftext=="=":

try:

# Evaluate the expression in the entry widget

result = str(eval([Link]()))

[Link](0, [Link])

[Link]([Link], result)

exceptException as e:

[Link](0, [Link])

[Link]([Link], "Error")

eliftext=="C":

#Clearthe entry widget

[Link](0, [Link])

else:
CSAI 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
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 2401921520107
GL BAJAJ
Institute of Technologies & Management
[Approved by AICTE, Govt. of India & Affiliated to Dr. APJ
Abdul Kalam Technical University, Lucknow, U.P., India]
Department of Computer Science & Engineering-AI
Greater Noida

[Link]("", click)

[Link]()#Run the GUI event loop

OUTPUT:

CSAI 2401921520107

You might also like