PYTHON LAB MANUAL EEE DEPT
PYTHON LAB MANUAL EEE DEPT
PYTHON LAB MANUAL EEE DEPT
INDEX
Page
TOPIC Number
Syllabus 3
Course Outcome 4
List Of Experiments
LIST OF PROGRAMS
2. Convert temperature values back and forth between Celsius (c), and Fahrenheit (f). 6
3. Simple desktop calculator using Python. Only the five basic arithmetic operators.. 7
4. Create, concatenate, and print a string and access a sub-string from a given string 9
12. Recursive function to find the greatest common divisor of two positive numbers 20
A program that accepts the lengths of three sides of a triangle as inputs. The program
should output whether or not the triangle is a right triangle (Recall from the
13. Pythagorean Theorem that in a right triangle, the square of one side equals the sum 21
of the squares of the other two sides). Implement using functions.
Program to define a module to find Fibonacci Numbers and import the module to
14. 22
another program.
Program to check whether the given number is a valid mobile number or not using
15. 23
functions.
1
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
2
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
SYLLABUS
LAB EXPERIMENTS:
6. Write a program to create, append, and remove lists in Python using numPy.
7. Programs to find the largest of three numbers.
8. Convert temperatures to and from Celsius, and Fahrenheit. [Formula: c/5 = f-
32/9]
9. Program to construct the stars (*) pattern, using a nested for loop
10. Program that prints prime numbers less than 20.
11. Program to find the factorial of a number using Recursion.
12. Recursive function to add two positive numbers.
13. Recursive function to multiply two positive numbers
14. Recursive function to the greatest common divisor of two positive numbers.
15. Program that accepts the lengths of three sides of a triangle as inputs. The
program output should indicate whether or not the triangle is a right triangle
(Recall from the Pythagorean Theorem that in a right triangle, the square of
one side equals the sum of the squares of the other two sides).
Implement using functions.
16. Program to define a module to find Fibonacci Numbers and import the module
to another program.
17. Program to define a module and import a specific function in that module to
another program.
18. Program to check whether the given number is a valid mobile number or not
using functions?
3
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
COURSE OUTCOMES
Course Objectives:
Bloom’s
Course Outcome Knowledge
Level (KL)
Utilize computing as a model for solving real-world problems.
CO1
K2
Articulate a problem before attempting to solve it and prepare a
CO2
clear and accurate model to represent the problem. K3
Note: K1- Remember, K2- Understand, K3- Apply, K4- Analyse, K5- Evaluate, K6-
Create
PO PO PO PO PO PO PO PO PO PO PO PO
1 2 3 4 5 6 7 8 9 10 11 12
CO 1 3 3 2 3
CO 2 3 3 3
3
CO 3 3 3 3 3
CO 4 3 3 3 3
4
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-1
Program to find the largest of three numbers..
AIM
To find the largest of three numbers.
PROGRAM
# Input three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
5
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-2
Convert temperature values back and forth between Celsius (C), and
Fahrenheit (F)
AIM
To Convert temperature values back and forth between Celsius (C), and Fahrenheit (F).
PROGRAM
print("\nTemperature Conversion:")
print("1. Convert Celsius to Fahrenheit")
print("2. Convert Fahrenheit to Celsius")
choice = input("Enter your choice (1/2): ")
if choice == '1':
# Convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")
else:
print("Invalid choice. Please enter 1 or 2")
RESULT
The program for temperature conversion is obtained.
6
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-3
Simple desktop calculator using Python. Only the five basic arithmetic
operators.
AIM
PROGRAM
METHOD 1
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
if choice == '1':
result = num1 + num2
print(f"{num1} + {num2} = {result}")
elif choice == '2':
result = num1 - num2
print(f"{num1} - {num2} = {result}")
elif choice == '3':
result = num1 * num2
print(f"{num1} * {num2} = {result}")
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(f"{num1} / {num2} = {result}")
else:
print("Error! Division by zero.")
elif choice == '5':
result = num1 % num2
print(f"{num1} % {num2} = {result}")
else:
7
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
METHOD 2
(Using match-case)
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
match choice:
case '1':
result = num1 + num2
print(f"{num1} + {num2} = {result}")
case '2':
result = num1 - num2
print(f"{num1} - {num2} = {result}")
case '3':
result = num1 * num2
print(f"{num1} * {num2} = {result}")
case '4':
if num2 != 0:
result = num1 / num2
print(f"{num1} / {num2} = {result}")
else:
print("Error! Division by zero.")
case '5':
result = num1 % num2
print(f"{num1} % {num2} = {result}")
else:
print("Invalid input. Please select a valid operation.")
RESULT
Implemented a desktop calculator using python with 5 basic operations
8
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-4
Create, concatenate, and print a string and access a sub-string from a given
string.
AIM
To Create, concatenate, and print a string and access a sub-string from a given string.
PROGRAM
RESULT
Various operations on string has been excecuted
9
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG -5
AIM:
To Familiarize time and date in various formats
PROGRAM:
RESULT:
10
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG :6
Write a program to create, append, and remove elements in lists ,using numpy also.
PROGRAM
# 1. Creating a List
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
RESULT.
The program to create, append, and remove elements in lists has been
obtained.
13
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-7
Program to construct patterns of stars (*), using a nested for loop.
AIM
Write a Program to construct patterns of stars (*), using a nested for loop..
PROGRAM
1.Square Pattern:
*****
*****
*****
*****
*****
3.Pyramid Pattern:
*
***
*****
*******
*********
# 1. Square Pattern
size = 5
print("Square Pattern:")
for i in range(size):
14
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
for j in range(size):
print('*', end=' ')
print()
# 3. Pyramid Pattern
print("Pyramid Pattern:")
for i in range(size):
# Print leading spaces
for j in range(size - i - 1):
print(' ', end=' ')
# Print stars
for k in range(2 * i + 1):
print('*', end=' ')
print()
RESULT
The various patterns have been obtained.
15
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-8
A program that prints prime numbers less than N.
AIM
Write a Program to that prints prime numbers less than N
PROGRAM
n = int(input("Enter a range: "))
16
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-9
Program to find the factorial of a number using Recursion.
AIM
Program to find the factorial of a number using Recursion.
PROGRAM
# Recursive function to calculate factorial
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: n * factorial of (n - 1)
return n * factorial(n - 1)
# Calculate factorial
result = factorial(number)
17
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-10
Recursive function to add two positive numbers.
AIM
Recursive function to add two positive numbers.
PROGRAM
# Recursive function to add two positive numbers
def add_positive_numbers(a, b):
# Base case: if one of the numbers is zero
if b == 0:
return a
# Recursive case: increment a and decrement b
return add_positive_numbers(a + 1, b - 1)
18
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-11
Recursive function to multiply two positive numbers..
AIM
Recursive function to multiply two positive numbers.
PROGRAM
# Recursive function to multiply two numbers
def multiply(a, b):
# Base case: if one of the numbers is zero
if b == 0:
return 0
# Recursive case: multiply by adding a to the result of multiplying a with (b - 1)
return a + multiply(a, b - 1)
19
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-12
Recursive function to find the greatest common divisor of two positive
numbers.(Euclid's Algorithm)
AIM
Write a program using Recursive function to find the greatest common divisor of two positive
numbers.(Euclid's Algorithm)
PROGRAM
# Recursive function to find GCD using Euclid's algorithm
def gcd(a, b):
# Base case: if b is 0, gcd is a
if b == 0:
return a
# Recursive case: call gcd with b and a % b
return gcd(b, a % b)
20
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-13
A program to check whether a triangle is right angled or not.
AIM
A program that accepts the lengths of three sides of a triangle as inputs. The program should
output whether or not the triangle is a right triangle (Recall from the Pythagorean Theorem
that in a right triangle, the square of one side equals the sum of the squares of the other two
sides). Implement using functions.
PROGRAM
# Function to check if the given sides form a right triangle
def is_right_triangle(a, b, c):
# Sort the sides to ensure that c is the largest side
sides = sorted([a, b, c])
# Apply the Pythagorean Theorem
return sides[2] ** 2 == sides[0] ** 2 + sides[1] ** 2
21
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-14
Program to define a module to find Fibonacci Numbers and import the
module to another program.
AIM
Program to define a module to find Fibonacci Numbers and import the module to another
program.
PROGRAM
fib.py # module
# Function to return the nth Fibonacci number
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
###############
import fib
for i in range(n):
print(fib.fibonacci(i), end=" ")
RESULT
The Program to define a module and import the module to another program is obtained.
22
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-15
Program to check whether the given number is a valid mobile number or
not using functions.
AIM
Program to check whether the given number is a valid mobile number or not using functions.
Rules:
1. Every number should contain exactly 10 digits.
2. The first digit should be 7 or 8 or 9
PROGRAM
# Function to check if the given number is a valid mobile number
def is_valid_mobile_number(number):
# Check if the number contains exactly 10 digits
if len(number) == 10 and number.isdigit():
# Check if the first digit is 7, 8, or 9
if number[0] in ['7', '8', '9']:
return True
return False
23
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-16
Program to Merge and sort the Lists
AIM
Input two lists from the user. Merge these lists into a third list such that in the merged list, all
even numbers occur first followed by odd numbers. Both the even numbers and odd numbers
should be in sorted order.
PROGRAM
# Input: Two lists from the user
list1 = list(map(int, input("Enter the elements of the first list separated by spaces: ").split()))
list2 = list(map(int, input("Enter the elements of the second list separated by spaces:
").split()))
24
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-17
A program to play stick games.
AIM
Write a program to play a sticks game in which there are 16 sticks. Two players take turns to
play the game. Each player picks one set of sticks (needn’t be adjacent) during his turn. A set
contains 1, 2, or 3 sticks. The player who takes the last stick is the loser. The number of sticks
in the set is to be input.
PROGRAM
# Initial number of sticks
total_sticks = 16
# Game loop
while total_sticks > 0:
# Player 1's turn
print(f"There are {total_sticks} sticks left.")
player1_choice = int(input("Player 1, pick 1, 2, or 3 sticks: "))
25
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
total_sticks -= player2_choice
26
GOVERNMENT MODEL ENGINEERING COLLEGE
UCEST 105-PYTHON LAB
PROG-18
Monty Hall problem
AIM
Suppose you're on a game show, and you are given the choice of three doors: Behind one
door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows
what is behind the doors, opens another door, say No. 3, which has a goat. He then asks, "Do
you want to pick door No. 2?"Is it to your advantage to switch your choice?
PROGRAM
# Function to simulate the Monty Hall problem
def monty_hall_simulation(num_trials):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
# Randomly place the car behind one of the three doors
doors = [0, 0, 0]
car_position = random.randint(0, 2)
doors[car_position] = 1
28
GOVERNMENT MODEL ENGINEERING COLLEGE