Professional Documents
Culture Documents
Lab 2 Ai
Lab 2 Ai
Semester BS (IT) – 06
Atif Jalal
02-235191-027
Exercises
Exercise 1
Write a Python program to create all possible permutations from a given collection of distinct numbers.
Let the collection be [3,6,9]
Expected Output:
Python Code:
def permute(nums):
result_perms = [[]]
for n in nums:
new_perms = []
for perm in result_perms:
for i in range(len(perm)+1):
new_perms.append(perm[:i] + [n] + perm[i:])
result_perms = new_perms
return result_perms
my_nums = [3,6,9]
print("Original Cofllection: ",my_nums)
print("Collection of distinct numbers:\n",permute(my_nums))
Output:
Exercise 2
Write a Python program to print a long text, convert the string to a list and print all the words and their
frequencies. Expected Output is given below.
Python Code:
sentence = "This is the basic python programming lab and we are trying to implement the task for calculating the
frequency of each word appeared in the string"
word_list = sentence.split()
print ("\n\nstring to list: \n",word_list)
all_freq = {}
for i in word_list:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
Exercise 3
Python Code:
@author: atifj
height=float(input("Enter Height in feet: "))
weight=float(input("Enter Weight in kg: "))
height1=height*30.48
if BMI<18.5:
print("Your BMI status is: underweight")
elif BMI>30.0:
print("Your BMI status is: Obese")
Output:
Exercise 4
Python Code:
print("Grading System")
sum = 0
list = ["AI", "EP", "TW", "MAD", "OTB", "IT_INF"]
for n in list:
obt = int(input("Marks of {}: ".format(n)))
sum = sum + obt
total = int(input("Enter total marks: "))
print("Obtained total: {}".format(sum))
avg = sum/6
if avg >= 85 and avg <= 100:
print("Grade: A")
print("GPA: 4.0")
elif avg >= 80 and avg <= 84:
print("Grade: A-")
print("GPA: 3.67")
elif avg >= 75 and avg <= 79:
print("Grade: B+")
print("GPA: 3.33")
elif avg >= 71 and avg <= 74:
print("Grade: B")
print("GPA: 3.0")
elif avg >= 68 and avg <= 70:
print("Grade: B-")
print("GPA: 2.67")
elif avg >= 64 and avg <= 67:
print("Grade: C+")
print("GPA: 2.33")
elif avg >= 60 and avg <= 63:
print("Grade: C")
print("GPA: 2.0")
elif avg >= 57 and avg <= 59:
print("Grade: C-")
print("GPA: 1.87")
elif avg >= 53 and avg <= 56:
print("Grade: D+")
print("GPA: 1.33")
elif avg >= 50 and avg <= 52:
print("Grade: D")
print("GPA: 1.0")
elif avg < 50:
print("Grade: F")
print("GPA: 0")
else:
print("Wrong input")
Output: