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

AI Lab 1

The document contains 8 questions related to Python programming concepts like lists, dictionaries, classes, functions etc. Multiple operations like appending, removing, sorting, retrieving values are performed on lists and dictionaries. Random numbers are generated and filtered based on conditions. A Restaurant class is defined with methods to describe and open a restaurant. A User class is also defined with methods to describe and greet a user.

Uploaded by

21103053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

AI Lab 1

The document contains 8 questions related to Python programming concepts like lists, dictionaries, classes, functions etc. Multiple operations like appending, removing, sorting, retrieving values are performed on lists and dictionaries. Random numbers are generated and filtered based on conditions. A Restaurant class is defined with methods to describe and open a restaurant. A User class is also defined with methods to describe and greet a user.

Uploaded by

21103053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1)

n = 3
maths = []
science = []
english = []
it = []
marks = []

for i in range(n):
m = float(input(f"Enter marks obtained in maths by student {i +
1}: "))
s = float(input(f"Enter marks obtained in science by student {i +
1}: "))
e = float(input(f"Enter marks obtained in english by student {i +
1}: "))
_i = float(input(f"Enter marks obtained in IT by student {i + 1}:
"))
maths.append(m)
science.append(s)
english.append(e)
it.append(_i)

marks.extend(maths)
marks.extend(science)
marks.extend(english)
marks.extend(it)

print("\nHighest marks per subject")


print("Maths:", max(maths))
print("Science:", max(science))
print("English:", max(english))
print("IT:", max(it))

print("\nLowest marks per subject")


print("Maths:", min(maths))
print("Science:", min(science))
print("English:", min(english))
print("IT:", min(it))

print("\nAverage marks per subject")


print("Maths:", sum(maths) / n)
print("Science:", sum(science) / n)
print("English:", sum(english) / n)
print("IT:", sum(it) / n)
print(f"\nHighest marks overall: {max(marks)}")
print(f"\nLowest marks overall: {min(marks)}")
print(f"\nAverage marks overall: {sum(marks) / n}")

Q2)

salary = int(input("Enter basic salary: "))


hra, da = 0, 0
if salary <= 10_000:
hra = 0.2 * salary
da = 0.8 * salary
elif salary > 10_000 and salary <= 20_000:
hra = 0.25 * salary
da = 0.9 * salary
else:
hra = 0.3 * salary
da = 0.95 * salary

print(f"Total salary is: {salary + hra + da}")

Q3)

password = input("Enter password: ")


lc = [c for c in password if c.islower()]
uc = [c for c in password if c.isupper()]
d = [c for c in password if c.isdigit()]
ch = [c for c in password if c in "$#@"]
if not lc or not uc or not d or not ch or len(password) < 6 or
len(password) > 16:
print("Not a valid password")
else:
print("Valid password")

Q4)

l = [10, 20, 30, 40, 50, 60, 70, 80]

#1
l.append(200)
l.append(300)
print(l)
#2
l.remove(10)
l.remove(30)
print(l)

#3
print(sorted(l))

#4
print(sorted(l, reverse=True))

Q5)

d = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five"
}

#1
d[6] = "six"

#2
del d[2]

#3
e = d.get(6, False)
if not e:
print("Key does not exist in dictionary")
else:
print("Key exists in dictionary")

#4
print(len(d))

#5
print(sum([i for i in d]))
Q6)

import math
import random

l = []
for i in range(100):
l.append(random.randrange(100, 901))

def isprime(n):
for i in range(2, math.ceil(math.sqrt(n))):
if n % i == 0:
return False
return True

print("Even numbers:", [i for i in l if i % 2 == 0])


print("\nOdd numbers:", [i for i in l if i % 2 != 0])
print("\nPrime numbers:", [i for i in l if isprime(i)])

Q7)

def compound_interest(p, r, t):


ci = (p * ((1 + (r / 100)) ** t)) - p
return ci

if __name__ == "__main__":
x = compound_interest(50_000, 10, 5)
print(x)

Q8)

(a)

class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type

def describe_restaurant(self):
print(f"Restaurant Name: {self.restaurant_name}")
print(f"Cuisine Type: {self.cuisine_type}")

def open_restaurant(self):
print("Restaurant is open")

obj = Restaurant("Sagar Ratnam", "South Indian")


obj.describe_restaurant()
obj.open_restaurant()

(b)

class User:
def __init__(self, first_name, last_name, dob, number):
self.first_name = first_name
self.last_name = last_name
self.dob = dob
self.number = number

def describe_user(self):
print(f"Name: {self.first_name} {self.last_name}")
print(f"Date of Birth: {self.dob}")
print(f"Mobile Number: {self.number}")

def greet_user(self):
print(f"Hello, {self.first_name}. Have good day!")

You might also like