In [ ]:
list # As with strings and numbers, simply typing the name of a Python "type" will print a CLASS instance back at you.
In [ ]:
list() # To get an actual INSTANCE of that CLASS, you "call" the object's primary built-in method using parenthesis.
In [ ]:
[] # Square brackets are used by Python to represent lists. This is an empty list just like list()
In [ ]:
type(list) # If you check the TYPE of the list CLASS, you will find that the CLASS defines a DATA TYPE.
In [ ]:
type(list()) # Once you put the parenthesis in, it's no longer the CLASS but rather is an INSTANCE of that CLASS.
In [ ]:
type(str), type(int), type(float), type(list), type(tuple), type(dict) # Lots of things are the definitions of CLASS TYPES.
In [ ]:
for a_type in str, int, float, list, tuple, dict:
message = "%s is a %s" % (a_type, type(a_type)) # These are the 6 major "types" you work with in Python.
print(message)
In [ ]:
repr(list) # When you see those pointy brackets in Python, it means you're seeing the "string representation" of a thing.
In [ ]:
help(list) # Types like lists have a lot of cool stuff built-in that's worth looking at with a help()
In [ ]:
[1, 2, 3] # Lists are more flexible than tuples. Just use square brackets for a list.
In [ ]:
for item in [1, 2, 3]: # You can control loops with lists
print(item)
In [ ]:
for item in [1, 2, 3, 'a', 'b', 'c', ('nested', 'tuple')]: # The lists can contain arbitrary data-types.
print(item)
In [ ]:
alist = ['a', 'b', 'c'] # You can assign a variable name to a list and loop through that instead:
for item in alist:
print(item)
In [ ]:
alist = ['a', 'b', 'c']
for item in alist:
print('foo-%s' % item) # Look at how you can build-up and display strings. The %s formats and inserts item into string.
In [ ]:
alist.append('d') # You can append to lists.
alist
In [ ]:
alist.pop() # You can pop items of teh end of the list.
alist
In [ ]:
alist.pop(0) # You can also pop from the beginning of a list uzing index 0.
alist
In [ ]:
alist = ['a', 'b', 'c']
for i, item in enumerate(alist):
print('iteration #%s: foo-%s' % (i, item)) # You can expose Python's internal index counter with enumerate()
In [ ]:
alist = ['a', 'b', 'c']
for i, item in enumerate(alist):
print('iteration #%s: foo-%s' % (i + 1, item)) # The internal counter is zero-based, so we must sometimes add one.