var.attr
var[3]
var["Aa"]
func(1, "Bb")
var.func(False)
Lists: a collection of different objects
a = []
a.append(1) # a = [1]
a.extend([2,3]) # a = [1,2,3]
a.reverse() # a = [3,2,1]
a.index(1) # 2 -------^
a.pop() # a = [3,2]
del a[0] # a = [2]
E.g. storing read books or published papers
Dictionaries: a mapping between keys and values
a = {1:1, 2:4, 3:9, 4:16}
a[5] = 25 # a = {1:1, 2:4, 3:9, 4:16, 5:25}
a.extend({0:0, 1:-1}) # a = {1:-1, 2:4, 3:9, 4:16, 5:25, 0:0}
del a[4] # a = {1:-1, 2:4, 3:9, 5:25, 0:0}
a.get(1) # -1
a.pop(2) # 4; a = {1:-1, 3:9, 5:25, 0:0}
list(a.keys()) # [1, 3, 5, 0]
list(a.values()) # [-1, 9, 25, 0]
list(a.items()) # [(1,-1),(3,9),(5,25),(0,0)]
E.g. storing a phonebook
a = []
a.append(1) # a = [1]
a.extend([2,3]) # a = [1,2,3]
What is the relationship between a
and a List?
What is append
or extend
?
Author defines how data is stored and how it should be used: an interface
Example: checking out a book at a library
User's data that follows the class author's standard
mybook = pick_random_book()
mybook.title # "Robinson Crusoe"
mybook.author # "Daniel Defoe"
mybook.checkout() # Error: no account specified
mybook.checkout(1234) # "Thank you for using our library"
Lists and dictionaries are built-in classes. Strings as well, and many others
To check which class (or type) is a variable:
>>> a = "hello"
>>> type(a)
<class 'str'>