AddisCoder: Week 2
Lecture 6A:
Intro & Logistics
Who am I?
Live in Massachusetts. I like to hike and play soccer.
Why Study Computer Science?
Modern daily life power by CS.
Why Study Computer Science?
Major driver of progress of our civilization.
Why Study Computer Science?
Not just by traditional Software Engineering...
Why Study Computer Science?
Many reasons beyond just wanting to make websites and apps!
It’s also just fun :)
Day Structure
TIME | SCHEDULE |
9:00-10:00 | Lecture A |
10:00-12:00 | Lab A |
12:00-1:00 | Lunch |
1:00-1:30 | Break |
1:30-2:30 | Lecture B |
2:30-5:00 | Lab B |
Week Overview
Learning Objectives for this week
Week Overview
MON | TUES | WED | THURS | FRI |
July 25, 2023 | July 26, 2023 | July 27, 2023 | July 28, 2023 | July 29, 2023 |
Lecture 6A�Intro, Week 1 Review, Functions | Lecture 7A Libraries, Pixels and Images, Image Manipulation | Lecture 8A Recursion | Lecture 9A More Recursion | Lecture 10A Review, Quiz |
Lecture 6B Nested Lists, Nested Loops, Slicing | Lecture 7B More Images | Lecture 8B More Recursion | Lecture 9B Time Complexity | Lecture 10B Maps, List Comprehensions |
Lecture Structure
Let’s begin!
Dictionaries
Dictionaries
Like physical dictionary, Python dictionaries are used for look-up.
Dictionaries
Like physical dictionary, Python dictionaries are used for look-up.
Physical dictionary: look up definition for word.
Python dictionary: look up value using a key.
Dictionaries
Like physical dictionary, Python dictionaries are used for look-up.
Physical dictionary: look up definition for word.
Python dictionary: look up value using a key.
These are also sometimes called “maps”.
Dictionaries
A dictionary is an unordered collection of key:value pairs.
Dictionaries
A dictionary is an unordered collection of key:value pairs.
Dictionaries are good to use when you have a mapping of some sort, such as...
Creating a Dictionary
We use curly braces to create a dictionary:
grades = { "HW1": 95, "Lab1": 100, "Lab2": 90 } |
Creating a Dictionary
We use curly braces to create a dictionary:
Key and value pairs are given with a colon in between, separated by commas.
grades = { "HW1": 95, "Lab1": 100, "Lab2": 90 } |
Key
Value
Indexing (Reading from the Dictionary)
Index into the dictionary using the key to get the associated value.
Notice, indexing here is just like for lists, except you put the key inside the brackets.
grades = { "HW1": 95, "Lab1": 100, "Lab2": 90 } score1 = grades["HW1"] # == 95 |
Dictionary KeyError
If you try to get the value associated with a key that doesn’t exist, you get a KeyError.
grades = { "HW1": 95, "Lab1": 100, "Lab2": 90 } score1 = grades["Exam1"] |
Dictionaries
What types can be in a dictionary?
movie_info = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'actors': ['Ryunosuke Kamiki', 'Mone Kamishiraishi'], 'score': 96, } |
Dictionary Mutation
Mutation
It’s very common to want to update the values in a dictionary.
Let’s use this sample dictionary as an example.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } |
Mutation
Just like lists, dictionaries are Mutable using assignment.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } |
Mutation: Add an Item
Just like lists, dictionaries are Mutable using assignment.
�Insert new key:value pair by assigning to the new key.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' |
Mutation: Change an item
Just like lists, dictionaries are Mutable using assignment.
�Keys are unique, so assigning to an existing one replaces it.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' |
Mutation: Change an item
Just like lists, dictionaries are Mutable using assignment.
�Keys are unique, so assigning to an existing one replaces it.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' movie['title'] = 'Finding Nemo' |
Mutation: Delete an Item
Delete key:value pairs by passing the key into the dictionary.pop() function.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' movie['title'] = 'Finding Nemo' |
Mutation: Delete an Item
Delete key:value pairs by passing the key into the dictionary.pop() function.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' movie['title'] = 'Finding Nemo' movie.pop('score') |
Mutation: Delete an Item
Delete key:value pairs by passing the key into the dictionary.pop() function.
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' movie['title'] = 'Finding Nemo' movie.pop('score') |
Checking Dictionary Contents
To check whether a key exists, use `in` operator. `in` operator only checks keys of dictionary
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } contains_key = 'screenwriter' in movie # == False |
Checking Dictionary Contents
To check whether a key exists, use `in` operator. `in` operator only checks keys of dictionary
movie = { 'title': 'Your Name', 'year': 2016, 'rating': 'PG', 'running_time': 112, 'score': 96, } movie['writer'] = 'Andrew' contains_key = 'writer' in movie # == True |
Dictionary Iteration
Iteration
Dictionaries are unordered. But we can still iterate over them with a forloop.
�This iterate over keys in the dictionaries. Keys are not guaranteed to be in the order you created them.
for key in dictionary: print(key) # print current key print(dictionary[key]) # print value at that key |
Iteration: Example
abbreviations = {'Louisiana': 'LA', 'Utah': 'UT', 'Oregon': 'OR'} for state in abbreviations: print('State: ', state) print('Postal abbreviation: ', abbreviations[state]) |
Coding Example:
Letter Count Accumulation
Coding Example:
Letter Count Accumulation
s = “abcabc”
frequency_dict = {}
for char in s:
if char in frequency_dict:
frequency_dict[char] += 1
else:
frequency_dict[char] = 1
print(frequency_dict)
Input
Input
We learned “output” for how to print stuff out to the terminal. Now, we will learn how to take things in from the terminal.
New function!
answer = input("Prompt") |
Input
We learned “output” for how to print stuff out to the terminal. Now, we will learn how to take things in from the terminal.
New function!
��Prints “Prompt” to the screen, then waits for user to provide input.
answer = input("Prompt") |
Input
We learned “output” for how to print stuff out to the terminal. Now, we will learn how to take things in from the terminal.
New function!
��Prints “Prompt” to the screen, then waits for user to provide input.
We assign the words that the user types in to the variable answer.
answer = input("Prompt") |
Function Review
Vocab: