0% found this document useful (0 votes)
20 views

Python File

The document contains the details of 20 programming experiments done by Bhrigu Soni as part of his Programming with Python course. Each experiment has the aim, code, and output for programs to find the area and circumference of a circle, check if a number is even or odd, find the largest of three numbers, print the table of a number, print prime numbers between ranges, and more.

Uploaded by

Mayank Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python File

The document contains the details of 20 programming experiments done by Bhrigu Soni as part of his Programming with Python course. Each experiment has the aim, code, and output for programs to find the area and circumference of a circle, check if a number is even or odd, find the largest of three numbers, print the table of a number, print prime numbers between ranges, and more.

Uploaded by

Mayank Saini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Name – Bhrigu Soni Class - B.

Tech, III Year Branch - AI & DS Sem – V


Subject – PROGRAMMING WITH PYTHON (AI 351)

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

17 Write a program to mail merge. 19-20 18-09-2023

18 Write a program to find the size 21-21 20-09-2023


(Resolution) of an image.
19 Write a program to catch multiple 22-22 25-09-2023
exception as a Parenthesized Tuple
(in one line).
20 Write a program to split a list into 23-23 27-09-2023
evenly sized chunks.
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)

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

26 Write a program to display Inheritance. 30-31 30-10-2023


Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)

Experiment - 1
Aim - Write a program to find the area and circumference of a circle.

Code –

r = int(input("Enter the value of radius:"))


a = float(3.14*r*r)
print("Area of the circle:", a)
b = float(2*3.14*r)
print("Circumference of circle:", b)

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 –

num = int(input("Enter the number:"))


if (num % 2 == 0):
print("Number is even")
else:
print("Number is odd")

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

def subtract(x, y):


return x-y

def multiply(x, y):


return x*y

def divide(x, y):


return x/y

print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:

choice = input("Enter the choice:")

if choice in ('1', '2', '3', '4'):


num1 = float(input("Enter the first number:"))
num2 = float(input("Enter the second number:"))

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")

next_calculation = input("Let's do the next calcultion?(yes/no):")


Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)

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:"))

result = list(map(lambda x: 2**x, range(terms)))

print("The total number fo terms are-", terms)


for i in range(terms):
print("2 raised to power ", i, "is", result[i])

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))

nterms = int(input("Enter the number of terms:"))

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):

for row in matrix:

print(*row)

rows = int(input("Enter the number of rows: "))


columns = int(input("Enter the number of columns: "))

matrix = []

print("Enter the elements of the matrix row wise (a b c):")

for i in range(rows):

row = list(map(int, input().split()))

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)

num = int(input("Enter the Nnumber:"))


if num < 0:
print("Enter a positive number:")
else:
print("The sum is", recur_sum(num))

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):

for recipient in 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 = [

{"name": "Alice", "balance": 500},

{"name": "Bob", "balance": 1000},

{"name": "Charlie", "balance": 750}

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)

width = (a[0] << 8)+a[1]

print("The resolution of the image is", width, "x", height)

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)

except (TypeError, ValueError) as e:


print("An error occurred:", e)

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]

chunk_size = int(input("Enter the Chunk Size: "))


my_list = []
n = int(input("Enter the number of elements in the List: "))

for i in range(0, n):


element = int(input("Enter the elements: "))
my_list.append(element)

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_path = input("Enter the file path: ")

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}

# Input: Taking user input


user_value = float(input("Enter a number: "))

# Calculating values and receiving multiple results


result_tuple, result_dict = calculate_values(user_value)

# Displaying the results


print(f"Using Tuple: {result_tuple}")
print(f"Using Dictionary: {result_dict}")

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 __init__(self, make, model, year):

self.__make = make # Encapsulated property

self.__model = model # Encapsulated property

self.__year = year # Encapsulated property

def display_car_info(self):

print(f"Car Details: {self.__year} {self.__make} {self.__model}")

def set_make(self, make):

self.__make = make

def get_make(self):

return self.__make

# Creating an object of the Car class


my_car = Car("Toyota", "Corolla", 2020)

# Accessing encapsulated properties through methods

my_car.set_make("Honda")

print("Updated Make:", my_car.get_make())

# Accessing encapsulated properties directly.

# This line will throw an error due to private access

#print("Car Make:", my_car.__make)


Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)

# Accessing encapsulated properties through a public method

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")

# Function to demonstrate polymorphism


def make_sound(animal):
animal.sound()

# Creating instances of different classes


dog = Dog()
cat = Cat()
bird = Bird()

# Calling the same function with different objects


make_sound(dog)
make_sound(cat)
make_sound(bird)

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")

# Creating instances of classes using inheritance


vehicle = Vehicle("Generic", 2000)
vehicle.display_info()

car = Car("Toyota", 2015, "Corolla")


car.display_car_info()

electric_car = ElectricCar("Tesla", 2022, "Model S", 75)


electric_car.display_electric_car_info()
Name – Bhrigu Soni Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – PROGRAMMING WITH PYTHON (AI 351)

Output –

You might also like