Lecture 3 (4/1/22)¶
Last time we covered:
Datahub and Jupyter notebooks
Jupyter markdown
Python basics: operations and data types
Today’s agenda:
Data structures: lists, dictionaries, sets
Loops & conditions
Functions
Data Structures¶
Lists¶
What is a list?
Ordered
Mutable
Variable items
Examples?
# Declaring lists
foo = list() # or just: foo = []
foo = ['bar']
# Adding and removing items: append, extend, insert, remove
# Accessing items: indexing, 'in'
# Note: indexing is really flexible (ranges, -1)
# bonus: len
# Operations: sort, reverse
# Can we put lists inside of lists?? Yes. Yes we can.
# EXTRA BONUS: list comprehensions!!!
# Come back to this at the end of class if there's time...
Dictionaries¶
What is a dictionary?
Not ordered
Mutable
Item type matters
Examples?
# Declaring dictionaries
foo = dict() # or just: foo = {}
# Adding and removing items
# Accessing items
# Note: can use `in` here as well!
# bonus: 'keys', 'values', 'items'
# Can we put a dictionary inside a dictionary?? Youbetchya! This is actually very common.
Sets¶
What is a set?
Not ordered
Mutable
Variable items
Examples?
# Declaring sets
foo = set()
foo = set('hi how are you')
bar = set('doing fine')
foo = {1, 2, 3, 4}
bar = {2}
# Adding and removing items: add, remove
# Set operations: &, -, |
# Accessing items: bit tricky (use for loop, set operations, `pop`)
# In general, this is one reason sets aren't used very often!
# Can we put a set inside a set? ...
Loops & Conditions¶
for
Loops¶
What is a for
loop for? [aSk tHe ClaSs]
# for loop syntax: `for x in some_list`, `for x in range`, `for key, val in dict.items()`
while
Loops¶
What is a while
loop for?
# while loop syntax: `while ...:`
# Note: CAUTION when using while loops
Functions¶
Understanding why we use functions, how to write them, and how things can go wrong are really important to coding at all levels. I can’t stress this enough!
We won’t have as much time to dedicate to functions as we should so if any of this feels shaky, please reach out!
What are functions for?
How to write them
If time: scoping
"""
Function cookbook
def name_of_function([optional: parameters]):
CODE HERE
[optional: return X]
"""
# Example: let's turn our fizzbuzz code above into a function!
# Add parameters
# Discuss: scope
# Ex. passing in the fizzbuzz list, modifying in the function w/o returning it
'\nFunction cookbook\n def name_of_function([optional: parameters]):\n CODE HERE\n [optional: return X]\n \n'