6.00.1x - Week 3

3.5.1 Tuples
ordered sequence of mixed element types
immutable, represented with parentheses ()
must add comma ',' at the end of 1 element tuple, convenient for swapping variable values

3.5.2 Lists
ordered sequence of mixed element types
mutable, represented with square brackets []
https://docs.python.org/3/tutorial/datastructures.html

3.5.3 List Operations
append, extend (change the list)
del, pop, remove, list (convert string to list)
sort vs. sorted, reverse
range returns something that behaves like a tuple or a list

3.5.4 Mutation, Aliasing, Cloning
clone: chill = old[:]
sort: mutates the list, returns nothing
sorted: does not mutate list, returns a new sorted list

3.5.5 Functions as Objects
Can pass function name as parameter
map(abs, [1, -2, -3, -4] => [1, 2, 3, 4]
map(min, L1, L2)

3.6.1 Quick Review

3.6.2 Dictionaries
instead of using multiple lists, use dictionary
represented with curly braces: {}
mutable, keys, values, no order
key: must be unique, immutable, hashable type

3.6.3 Example with a Dictionary

3.6.4 Fibonacci and Dictionaries
faster algorithm by storing already calculated result into a dictionary

3.6.5 Global Variables
dangerous to use
keep track of information inside function
keyword: global









Comments

Popular posts from this blog

Conditional Statements

6.00.1x - Week 2

Basic Machine Architecture