Python File
Python File
Index
Date of Sign/
Sr. Page
Topic
No No. Remarks
Done Checked
1. Write a program to find the area and 1-1 07-08-2023
circumference of a circle.
2 Write a Program to check number is even 2-2 07-08-2023
or odd.
3 Write a program to find the largest 3-3 09-08-2023
among three numbers.
4 Write a program to print the table of a 4-4 09-08-2023
number.
5 Write a program to print prime number 5-5 16-08-2023
between 900 to 1000.
6 Write a program to print right angled 6-6 16-08-2023
‘#’ pattern.
7 Write a program to display the Fibonacci 7-7 21-08-2023
sequence upto nth term.
8 Write a program to check Armstrong 8-8 21-08-2023
number in a certain interval.
9 Write a program to find that the number 9-9 23-08-2023
is positive or negative using ladder if-
else.
10 Write a program to find that the number 10-10 23-08-2023
is positive or negative using nested if-
else.
11 Write a program to make a simple 11-12 28-08-2023
calculator.
12 Write a program to display the powers of 13-13 28-08-2023
‘2’ using anonymous function.
13 Write a program to display the Fibonacci 14-14 04-09-2023
sequence using Recursion.
14 Write a program to transpose a matrix 15-16 06-09-2023
using a nested loop.
15 Write a program to find the sum of a 17-17 11-09-2023
natural number using Recursion.
16 Write a program to import a module. 18-18 13-09-2023
Index
Date of Sign/
Sr. Page
Topic
No No. Remarks
Done Checked
21 Write a program to find hash of a file 24-24 09-10-2023
and display it.
22 Write a program to return multiple 25-25 11-10-2023
values from a function.
23 Write a program for catching Exceptions 26-26 16-10-2023
in Python.
24 Write a program to display 27-28 18-10-2023
Encapsulation.
25 Write a program to display Polymorphism. 29-29 25-10-2023
Experiment - 1
Aim - Write a program to find the area and circumference of a circle.
Code –
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 2
Aim - Write a Program to check number is even or odd.
Code –
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 3
Aim - Write a program to find the largest among three numbers.
Code –
a = int(input("Enter the first number:"))
b = int(input("Enter the second number:"))
c = int(input("Enter the third number:"))
if (a > b and a > c):
print("The greatest number is:", a)
elif (b > a and b > c):
print("The greatest number is:", b)
elif (c > a and c > b):
print("The greatest number is:", c)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 4
Aim - Write a program to print the table of a number.
Code –
num = int(input("Enter the Number-"))
print("Table of:", num)
for i in range(0, 11):
print(num, "*", i, "=", num*i)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 5
Aim - Write a program to print prime number between 900 to 1000.
Code –
lower = 900
upper = 1000
print("prime numbers between", lower, " & ", upper)
for i in range(lower, upper+1):
for n in range(2, i):
if (i % n == 0):
break
else:
print(i)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 6
Aim - Write a program to print right angled ‘#’ pattern.
Code –
for i in range(1, 10):
for j in range(1, i+1):
print("#", end="")
print()
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 7
Aim - Write a program to display the Fibonacci sequence upto nth term.
Code –
num = int(input("Enter the number:"))
n1, n2 = 0, 1
c = 0
if num <= 0:
print("please enter the valid number")
elif num == 1:
print("fibonacci series upto", num, " term ")
print(n1)
else:
print("fibonacci series upto", num, " terms ")
while c < num:
print(n1)
nth = n1+n2
n1 = n2
n2 = nth
c += 1
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 8
Aim - Write a program to check Armstrong number in a certain interval.
Code –
a = int(input("Enter the lower limit: "))
b = int(input("Enter the upper limit: "))
print("armstrong numbers between", a, "and", b, "are")
for num in range(a, b):
order = len(str(num))
sum = 0
temp = num
while (temp > 0):
digit = temp % 10
sum += digit**order
temp //= 10
if (num == sum):
print(num)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 9
Aim - Write a program to find that the number is positive or negative
using ladder if-else.
Code –
num = float(input("Enter the number-"))
if num > 0:
print(num, "is a positive number")
elif num == 0:
print(num, "is a zero interger")
else:
print(num, "is a negative number")
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 10
Aim - Write a program to find that the number is positive or negative
using nested if-else.
Code –
num = float(input("Enter the number:"))
if num >= 0:
if num == 0:
print(num, "is a zero integer")
else:
print(num, "is a positive integer")
else:
print(num, "is a negative integer")
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 11
Aim - Write a program to make a simple calculator.
Code –
def add(x, y):
return x+y
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("invalid choice")
if next_calculation.lower() == "no":
break
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 12
Aim - Write a program to display the powers of 2 using anonymous
function.
Code –
terms = int(input("Number of terms:"))
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 13
Aim - Write a program to display the Fibonacci sequence using
Recursion.
Code –
def recur_fibo(n):
if n <= 1:
return n
else:
return (recur_fibo(n-1)+recur_fibo(n-2))
if nterms <= 0:
print("invalid!! please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 14
Aim - Write a program to transpose a matrix using a nested loop.
Code –
def display_matrix(matrix):
print(*row)
matrix = []
for i in range(rows):
matrix.append(row)
print("Original Matrix:")
display_matrix(matrix)
transposed_matrix = []
for i in range(columns):
transposed_row = []
for j in range(rows):
transposed_row.append(matrix[j][i])
transposed_matrix.append(transposed_row)
print("\nTransposed Matrix:")
display_matrix(transposed_matrix)
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 15
Aim - Write a program to find the sum of a natural number using
Recursion.
Code –
def recur_sum(n):
if n <= 1:
return n
else:
return n+recur_sum(n-1)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 16
Aim - Write a program to import a module.
Code –
import math as m
import math
print("(importing module) The value of pi=", math.pi)
print("(importing & renaming module) The value of pi=", m.pi)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 17
Aim - Write a program to mail merge.
Code –
def mail_merge(template, recipients):
merged_text = template.format(**recipient)
print(merged_text)
email_template = "Hello {name},\n\nWe hope this message finds you well. Your
account balance is ${balance}.\n\nBest regards,\nThe AI Company"
recipient_list = [
mail_merge(email_template, recipient_list)
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 18
Aim - Write a program to find the size (Resolution) of an image.
Code –
def jpeg_res(filename):
with open(filename, 'rb') as img_file:
img_file.seek(1050)
a = img_file.read(2)
height = (a[0] << 8)+a[1]
a = img_file.read(2)
jpeg_res("test.jpg")
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 19
Aim - Write a program to catch multiple exception as a Parenthesized
Tuple (in one line).
Code –
string = input("Enter a string: ")
try:
num = int(input("Enter a number: "))
result = string + str(num)
print("Concatenated string and number:", result)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 20
Aim - Write a program to split a list into evenly sized chunks.
Code –
def split(list_a, chunk_size):
for i in range(0, len(list_a), chunk_size):
yield list_a[i:i + chunk_size]
print(list(split(my_list, chunk_size)))
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 21
Aim - Write a program to find hash of a file and display it.
Code –
def calculate_file_hash(file_path):
hash_value = 0
block_size = 65536 # 64 KB buffer size
try:
with open(file_path, 'rb') as file:
file_buffer = file.read(block_size)
while len(file_buffer) > 0:
hash_value ^= int.from_bytes(file_buffer, byteorder='big')
file_buffer = file.read(block_size)
except FileNotFoundError:
return "File not found"
return hex(hash_value)
file_hash = calculate_file_hash(file_path)
print("Hash of the file:", file_hash)
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 22
Aim - Write a program to return multiple values from a function.
Code –
def calculate_values(user_input):
a = user_input * 2
b = user_input + 5
c = user_input ** 2
return (a, b, c), {'value_1': a, 'value_2': b, 'value_3': c}
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 23
Aim - Write a program for catching Exceptions in Python.
Code –
try:
dividend = int(input("Enter the dividend: "))
divisor = int(input("Enter the divisor: "))
result = dividend / divisor
print("Result of division:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed")
except ValueError:
print("Error: Please enter a valid number")
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 24
Aim - Write a program to display Encapsulation.
Code –
class Car:
def display_car_info(self):
self.__make = make
def get_make(self):
return self.__make
my_car.set_make("Honda")
my_car.display_car_info()
Output -
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 25
Aim Write a program to display Polymorphism.
Code –
class Dog:
def sound(self):
print("Woof! Woof!")
class Cat:
def sound(self):
print("Meow")
class Bird:
def sound(self):
print("Chirp Chirp")
Output –
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)
Experiment - 26
Aim - Write a program to display Inheritance.
Code –
class Vehicle:
def __init__(self, make, year):
self.make = make
self.year = year
def display_info(self):
print(f"Vehicle Information: {self.make} - {self.year}")
class Car(Vehicle):
def __init__(self, make, year, model):
super().__init__(make, year)
self.model = model
def display_car_info(self):
print(f"Car Information: {self.make} - {self.model} - {self.year}")
class ElectricCar(Car):
def __init__(self, make, year, model, battery_capacity):
super().__init__(make, year, model)
self.battery_capacity = battery_capacity
def display_electric_car_info(self):
print(f"Electric Car Information: {self.make} - {self.model} - {self.year} -
{self.battery_capacity} kWh")
Output –