Professional Documents
Culture Documents
print(d.keys())
print(d.values())
print(d.items())
Enter an integer7
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}
Exercise 2 : Read an integer N. With all integers from 1 to N, using list comprehension,
create a list of tuples which has the integer and its cube, iff the integer is divisible by 5 and
7. Convert this list into a dictionary.
n = int(input("Enter an integer"))
out =[(i, i**3) for i in range(1,n+1) if (i%3 == 0) and (i%7 == 0)]
print(dict(out))
Enter an integer50
{21: 9261, 42: 74088}
Exercise 3: Read an integer N. Create a dictionary where the keys are the numbers from 1
to N. Values are tuples having the square of the integer and its factorial. Sample Input : N =
4. Output : {1:(1,1), 2:(4,2), 3:(9,6), 4:(16, 24)}
n = int(input("Enter an integer"))
out={}
for i in range(1,n+1):
f = 1
for j in range(1,i+1):
f =f*j
out[i] = (i*i, f)
print(out)
Enter an integer5
{1: (1, 1), 2: (4, 2), 3: (9, 6), 4: (16, 24), 5: (25, 120)}
Exercise 4: Create a list as shown in sample input, where the list can contain single
elements, lists, tuples or dictionaries with numeric keys. Find the sum of all elements in the
list collectively. Sample Input : [1, 2, [66,44,55], (2,0,8,9), {11:'eleven'}] Output : Sum of the
list is 198
inp = eval(input("Enter a list with single elements, list, tuple and
dictionary as []"))
print("The input list is :\n", inp)
s = 0
for i in inp:
if type(i) == list or type(i) == tuple:
s = s + sum(i)
elif type(i) == dict:
s = s + sum(i.keys())
else:
s = s + i
print("The final sum is :", s)
Exercise 7:Professor Smith is the faculty advisor of 3 students. Initially he received only the
ID Nos of his advisees from Dean's office as IDNO = [20181ISE349, 20181ISE550,
20181ISE560, 20181ISE289]. After his first meeting with his advisees he collected their
CGPA = [8.9, 3.0, 5.5, 7.8] respectively.
a) Help the professor to consolidate these two lists into a dictioanry Output :
{'20181ISE349':8.9, '20181ISE550':3.0, '20181ISE560':5.5, '20181ISE289':7.8
b) Help the professor to identify the slow learners, students with CGPA of 4 and below
Output: ['20181ISE550']
c) In his next meeting, he decides to meet his advisees based on their CGPA, prioritizing
slow learners first. Preapre a visiting list for him Output : ['20181ISE550', '20181ISE560',
'20181ISE289', '20181ISE349']
import operator
IDNO = ['20181ISE349', '20181ISE550', '20181ISE560', '20181ISE289']
CGPA = [7.8, 3.0, 5.5, 8.7]
advisees = {i:j for i, j in zip(IDNO,CGPA)}
print("The advisees of Prof Smith are:\n", advisees)
Exercise 8: USing a function to find factorial, compute the binomial coefficient nCr = n!/(r! )
(n-r)!, for a given value of n and r.
def my_fact(n):
f = 1
for i in range(1,n+1):
f = f*i
return(f)
Enter a number4
Enter r2
NCR = 6.0
Exercise 9: Create a list of list with the daily average temperatures of a week as
[['sun',33.3], ['mon',32.1], ['tue',34.5], ['wed',28.7], ['thu',29.9], ['fri',28.6], ['sat',25.5]].
Using a function find the warmest and the coldest day of the week and the corresponding
temperature. Ouput : Warmest is ['tue':34.5] Coldest is ['sat':25.5]
def maxmin(weekly_temp):
out = sorted(weekly_temp, key=operator.itemgetter(1))
return(out)
import operator
weekly_temp = eval(input("Enter the daily temperature in a week as a
[[]]"))
p = maxmin(weekly_temp)
print("The coldest day is :", p[0])
print("The warmest day is :", p[len(p) - 1])