You are on page 1of 4

d = {'a':'android', 'b':'ball','c':'cat'}

print(d.keys())
print(d.values())
print(d.items())

dict_keys(['a', 'b', 'c'])


dict_values(['android', 'ball', 'cat'])
dict_items([('a', 'android'), ('b', 'ball'), ('c', 'cat')])

Lab 2 covers exercises on dictionaries, functions and data frames.

Excercise 1 : Given an integer n, generate a dictionary (i, i*i), where i is


an integer between 1 and n(included) bold text
n = int(input('Enter an integer'))
out = {}
for i in range(n):
out[i] = i*i
print(out)

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)

Enter a list with single elements, list, tuple and dictionary as []


[1,2,[3,4,5],(6,7),{10:'ten'}]
The input list is :
[1, 2, [3, 4, 5], (6, 7), {10: 'ten'}]
The final sum is : 38

Exercise 5: Create a dictionary having each day and its temperature in


a week. Program should then create a list of days when the
temperature is between 32 to 35.
Sample Input : {'sun':25, 'mon':34, 'tue':33, 'wed':29, 'thu':28, 'fri':35, 'sat':40}** Output :
['mon', 'tue', 'fri']
temp = eval(input("Enter the day name and temp of a week as a {}"))
out_days = []
for i in temp:
if temp[i] >=32 and temp[i]<= 35:
out_days.append(i)
print(out_days)

Enter the day name and temp of a week as a {}{'mon':23,'tue':45}


[]

Exercise 6: Given cities = ['Delhi', 'Mumbai', 'Chennai', 'Bangalore'] and pollution_level =


[56, 45, 40, 44], create a dictionary using cities as keys and pollution_level as values and
dictionary comprehension. Output : {'Delhi':56, 'Mumbai':45, 'Chennai':40, 'Bangalore':44}
cities = ['Delhi', 'Mumbai', 'Chennai', 'Bangalore', 'hyderabad']
pollution_level = [56, 45, 40, 44,50]
out = {i:j for(i,j) in zip(cities,pollution_level)}
print(out)
{'Delhi': 56, 'Mumbai': 45, 'Chennai': 40, 'Bangalore': 44,
'hyderabad': 50}

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)

#print slow learners


slow_learners = []
for i in advisees:
if advisees[i] <=4:
slow_learners.append(i)
print("The list of slow learners are:\n", slow_learners)

#Visiting schedule for Prof Smith

order = sorted(advisees.items(), key = operator.itemgetter(1))


#output will be a list of tuples
print("The meeting schedule for Prof Smith is :\n",
dict(order).keys())

The advisees of Prof Smith are:


{'20181ISE349': 7.8, '20181ISE550': 3.0, '20181ISE560': 5.5,
'20181ISE289': 8.7}
The list of slow learners are:
['20181ISE550']
The meeting schedule for Prof Smith is :
dict_keys(['20181ISE550', '20181ISE560', '20181ISE349',
'20181ISE289'])

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)

num = int(input("Enter a number"))


r = int(input("Enter r"))
ncr = my_fact(num)/ (my_fact(r) * my_fact(num-r))
print("NCR = ", ncr)

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])

Enter the daily temperature in a week as a [[]][['sun',33.3],


['mon',32.1], ['tue',34.5], ['wed',28.7], ['thu',29.9], ['fri',28.6],
['sat',25.5]]
The coldest day is : ['sat', 25.5]
The warmest day is : ['tue', 34.5]

You might also like