Here is or
>>> True or True
True
>>> True or False
True
>>> False or True
True
>>> False or False
False
Here is not
>>> not False
True
>>> not True
False
Here is and
>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False
Making an empty list, then adding to it.
>>> x = []
>>> x.append("cow")
>>> x
['cow']
>>> x.append("sheep")
>>> x
['cow', 'sheep']
Here is the length of a string.
>>> s = "werrewtwer"
>>> len(s)
10
No surprises here.
>>> len(x)
2
This is a new list, y
>>> y = ["farmer", "tractor"]
>>> x
['cow', 'sheep']
>>> y
['farmer', 'tractor']
Get an apple and add'em.
>>> x + y
['cow', 'sheep', 'farmer', 'tractor']
>>> x
['cow', 'sheep']
>>> y
['farmer', 'tractor']
Every Python object has an id. The id
function
reveals it. Observe how the state of x
changes. Lists
are mutable!
>>> id(x)
140581055586880
>>> x += y
>>> id(x)
140581055586880
>>> x
['cow', 'sheep', 'farmer', 'tractor']
Now watch this.
>>> a = "some"
>>> id(a)
140581055765232
>>> a += "thing"
>>> a
'something'
>>> id(a)
140581055765104
Do you see that when we did a += "thing"
that the
variable a
is pointing at a different string
.
Remember, strings are immuable.
>>> quack = ["teal", "mallard", "canvasback"]
>>> squawk = quack
>>> id(quack)
140581055763904
>>> id(squawk)
140581055763904
The two variables squawk
and quack
are pointing at the same object here.
>>> squawk.append("weasel")
>>> quack
['teal', 'mallard', 'canvasback', 'weasel']
>>> squawk
['teal', 'mallard', 'canvasback', 'weasel']
>>> squawk is quack
True
This is called aliasing.
Small integers are pre-loaded into Python's memory.
>>> id(0)
4463503680
>>> id(1) - id(0)
32
>>> id(100) - id(0)
3200
>>> id(200) - id(0)
6400
>>> id(250) - id(0)
8000
>>> id(260) - id(0)
140576592119984
>>> id(255) - id(0)
8160
>>> id(256) - id(0)
8192
>>> id(257) - id(0)
140576592119984
No pattern here.
>>> id(True)
4463282616
>>> id(False)
4463283352
>>> 616-352
264
This shows you all visible stuff. The double-underscore things
are Python's infrastructure.
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'quack', 's', 'squawk', 'x', 'y']
>>> x
['cow', 'sheep', 'farmer', 'tractor']
>>> y
['farmer', 'tractor']
Lists are a heterogeneous mutable sequence type. They can even
contain other lists!
>>> party = []
>>> party.append(5)
>>> party
[5]
>>> party.append("cowabunga")
>>> party
[5, 'cowabunga']
>>> party.append([1,2,3,4,5])
>>> party
[5, 'cowabunga', [1, 2, 3, 4, 5]]
>>> len(party)
3
Here is a little indexing fun.
>>> party[0]
5
>>> party[2]
[1, 2, 3, 4, 5]
>>> party[2][4]
5
Here we take it a little further.
>>> nest = [1,[2,3,4,"cows",[5,6,"heifers"]]]
>>> nest[1]
[2, 3, 4, 'cows', [5, 6, 'heifers']]
>>> nest[1][4]
[5, 6, 'heifers']
>>> nest[1][4][3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> nest[1][4][2]
'heifers'
>>> nest[1][4][2][0]
'h'
Ooh, list items are lvalues.
>>> x = [1,2,3,4,5]
>>> x[0] = 10
>>> x
[10, 2, 3, 4, 5]
>>> #items in a list are lvalues
>>> x
[10, 2, 3, 4, 5]
Kinda remind you of strings?
>>> x[1:4]
[2, 3, 4]
Rosemary Woods goes to work.
>>> x[1:4] = []
>>> x
[10, 5]
>>> x
[10, 5]
Now she is doing it again. List slices are lvalues that accept
lists.
>>> x[1:1]
[]
>>> x[1:1] = [2,3,4]
>>> x
[10, 2, 3, 4, 5]
>>> x[::2]
[10, 3, 5]
>>> x[1::2]
[2, 4]
>>> x
[10, 2, 3, 4, 5]
Are you in the in
crowd?
>>> 2 in x
True
>>> 9 in x
False
>>> "cat" in "caterwaul"
True
Alas.
>>> x.find(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'find'
Here is how to do it!
>>> x.index(2)
1
Python dishes out discipline.
>>> x.index(9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 9 is not in list
Here is a cast.
>>> x
[10, 2, 3, 4, 5]
>>> str(x)
'[10, 2, 3, 4, 5]'
BOOM!!
>>> list("trinitrotolulene")
['t', 'r', 'i', 'n', 'i', 't', 'r', 'o', 't', 'o', 'l', 'u', 'l', 'e', 'n', 'e']
>>> boom = list("trinitrotolulene")
>>>