GEETANJALI INSTITUTE OF TECHNICAL STUDIES
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SESSION 2025-26
Python Lab
(6CS4-23)
SUBMITTED TO: SUBMITTED BY:
Dr Ruchi Vyas Shubham Vaishnav
Associate Professor 23EGICS163
SEC. - C
Python Lab (6CS4-23)
COURSEOUTCOMES(COs)
DescribethePythonlanguagesyntaxincludingcontrolstatements,loopsand functions to
CO1 write programs for a wide varietyproblem in mathematics, science, and
games.
Examine the core data structures like lists, dictionaries, tuples and sets in Python to
CO2
store, process and sort the data.
InterprettheconceptsofObject-orientedprogrammingasusedinPythonusing encapsulation,
CO3
polymorphism and inheritance.
Discover the capabilitiesofPythonregularexpressionfordataverificationand utilize
CO4
matrices for building performance efficient Python programs.
Identifythe external modules for creating and writing data to excel files and inspect the
CO5
file operations to navigate the file systems.
2
Python Lab (6CS4-23)
INDEX
[Link]. Description Date Grade
&Signature
Write a Python program to demonstrate basic data types in
Python.
1. Write a Python program to calculate distance between two
points taking input from the user.
Write a Python program that takes two numbers and prints
their sum.
2. Write a Python program to check whether the given number
is even or not.
Write a Python program, using a for loop, that prints out the
decimal equivalents of 1/2, 1/3, 1/4, ..., 1/10.
3.
Write a program to demonstrate list in Python.
Write a program to demonstrate tuple in Python.
Write a program using a for loop that loops over a
sequence.
4.
Write a program using a while loop that asks the user for a
number and prints a countdown from that number to zero.
Find the sum of all the primes below two million.
By considering the terms in the Fibonacci sequence whose
5. value does not exceed 4 million, write a program to find the
sum of even-valued terms.
Write a program to count the number of characters in a
string and store them in a dictionary data structure.
6.
Write a program to use split and join methods in the
string and trace a birthday of a person with a dictionary
data structure.
7. Write a program to count frequency of characters in a given file.
Can you use character frequency to tell whether the given file is
3
Python Lab (6CS4-23)
a Python program file, C program file, or a text file?
Write a program to print each line of a file in reverse order.
8.
Write a program to compute the number of characters,
words, and lines in a file.
Write a function nearly equal to test whether two strings are
nearly equal.
9.
Write functions to compute GCD and LCM of two numbers
(each function should not exceed one line)..
Write a program to implement Merge Sort.
10.
Write a program to implement Selection Sort and Insertion Sort.
4
Python Lab (6CS4-23)
Practical No.1(a)
Aim: Write a program to demonstrate basic data type in python.
Code:
name = input("Enter Student Name: ")
age = int(input("Enter Student Age: "))
percent = float(input("Enter Student Percentage: "))
course = input("Enter Course Details: ")
Roll_no = input("Enter Roll Number: ")
print("LIST data type")
students_list = [name, age, percent, course]
print("Student List: (Name, Age, Percentage, Course)\n", students_list)
print("\nTUPLE data type")
student_tuple = (name, age, percent, course)
print("Student Tuple (Name, Age, Percentage, Course)\n", student_tuple)
print("\nDICTIONARY ")
student_dict = {
"name": name,
"roll_no": Roll_no,
"age": age,
"percent": percent,
"course": course
}
print("Student Dictionary:", student_dict)
print("\nSET (Unordered, Unique Values)")
student_set = {name, age, percent, course}
print("Student Set:", student_set)
4. Shubham Chouhan 23EGICS160
Python Lab (6CS4-23)
Output:
6
Python Lab (6CS4-23)
Practical No.1(b)
Aim: Write a python program to calculate distance between two points taking input
from the user.
Code:
import math
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
distance = [Link]((x2 - x1)**2 + (y2 - y1)**2)
print("Distance between two points is:", distance)
Output
7
Python Lab (6CS4-23)
Practical No.2(a)
Aim: Write a Python program that takes two numbers and prints their sum.
Code:
import sys
if len([Link]) != 3:
print("Usage: python [Link] <num1><num2>")
[Link](1)
num1 = float([Link][1])
num2 = float([Link][2])
print("Sum =", num1 + num2)
Output:
8
Python Lab (6CS4-23)
Practical No.2(b)
Aim:Write a python program to check whether the given number iseven or not
Code:
n = int(input("Enter the number : "))
if(n%2 == 1):
print("Number is odd")
else:
print("Number is even")
Output:
9
Python Lab (6CS4-23)
Practical No.3(a)
Aim : Write a python program, using for loop that prints out the decimal
equivalents of 1/2,1/3,1/4...,1/10
Code:
for i in range(2, 11):
result = 1 / i
print("1/", i, "=", result)
Output:
10
Python Lab (6CS4-23)
Practical No.3(b)
Aim : Writeaprogramtodemonstratelistinpython
Code:
# Demonstrate List
n = int(input("Enter number of elements: "))
my_list = []
for i in range(n):
value = input("Enter element: ")
my_list.append(value)
print("The list is:", my_list)
Output:
11
Python Lab (6CS4-23)
Practical No.4(a)
Aim: Write a program to demonstrate tuple in Python.
Code:
# Demonstrate Tuple
n = int(input("Enter number of elements: "))
temp_list = []
for i in range(n):
value = input("Enter element: ")
temp_list.append(value)
my_tuple = tuple(temp_list)
print("Tuple is:", my_tuple)
Output:
12
Python Lab (6CS4-23)
Practical No.4(b)
Aim: Write a program using for loop that loops to print a table.
Code:
n = int(input("Enter a number : "))
for i in range(1,11):
print(f"{n} x {i} = {n*i}")
Output:
13
Python Lab (6CS4-23)
Practical No.4(c)
Aim: Write a programusing while loopthat asksthe user for a number and prints a
countdown from that number to zero
Code:
# Countdown program
num = int(input("Enter a number: "))
while num>= 0:
print(num)
num = num– 1
Output:
14
Python Lab (6CS4-23)
Practical No.5(a)
Aim: wirte a program to print the Fibonacci sequence
Code:
# Fibonacci sequence
n = int(input("Enter number of terms: "))
a=0
b=1
print("Fibonacci sequence:")
for i in range(n):
print(a)
c=a+b
a=b
b=c
Output:
15
Python Lab (6CS4-23)
Practical No.5(b)
Aim: WAPtofindthe sum of even valued terms
Code:
# Sum of even numbers
n = int(input("Enter how many numbers you want to enter: "))
sum_even = 0
for i in range(n):
num = int(input("Enter number: "))
if num % 2 == 0:
sum_even = sum_even + num
print("Sum of even numbers =", sum_even)
Output:
16
Python Lab (6CS4-23)
Practical No.6(a)
Aim: Write a program to count the number of characters in a string and store them in a
dictionary data structure.
Code:
text = input("Enter a string: ")
char_count = {}
for ch in text:
if ch in char_count:
char_count[ch] += 1
else:
char_count[ch] = 1
print("Character count:", char_count)
Output:
17
Python Lab (6CS4-23)
Practical No.6(b)
Aim: Write a program to use split and join methods in the string and trace a birthday of a
person with a dictionary data structure.
Code:
text = input("Enter a string: ")
char_count = {}
for ch in text:
if ch in char_count:
char_count[ch] += 1
else:
char_count[ch] = 1
print("Character count:", char_count)
Output:
18
Python Lab (6CS4-23)
Practical No.7(a)
Aim: Write a program to count frequency of characters in a given file.
Code:
filename = input("Enter file name: ")
file = open(filename, "r")
text = [Link]()
[Link]()
char_count = {}
for ch in text:
if ch in char_count:
char_count[ch] += 1
else:
char_count[ch] = 1
print("Character frequency:")
print(char_count)
Output:
19
Python Lab (6CS4-23)
Practical No.7(b)
Aim: Can you use character frequency to tell whether the given file is a Python program
file, C program file or a text file?
Code: filename = input("Enter file name: ")
file = open(filename, "r")
text = [Link]()
[Link]()
char_count = {}
# Step 1: frequency count
for ch in text:
if ch in char_count:
char_count[ch] += 1
else:
char_count[ch] = 1
print("Character Frequency:", char_count)
# Step 2: simple guessing logic
if '{' in char_count or '}' in char_count or ';' in char_count:
print("File type: C Program")
elif ':' in char_count or '#' in char_count:
print("File type: Python Program")
else:
print("File type: Text File")
Output:
20
Python Lab (6CS4-23)
Practical No.8(a)
Aim: Write a program to print each line of a file in reverse order.
Code:
filename = input("Enter file name: ")
file = open(filename, "r")
for line in file:
print([Link]()[::-1])
[Link]()
Output:
21
Python Lab (6CS4-23)
Practical No.8(b)
Aim: Write a program to compute the number of characters, words and lines in a file
Code:
filename = input("Enter file name: ")
file = open(filename, "r")
text = [Link]()
[Link]()
# Count characters
char_count = len(text)
# Count words
words = [Link]()
word_count = len(words)
# Count lines
lines = [Link]("\n")
line_count = len(lines)
print("Characters:", char_count)
print("Words:", word_count)
print("Lines:", line_count)
Output:
22
Python Lab (6CS4-23)
Practical No.9(a)
Aim: Write a function nearly equal to test whether two strings are nearly equal.
Code: def nearly_equal(str1, str2):
if sorted(str1) == sorted(str2):
return True
else:
return False
# Test
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
if nearly_equal(s1, s2):
print("Nearly Equal")
else:
print("Not Nearly Equal")
Output:
23
Python Lab (6CS4-23)
Practical No.9(b)
Aim: Write functions to compute GCD and LCM of two numbers (each function should
not exceed one line).
Code: def gcd(a, b): return a if b == 0 else gcd(b, a % b)
def lcm(a, b): return (a * b) // gcd(a, b)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD =", gcd(a, b))
print("LCM =", lcm(a, b))
Output:
24
Python Lab (6CS4-23)
Practical No.10(a)
Aim: Write a program to implement Merge Sort.
Code:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i=j=k=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
25
Python Lab (6CS4-23)
# Input
arr = list(map(int, input("Enter numbers separated by space: ").split()))
merge_sort(arr)
print("Sorted array:", arr)
Output:
26
Python Lab (6CS4-23)
Practical No.10(b)
Aim: Write a program to implement Selection Sort and Insertion Sort.
Code:
# Selection Sort
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i+1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
# Insertion Sort
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
# Input
arr = list(map(int, input("Enter numbers: ").split()))
# Copy for both sorts
arr1 = [Link]()
arr2 = [Link]()
27
Python Lab (6CS4-23)
selection_sort(arr1)
insertion_sort(arr2)
print("Selection Sort:", arr1)
print("Insertion Sort:", arr2)
Output:
28