Housekeeping The quiz will go up to
§3 of p0.pdf
. Download this from Canvas.
Unfinished Business
Here are three more relational operators we will see.
is The Bill Clinton Operator. It checks
for equality of identity. Notice that every object has
a unique ID. Since x
and
y
have different ids, they are diffent
objects.
>>> x = []
>>> y = []
>>> x == y
True
>>> x is y
False
>>> id(x)
140504783066752
>>> id(y)
140504783240384
Small strings get put in the string pool. This area of memory is not garbage collected. These strings get recycled during the program's execution.
>>> x = "cows"
>>> y = "cows"
>>> x == y
True
>>> x is y
True
You can do this because strings are immutable. It makes no sense to store copies of a given string. You can see the immutability demonstrated here.
>>> x[0] = "C"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
is not
id
Python Sequence Types
There are three.
- lists: This is a heterogeneous mutable sequence type.
- tuples: This is a heterogeneous immutible sequence type.
- strings: This is an immutable sequence of characters.
Common Features
len() This returns the number of elements in a list or tuple or the number of characters in a string.
in for a list or a tuple, this tests for membeship. For a string, it checks for substringness.
Slicing and Indexing
What's up Doc?Bookmark these.
Method vs. Function
Sequence Methods