point1 = [(1, 1)] # this is a list of tuples
point1.append((2,2)) # this will change point1 directly (in-place)
print(point1)
[(1, 1), (2, 2)]
point1 = [(1, 1)] # this is a list of tuples
point1 = point1.append((2,2)) # point1.append() returns None
print(point1) # this prints None, which is the content of point1
None
point1 = [(1,1)]
point1.append((0,0)) # this will make point1 None
print(point1)
[(1, 1), (0, 0)]
my_dict = {} # this is an empty dictionary
type(my_dict)
type({0, 1})
type({0: 1, 1:2})
my_set = set() # create an empty set
my_set
valid_dictionary = {("a", "b"): 0, ("b", "c"):1}
invalid_dictionary = {{1, 2}: 0, {2,0}: 1}
TypeError: unhashable type: 'set'
valid_dictionary[("a", "b")] # get the value corresponding to the key ("a", "b")
# Exercise: fix the dictionary
# invalid dictionary - this should break
room_numbers = {
('Freddie', 'Jessica'): 403,
('Ned', 'Keith'): 391,
('Kristin', 'Jasmine'): 411,
('Eugene', 'Jack'): 395
}
# TODO: fix the problem
print(room_numbers)
{('Freddie', 'Jessica'): 403, ('Ned', 'Keith'): 391, ('Kristin', 'Jasmine'): 411, ('Eugene', 'Jack'): 395}
int("1" + "2") - 1
# quiz: what is printed?
"1" + "2" * 3
elements = {"hydrogen": 1, "helium": 2, "carbon": 6}
elements.get("hydrogen", "Sorry, no gold.")
pets = {'dogs':[1, 2, 5], 'cats':[0, 3]}
pets.get('dogs')[1]
my_list = [1, 2, 5, 3] # a list is sortable and also ordered
my_list.sort()
print(my_list)
[1, 2, 3, 5]
my_tuple = (1, 2, 5, 3) # a tuple is not sortable and ordered
my_tuple.sort()
AttributeError: 'tuple' object has no attribute 'sort'
# Here you are given a dictionary of elements in the periodic table
elements = {'hydrogen': {'number': 1, 'weight': 1, 'symbol': 'H'},
'helium': {'number': 2, 'weight': 4, 'symbol': 'He'}}
# Task 1: without defining a new dictionary, add a new element 'carbon' into the dictionary
# Carbon has atomic number of 6, weight 12, and symbol 'C'
carbon = {'number':6, 'weight':12,'symbol':'C'}
elements['carbon'] = carbon
# Task 2: without defining a new dictionary, add a new information 'group'
# to the value for each element
# Carbon belongs to group 14 (carbon group), hydrogen 1 (alkali metals), and helium 18 (noble gas)
elements['hydrogen']['group'] = 1
elements['carbon']['group'] = 14
elements['helium']['group'] = 18
# Test
print(elements['hydrogen']['group']) # 1
print(elements['helium']['group']) # 18
print(elements['carbon']['group']) # 14
print(elements['carbon']['symbol']) # C
1
18
14
C
elements = {'hydrogen': {'number': 1, 'weight': 1, 'symbol': 'H'},
'helium': {'number': 2, 'weight': 4, 'symbol': 'He'}}
elements['hydrogen']
# Exercise: List v.s. Strings
sentence1 = "I love to program with Python."
sentence2 = ['I', 'love', 'to', 'program', 'with', 'Python', '.']
# Task 1: change the last element of sentence2 to "!"
sentence2[-1] = '!'
# Task 2: change the first element of sentence2 to "We"
sentence2[0] = 'We'
# Task 3: get the last element of sentence1
print(sentence1[-1])
# can I change the last element of sentence1 to "!"
sentence1[-1] = "!"
.
TypeError: 'str' object does not support item assignment
%run -i students.py
Unsupported output type: clearOutput
The chosen one is: Theonie Wijaya
# In this exercise, you are given the text of a poem by Rudyard Kipling, "If"
# https://www.poetryfoundation.org/poems/46473/if---
verse = "if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise"
# Below you find a dictionary containing the words in the poem and how many times they appear.
verse_dict = {'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, 'don\'t': 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1}
# Use the dictionary to answer the following
# 1. How many elements (i.e. words) are there in the dictionary?
# 2. Is the word 'smorgasbord' in the dictionary?
# 3. What is the first element you get if you sort the keys in alphabetical order?
# TDOO: print the number of words in the dictionary
# HINT: use the len() function
print("Number of words in dictionary:", len(verse_dict))
# TODO: print whether the word 'smorgasbord' is in the dictionary
# HINT: use the membership operator ("in" or "not in")
smorgasbord = 'smorgasbord' in verse_dict
print ("'smorgasbord' in dictionary:", smorgasbord)
# TDOO: get the first element in alphabetical order
# HINT: use the sorted built in function
sorted_dictionary = sorted(verse_dict)
print ("First element:", sorted_dictionary[0])
Number of words in dictionary: 51
'smorgasbord' in dictionary: False
First element: about
verse_dict
verse_dict.keys()
verse_dict.values()
sorted(verse_dict) == sorted(verse_dict.keys())
# Challenge exercise
# convert the following string into dictionary as above
verse = "if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise"
# TODO 1: convert verse into a list of words
# HINT: use the split() method
list_verse = verse.split()
print(list_verse)
# TODO 2: convert the list into a collection of unique elements
unique_verse = set(list_verse)
print(unique_verse)
['if', 'you', 'can', 'keep', 'your', 'head', 'when', 'all', 'about', 'you', 'are', 'losing', 'theirs', 'and', 'blaming', 'it', 'on', 'you', 'if', 'you', 'can', 'trust', 'yourself', 'when', 'all', 'men', 'doubt', 'you', 'but', 'make', 'allowance', 'for', 'their', 'doubting', 'too', 'if', 'you', 'can', 'wait', 'and', 'not', 'be', 'tired', 'by', 'waiting', 'or', 'being', 'lied', 'about', 'don’t', 'deal', 'in', 'lies', 'or', 'being', 'hated', 'don’t', 'give', 'way', 'to', 'hating', 'and', 'yet', 'don’t', 'look', 'too', 'good', 'nor', 'talk', 'too', 'wise']
{'keep', 'theirs', 'head', 'lies', 'yet', 'being', 'on', 'by', 'don’t', 'wait', 'way', 'yourself', 'blaming', 'their', 'or', 'to', 'doubt', 'about', 'hated', 'it', 'can', 'tired', 'nor', 'wise', 'talk', 'make', 'give', 'but', 'are', 'in', 'hating', 'waiting', 'good', 'and', 'lied', 'losing', 'too', 'your', 'when', 'if', 'men', 'you', 'for', 'not', 'deal', 'trust', 'be', 'allowance', 'doubting', 'look', 'all'}