You are on page 1of 3

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

print(d['b'])
print("Keys of the dictionary are",d.keys())
print("Values of the dictionary are", d.values())
print("Items of the dictionary", d.items())

ball
Keys of the dictionary are dict_keys(['a', 'b', 'c'])
Values of the dictionary are dict_values(['apple', 'ball', 'cat'])
Items of the dictionary dict_items([('a', 'apple'), ('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 = 5 d ={1:1, 2:4, 3:9, 4:16, 5:25}
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.
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)}
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

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',34] ['tue',33], ['fri',35]]
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}
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']
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(n1):
f = 1
for i in range(1,n+1):
f = f * i
return(f)

n = int(input("ENter a number n"))


r = int(input("Enter r"))
ncr = my_fact(n) / (my_fact(r) * my_fact(n-r))
print('ncr', ncr)

ENter a number n3
Enter r2
ncr 0.16666666666666666

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(temps1):
out = sorted(temps1, key = operator.itemgetter(1))
return(out)

import operator
temps = [['sun',33.3], ['mon',32.1], ['tue',34.5], ['wed',28.7],
['thu',29.9], ['fri',28.6], ['sat',25.5]]
temps = maxmin(temps)
print("The Coldest Day is", temps[0])
print("The Warmest Day is", temps[len(temps) - 1])

The Coldest Day is ['sat', 25.5]


The Warmest Day is ['tue', 34.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
[[]]"))
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 [[]][['sun',33.3],


['mon',32.1],['tue',34.5],['wed',28.7]]
The coldest day is: ['wed', 28.7]
The warmest day is: ['tue', 34.5]

You might also like