9 September 2020

Lists

Last Time We will recap what we did last time and add in a few new elements.

Lists index just like strings.

>>> x = [5,3,-2, True, "caterwaul"]
>>> x[1]
3

You can do this too, if you like.

>>> x[-1]
'caterwaul'
>>> x[-2]
True

This is a very common Python idiom.

>>> x = x[:-1]  #puffy cheeks emoticon
>>> x
[5, 3, -2, True]

You can slice

>>> x[1:3]
[3, -2]

You can splice

>>> x[1:3] = ["cat", "dawg", "tapir"]
>>> 
>>> x
[5, 'cat', 'dawg', 'tapir', True]

You can explode, too.

>>> x[3:3] = "mess"
>>> x
[5, 'cat', 'dawg', 'm', 'e', 's', 's', 'tapir', True]

Rats, you can't do this!

>>> x[::2]
[5, 'dawg', 'e', 's', True]
>>> x[::2] = []
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 0 to extended slice of size 5

But you can do this.

>>> x[::2] = [10,11,12,13,14]
>>> x
[10, 'cat', 11, 'm', 12, 's', 13, 'tapir', 14]

Typical variable swappage.

>>> x = 0
>>> y = 1
>>> tmp = x
>>> x = y
>>> y= tmp
>>> x
1
>>> y
0

Pythonic variable swappage.

>>> x,y = y,x
>>> x
0
>>> y
1

Swapping is handy for lists

>>> x = ["Fawcett", "is", "spacing", "out"]
>>> x[0],x[1] = x[1],x[0]
>>> x
['is', 'Fawcett', 'spacing', 'out']

Trip's over; time to unpack.

>>> alpha, beta, gamma, delta = x
>>> alpha
'is'
>>> beta
'Fawcett'
>>> gamma
'spacing'
>>> delta
'out'

Are you my type?

>>> type(x)
<class 'list'>

Tuples

A tuple is an immutable list. Why this immutability?

Soon, we will be looking at hashed types; these include dictionaries and sets. Hashing enables these types to offer rapid retrieval of their contents. To see if a Python object is hashable, try calling the builtin hash function on it.

>>> hash(5)
5
>>> hash("foo")
7849859638514754987
>>> hash([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Lists are not hashable. No mutable Python container is hashable. All immutable types are hashable. Only hashable items can be placed in hashed containers.

Let's make a tuple

>>> x = (1,2,3,4,5)

Indexing is the same.

>>> x[0]
1

Have a slice and get a tuple

>>> x[3:5]
(4, 5)

Punishment time.

>>> x[0] = "pegasus"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

This is familiar.

>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
>>> (1,2,3) + (4,5,6)
(1, 2, 3, 4, 5, 6)

I reset my Python session so there are no leftovers. Observe this.

>>> s = [1,2,3]
>>> t = [4,5,6]
>>> u = s        #u and s point at the same list
>>> s += t
>>> s
[1, 2, 3, 4, 5, 6]
>>> u is s       #u and s point at the same list
True
>>> s = (1,2,3)
>>> t = (4,5,6)
>>> u = s         #u and s point at the same tuple.
>>> s += t
>>> s
(1, 2, 3, 4, 5, 6)
>>> u is s        #s is pointing at a NEW tuple.
False
>>> u             #The original tuple is untouched.  Immutability.
(1, 2, 3)

A Boolean Mystery Go to the Specs page and learn about boolean.html, in which you will do experiments and establish the order of operations for boolean operators.