Daily Python 3 – Nested dictionaries

in the following excersise we have a dictionary inside a dictionary (nested dictionaries) and we want to add information to the nested dictionaries. Namely, the rating a book received out of 10. You have a list corresponding to the books in order. Use a for loop to add the new information to the nested dictionaries.

This is doable in 2 lines. Tips: use enumerate and f-strings!

Copy the following:

rating = [9,8,4]

book_dict = {
    'book1': {'name': 'Little women', 'pages': 759},
    'book2': {'name': 'War and Peace', 'pages': 1225},
    'book3': {'name': 'The Prince', 'pages': 164}
}

an example of how to access data in a nested dictionary is:

book_dict['book1']['pages']
>>> 759

Daily Python 4 – If we pass

# if we pass

lets set up a grader that changes scores attained by students, into their F to A equivalent. There are many ways to do this, but you will do it using if else statements (elif).

– Below 25 – F

– 25 to 45 – E

– 45 to 50 – D

– 50 to 60 – C

– 60 to 80 – B

– Above 80 – A

There is a catch, if students have a grade that is passing (50 and higher), and completed their extra assignments, then they can get a ‘+’ score on their alphabetical equivalent.

You will get a list of tuples with scores for each of the students and also a True or False value.

e.g

grades = [(87, True), (45, True), (67, False), (82, False)]

87, true = A+

87, false = A

Do this for the full list of 500, and check how many students pass.

Copy the full list to your notebook.

grade_list = ((86, True),
 (30, False),
 (2, True),
 (9, True),
 (78, True),
 (74, True),
 (71, False),
 (20, True),
 (61, False),
 (28, True),
 (32, True),
 (7, False),
 (85, True),
 (43, True),
 (39, False),
 (72, True),
 (72, True),
 (12, False),
 (81, True),
 (56, False),
 (78, True),
 (2, True),
 (73, True),
 (1, True),
 (49, True),
 (86, False),
 (87, False),
 (8, False),
 (82, False),
 (94, False),
 (80, False),
 (60, False),
 (47, False),
 (13, True),
 (55, False),
 (29, True),
 (39, False),
 (14, True),
 (88, False),
 (29, False),
 (73, False),
 (17, True),
 (46, False),
 (87, False),
 (72, False),
 (98, True),
 (70, False),
 (56, False),
 (93, True),
 (20, False),
 (4, True),
 (11, True),
 (14, False),
 (91, True),
 (43, True),
 (89, False),
 (8, True),
 (37, True),
 (5, False),
 (40, False),
 (65, False),
 (61, True),
 (76, False),
 (23, True),
 (14, False),
 (94, True),
 (89, False),
 (44, False),
 (15, True),
 (16, True),
 (10, False),
 (70, True),
 (29, True),
 (5, True),
 (16, False),
 (6, False),
 (33, True),
 (78, False),
 (71, False),
 (49, True),
 (26, False),
 (71, True),
 (12, True),
 (18, False),
 (23, True),
 (67, False),
 (6, True),
 (73, False),
 (6, True),
 (17, True),
 (72, False),
 (3, True),
 (83, True),
 (16, False),
 (56, True),
 (9, False),
 (0, False),
 (77, True),
 (49, False),
 (16, True),
 (5, False),
 (96, True),
 (43, True),
 (92, True),
 (92, False),
 (16, False),
 (65, False),
 (99, False),
 (17, True),
 (52, True),
 (38, False),
 (23, True),
 (16, False),
 (50, True),
 (12, True),
 (14, True),
 (31, False),
 (55, True),
 (42, False),
 (52, False),
 (14, False),
 (99, True),
 (44, True),
 (30, True),
 (24, False),
 (95, False),
 (45, False),
 (48, False),
 (20, False),
 (73, False),
 (14, True),
 (98, True),
 (90, False),
 (74, True),
 (7, True),
 (53, True),
 (25, False),
 (49, True),
 (61, True),
 (33, False),
 (27, True),
 (77, True),
 (58, True),
 (27, True),
 (55, True),
 (61, True),
 (4, False),
 (97, True),
 (54, False),
 (76, True),
 (68, False),
 (26, False),
 (79, True),
 (72, True),
 (80, True),
 (4, False),
 (41, True),
 (71, False),
 (23, True),
 (74, True),
 (33, True),
 (59, False),
 (40, True),
 (49, False),
 (13, True),
 (14, False),
 (72, False),
 (59, True),
 (52, False),
 (10, True),
 (77, False),
 (39, True),
 (69, True),
 (76, True),
 (55, True),
 (71, False),
 (50, True),
 (99, True),
 (37, True),
 (77, False),
 (16, False),
 (72, False),
 (72, False),
 (78, False),
 (38, False),
 (94, True),
 (68, False),
 (84, False),
 (75, False),
 (99, False),
 (66, False),
 (44, True),
 (26, False),
 (43, True),
 (68, False),
 (3, True),
 (24, False),
 (59, False),
 (44, True),
 (17, True),
 (58, False),
 (29, False),
 (89, True),
 (94, False),
 (10, False),
 (70, False),
 (71, False),
 (25, True),
 (3, False),
 (98, False),
 (27, True),
 (62, True),
 (73, True),
 (32, True),
 (33, True),
 (25, True),
 (6, False),
 (75, True),
 (7, False),
 (85, True),
 (7, True),
 (97, True),
 (84, True),
 (94, True),
 (95, False),
 (33, True),
 (47, False),
 (13, True),
 (23, False),
 (2, False),
 (5, False),
 (13, True),
 (88, True),
 (7, False),
 (64, False),
 (96, False),
 (58, True),
 (44, False),
 (64, False),
 (83, False),
 (70, False),
 (96, True),
 (86, False),
 (2, False),
 (60, True),
 (31, False),
 (65, True),
 (14, True),
 (72, True),
 (87, True),
 (17, True),
 (88, False),
 (76, True),
 (84, True),
 (77, False),
 (57, True),
 (76, True),
 (9, True),
 (66, True),
 (42, False),
 (55, False),
 (1, False),
 (69, True),
 (46, False),
 (74, True),
 (88, False),
 (5, False),
 (96, True),
 (22, False),
 (18, True),
 (30, False),
 (71, False),
 (64, False),
 (46, True),
 (62, True),
 (12, True),
 (85, True),
 (96, False),
 (46, False),
 (7, True),
 (85, True),
 (77, False),
 (46, False),
 (62, True),
 (22, True),
 (4, True),
 (96, True),
 (22, False),
 (39, True),
 (14, True),
 (63, True),
 (19, False),
 (19, True),
 (78, False),
 (67, True),
 (47, False),
 (2, False),
 (97, False),
 (51, True),
 (90, False),
 (51, True),
 (72, True),
 (79, False),
 (50, True),
 (46, False),
 (77, False),
 (18, False),
 (46, False),
 (0, True),
 (69, True),
 (81, True),
 (0, True),
 (32, True),
 (76, True),
 (59, False),
 (26, True),
 (2, False),
 (16, True),
 (5, True),
 (31, False),
 (45, True),
 (33, True),
 (77, False),
 (83, True),
 (2, True),
 (10, False),
 (52, True),
 (70, False),
 (91, True),
 (43, True),
 (75, False),
 (67, True),
 (29, True),
 (43, False),
 (35, False),
 (9, True),
 (6, False),
 (18, False),
 (75, True),
 (61, True),
 (53, True),
 (83, True),
 (75, True),
 (69, True),
 (76, True),
 (9, True),
 (99, True),
 (71, True),
 (56, False),
 (62, False),
 (60, True),
 (79, True),
 (82, False),
 (70, True),
 (65, False),
 (53, False),
 (49, False),
 (81, True),
 (58, False),
 (37, True),
 (98, True),
 (10, True),
 (74, True),
 (83, True),
 (51, False),
 (35, True),
 (43, True),
 (75, False),
 (48, True),
 (31, True),
 (71, True),
 (74, False),
 (85, False),
 (72, False),
 (62, True),
 (61, True),
 (93, False),
 (17, True),
 (47, False),
 (39, False),
 (54, True),
 (38, True),
 (42, False),
 (13, False),
 (58, True),
 (81, True),
 (67, False),
 (49, True),
 (0, True),
 (4, True),
 (80, True),
 (66, False),
 (48, False),
 (1, True),
 (74, False),
 (0, False),
 (52, True),
 (30, False),
 (74, False),
 (61, True),
 (60, True),
 (96, True),
 (4, True),
 (41, False),
 (93, False),
 (26, False),
 (91, True),
 (1, False),
 (10, False),
 (54, True),
 (60, True),
 (91, False),
 (35, False),
 (64, False),
 (80, True),
 (28, False),
 (17, False),
 (81, False),
 (24, False),
 (74, True),
 (55, True),
 (73, False),
 (94, True),
 (54, False),
 (14, True),
 (0, False),
 (50, False),
 (46, False),
 (11, False),
 (16, True),
 (71, True),
 (42, True),
 (43, True),
 (60, True),
 (47, True),
 (30, False),
 (13, True),
 (91, True),
 (74, True),
 (10, False),
 (61, False),
 (98, True),
 (29, False),
 (6, True),
 (35, False),
 (41, True),
 (99, True),
 (63, False),
 (15, False),
 (82, True),
 (7, False),
 (1, True),
 (43, True),
 (30, False),
 (98, True),
 (11, False),
 (76, False),
 (15, True),
 (0, False),
 (82, False),
 (16, False),
 (70, False),
 (81, False),
 (63, True),
 (19, False),
 (76, True),
 (17, True),
 (23, True),
 (25, True),
 (30, True),
 (34, False),
 (78, True),
 (18, True),
 (72, True),
 (63, False),
 (64, False),
 (78, False),
 (25, True),
 (30, False),
 (63, False),
 (54, True),
 (19, True),
 (90, False),
 (20, False),
 (63, False),
 (60, True),
 (81, True),
 (51, False),
 (50, False),
 (44, True),
 (11, False),
 (90, True),
 (14, True),
 (96, False),
 (30, False),
 (66, True),
 (47, True),
 (74, False),
 (57, False),
 (30, False))

Daily Python 7 – Writing our own max()

Implement a function that takes as input three variables, and returns the largest of the three. Do this <b>without</b> using the Python max() function!

The goal of this exercise is to think about some internals that Python normally takes care of for us. All you need is some variables and if statements!

e.g max_number(1,2,3) >>> 3

def max_number(n1, n2, n3):
    current_max = n1
    if current_max > n2 and current_max > n3:
        return current_max
    elif current_max < n2:
        current_max = n2
    if current_max > n3:
        return current_max
    else:
        return n3

Daily Python 1 – Counting words & dictionaries

Yi Sang’s Poem No. 1

We noticed that many of you find it hard to work with dictionaries and add values. Hence the following assignment. I’ve also received feedback that many of you enjoyed the difficult excersise in recreating Yi Sang’s poem. To not deny those that have not worked on it the pleasure of his work, we will again follow his poem No1 (translated in english).

Copy the following code block into your own notebook and good luck!

Focus on doing this assingment in steps if you find it hard. Start with the for loop, and expand from their. Use the hint section (below) to get help.

poem = """
13 children speed toward the way.
(For the road a blocked alley is apt.)

The 1st child says it is scary.
The 2nd child says it is scary.
The 3rd child says it is scary.
The 4th child says it is scary.
The 5th child says it is scary.
The 6th child says it is scary.
The 7th child says it is scary.
The 8th child says it is scary.
The 9th child says it is scary.
The 10th child says it is scary.

The 11th child says it is scary.
The 12th child says it is scary.
The 13th child says it is scary.

Among 13 children there are scary children and
scared children and they are all they are. (It is
better that there is no other excuse.)

Of those it is fine to say that 1 child is scary.
Of those it is fine to say that 2 children are scary.
Of those it is fine to say that 2 children are scared.
Of those it is fine to say that 1 child is scared.

(For the road an opened one is apt.)
It does not matter if 13 children do not speed toward the way.
"""

Good luck. The solutions will be uploaded the next day along with the new practice assignment.

Click here to see Hint 1

first check if you can print each words present in the poem using a for loop. 

Hint 1 Solution

first check if you can print each words present in the poem using a for loop. 

# using .split() on any text will turn it into a list of words, sepearting by whitespace.
poem_dict = {}
for word in poem.split():
    print(word)


Click here to see Hint 2

You will need an if-else statement in your for loop

do a sanity check of your if else statement. Check if what is happening is indeed happening.


Hint 2 Solution

You will need an if-else statement in your for loop

do a sanity check of your if else statement. Check if what is happening is indeed happening.


I do this through the following:
    - adding 1 word to the dictionary
    - check if 'excuse' in poem_dict indeed returns True. (it should..)
    - check if 'test' in poem_dict return False. (it should..)

# next we write an if statement
poem_dict = {}
poem_dict['excuse'] = 1
# will be in my if statement - should return True.
print('excuse' in poem_dict)
# should return False
print('test' in poem_dict)