PYTHON (1) Set 1 Prasad
PYTHON (1) Set 1 Prasad
INDEX
PAGE DATE OF DATE OF
S. PROGRAM NO PERFORMANCE SUBMISSION SIGNATURE MARKS
NAME
NO
1 Set 1
1. A program
that converts
temperatures
from Fahrenheit
to Celsius and
vice versa.
2. A program
that calculates
the area and
perimeter of a
rectangle.
3. A program
that generates a
random
password of a
specified
length.
4. A program
that calculates
the average of a
list of numbers.
5. A program
that checks if a
given year is a
leap year.
6. A program
that calculates
the factorial of
a number.
7. A program
that checks if
a given string is
a palindrome.
8. A program
that sorts a list
of numbers in
ascending or
descending
order.
9. A program
that generates a
multiplication
table for a given
number.
10. A program
that converts a
given number
from one base
to another.
2 Set 2
1. A program
that models a
bank account,
with classes for
the account, the
customer, and
the bank.
2. A program
that simulates a
school
management
system, with
classes for the
students, the
teachers, and
the courses.
SET-01
PRACTIACAL 01
AIM: A program that converts temperatures from Fahrenheit to Celsius and vice
versa.
Program:
celsius=int(input("enter the temperature in celsius: \n"))
fahrenheit=int(input("enter the temperature in fahrenheit: \n"))
celsius_result=(fahrenheit-32)*5/9
print(f"{fahrenheit}F is equal to {celsius_result}C")
fahrenheit_result=(celsius*9/5)+32
print(f"{celsius}C is equal to {fahrenheit_result}F")
Output:
PRACTICAL 02
AIM: A program that calculates the area and perimeter of a rectangle.
Program:
length=int(input("Enter the length of rectangle: \n"))
breadth=int(input("Enter the breadth of rectangle: \n"))
Area=(length*breadth)
print("Area of rectangle:",Area)
Perimeter=2*(length+breadth)
print("Perimeter of rectangle:",Perimeter)
Output:
PRACTICAL 03
AIM: A program that generates a random password of a specified length.
Program:
import random
import string
password_length=12
characters = string.ascii_letters+string.digits +string.punctuation
password=''.join(random.choice(characters) for i in range(password_length))
print(f"Your generated password is:{password}")
Output:
PRACTICAL 04
AIM: A program that calculates the average of a list of numbers.
Program:
num=[1,2,3,4,5,6]
sum=0
for i in num:
sum=sum+i
average=sum/len(num)
print(f"The average of numbers is {average}")
Output:
PRACTICAL 05
AIM: A program that checks if a given year is a leap year.
Program:
year=int(input("Enter a year: \n"))
if (year%4==0 and year%100!=0)or (year%400==0):
print("leap year")
else:
print("Not a leap year")
Output:
PRACTICAL 06
AIM: A program that calculates the factorial of a number.
Program:
def factorial(n):
if n==0 or n==1:
return 1
else:
return n*factorial(n-1)
num=int(input("Enter the number: \n"))
result=factorial(num)
print(f"The factorial of a {num} is: {result}")
Output:
PRACTIACAL 07
AIM: A program that checks if a given string is a palindrome.
Program:
#using method
def is_palindrome (s):
s=''.join(s.split()).lower()
return s==s[::-1]
input_string=input("Enter a string: \n")
if is_palindrome(input_string):
print(f"{input_string} is a palindrome.")
else:
print(f"{input_string}is not a palindrome.")
#Using for loop
input_string=input_string.lower()
rev=""
for i in range(len(input_string)-1,-1,-1):
rev+=input_string[i]
if input_string==rev:
print(f"{input_string} is a palindrome.")
else:
print(f"{input_string}is not a palindrome.")
#or
input_string=input_string.lower()
rev=""
for i in input_string:
rev=i+rev
if input_string==rev:
print(f"{input_string} is a palindrome.")
else:
print(f"{input_string} is not a palindrome.")
Output:
PRACTICAL 08
AIM: A program that sorts a list of numbers in ascending or descending order.
Program:
numlist=[3,2,5,1,6]
ascending_sorted=sorted(numlist)
descending_sorted=sorted(numlist,reverse=True)
print("Original list:",numlist)
print("ascending list:",ascending_sorted)
print("descending list:",descending_sorted)
Output:
PRACTICAL 09
AIM: A program that generates a multiplication table for a given number.
Program:
num=int(input("Enter the number: \n"))
for i in range(1,11,1):
result=(num*i)
print(f"{num}*{i}={result}")
Output:
PRACTICAL 10
AIM: A program that converts a given number from one base to another.
Program:
def to_base10(number,base):
return int(number,base)
def from_base10(number,base):
digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if number==0:
return "0"
result=""
while number>0:
result=digits[number%base]+result
number=number//base
return result
number=input("Enter the number: \n")
source_base=int(input("Enter the source base: \n"))
target_base=int(input("Enter the target base: \n"))
base10_number=to_base10(number,source_base)
converted_number=from_base10(base10_number,target_base)
print(f"{number} in base {converted_number} in base {target_base}.")
Output:
SET-02
PRACTICAL 1
AIM: A program that models a bank account, with classes for the account, the
customer, and the bank.
Program:
class Account:
def init (self, account_number, balance=0.0):
self.account_number = account_number
self.balance = balance
def get_balance(self):
return self.balance
class Customer:
def init (self, name, customer_id):
self.name = name
self.customer_id = customer_id
self.accounts = []
def get_accounts(self):
return self.accounts
class Bank:
def init (self, name):
self.name = name
self.customers = []
customer1 = Customer("Ram", 1)
customer2 = Customer("Sita", 2)
bank.add_customer(customer1)
bank.add_customer(customer2)
customer1.add_account(account1)
customer2.add_account(account2)
account1.deposit(200)
account1.withdraw(100)
print(f"Balance for {customer1.name}: {account1.get_balance()}")
account2.deposit(500)
account2.withdraw(300)
print(f"Balance for {customer2.name}: {account2.get_balance()}")
print(customer1)
print(customer2)
print(bank)
Output: