Professional Documents
Culture Documents
Assignment 1 (102103375 Sunali)
Assignment 1 (102103375 Sunali)
Lab Assignment-1
Ques 1 A class with 10 students wants to produce some information from the results of the four
standard tests in Maths, Science, English and IT. Each test is out of 100 marks. The information
output should be the highest, lowest and average mark for each test and the highest, lowest
and average mark overall. Write a program in Python to complete this task. SOLUTION:-
CODE:-
def calculate_statistics(scores):
highest = max(scores)
lowest = min(scores)
def calculate_class_statistics(test_scores):
maths_stats = calculate_statistics(test_scores['Maths'])
science_stats = calculate_statistics(test_scores['Science'])
english_stats = calculate_statistics(test_scores['English'])
it_stats = calculate_statistics(test_scores['IT'])
overall_stats = calculate_statistics(all_scores)
test_scores = {
'Maths': [80, 90, 70, 95, 85, 75, 60, 92, 88, 79],
'Science': [70, 85, 90, 78, 82, 93, 65, 88, 95, 76],
'English': [75, 88, 92, 68, 80, 85, 90, 79, 83, 77],
'IT': [88, 92, 70, 80, 85, 90, 75, 83, 79, 72]
calculate_class_statistics(test_scores)
OUTPUT:
QUES 2 Write a Python Program to input basic salary of an employee and calculate its Gross salary
according to following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 :
HRA = 25%, DA = 90% Basic Salary > 20000 : HRA = 30%, DA = 95%. SOLUTION:-
CODE:-
def calculate_gross_salary(basic_salary):
da = 0.8 * basic_salary
da = 0.9 * basic_salary
else:
da = 0.95 * basic_salary
gross_salary = calculate_gross_salary(basic_salary)
OUTPUT:-
by users. Validation:
SOLUTION:-
CODE:-
import re
def validate_password(password):
# Check length
if len(password) < 6 or len(password) > 16:
return False
# Check for lowercase letter
return False
return False
return False
return False
return True
valid = validate_password(password)
if valid:
print("Password is valid.")
else:
print("Password is invalid.")
OUTPUT:
QUES 4 Create a List L that is defined as= [10, 20, 30, 40, 50, 60, 70, 80].
SOLUTION:-
CODE:-
print("Initial List:", L)
L.append(200)
L.append(300)
L.remove(10)
L.remove(30)
L.sort()
L.sort(reverse=True)
“Four”, 5:”Five”}. (i) WAP to add new entry in D; key=6 and value is “Six”
SOLUTION:-
CODE:-
print("Initial Dictionary:", D)
D[6] = "Six"
del D[2]
else:
print("(iii) Key=6 is not present in the dictionary.")
num_elements = len(D)
values_sum = sum(D.values())
OUTPUT:-
QUES 6 WAP to create a list of 100 random numbers between 100 and 900. Count and
SOLUTION
CODE:-
import random
# Initialize lists for odd numbers, even numbers, and prime numbers
odd_numbers = []
even_numbers = []
prime_numbers = []
# Iterate through the random_numbers
if number % 2 != 0:
odd_numbers.append(number)
elif number % 2 == 0:
even_numbers.append(number)
is_prime = True
if number > 1:
number % i == 0:
is_prime = False
break
else:
is_prime = False
if is_prime:
prime_numbers.append(number)
OUTPUT:-
QUES 7 (i) Write a function which takes principal amount, interest rate and time. This
function returns compound interest. Call this function to print the output.
SOLUTION:-
CODE:-
return compound_interest
# Example usage
principal_amount = 1000
interest_rate = 0.05
time_period = 3
(ii) Save this function (as a module) in a python file and call it in another python
# Example usage
principal_amount = 1000
interest_rate = 0.05
time_period = 3
OUTPUT:-
QUES 8 A) Make a class called Restaurant. The __init__() method for Restaurant should store
describe_restaurant() that prints these two pieces of information, and a method called
open_restaurant() that prints a message indicating that the restaurant is open. Make an instance
called restaurant from your class. Print the two attributes individually, and then call both
methods. SOLUTION:-
CODE:-
class Restaurant:
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
def open_restaurant(self):
print("The restaurant is open.")
restaurant.describe_restaurant()
restaurant.open_restaurant()
OUTPUT:-
B) Make a class called User. Create two attributes called first_name and last_name, and then
create several other attributes that are typically stored in a user profile. Make a method
called describe_user() that prints a summary of the user’s information. Make another method
called greet_user() that prints a personalized greeting to the user. Create several instances
representing different users, and call both method for each user.
CODE:-
class User:
self.first_name = first_name
self.last_name = last_name
self.age = age
self.location = location
self.occupation = occupation
def describe_user(self):
print("User Information:")
print("Age:", self.age)
print("Location:", self.location)
print("Occupation:", self.occupation)
def greet_user(self):
user user1.describe_user()
user1.greet_user()
print()
user2.describe_user()
user2.greet_user()
print()
user3.describe_user()
user3.greet_user()
OUTPUT:-