The ‘in’ Operator
· Boolean test whether a value is inside a container:
>>> t = [1, 2, 4, 5]
>>> 3 in t
False
>>> 4 in t
True
>>> 4 not in t
False
· For strings, tests for substrings
>>> a = 'abcde'
>>> 'c' in a
True
>>> 'cd' in a
True
>>> 'ac' in a
False
· Be careful: the in keyword is also used in the syntax
of for loops and list comprehensions
The + Operator
The + operator produces a new tuple, list, or
string whose value is the concatenation of its
arguments.
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> “Hello” + “ ” + “World”
‘Hello World’
The * Operator
· The * operator produces a new tuple, list, or
string that “repeats” the original content.
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’
Mutability:
Tuples vs. Lists
Lists are mutable
>>> li = [‘abc’, 23, 4.34, 23]
>>> li[1] = 45
>>> li
[‘abc’, 45, 4.34, 23]
· We can change lists in place.
· Name li still points to the same memory
reference when we’re done.
Tuples are immutable
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> t[2] = 3.14
Traceback (most recent call last):
File "<pyshell#75>", line 1, in -toplevel-
tu[2] = 3.14
TypeError: object doesn't support item assignment
· You can’t change a tuple.
· You can make a fresh tuple and assign its
reference to a previously used name.
>>> t = (23, ‘abc’, 3.14, (2,3), ‘def’)
· The immutability of tuples means they’re faster
than lists.
Operations on Lists Only
>>> li = [1, 11, 3, 4, 5]
>>> [Link](‘a’) # Note the method
syntax
>>> li
[1, 11, 3, 4, 5, ‘a’]
>>> [Link](2, ‘i’)
>>>li
[1, 11, ‘i’, 3, 4, 5, ‘a’]
The extend method vs +
· + creates a fresh list with a new memory ref
· extend operates on list li in place.
>>> [Link]([9, 8, 7])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
· Potentially confusing:
• extend takes a list as an argument.
• append takes a singleton as an argument.
>>> [Link]([10, 11, 12])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10,
11, 12]]
Operations on Lists Only
Lists have many methods, including index, count,
remove, reverse, sort
>>> li = [‘a’, ‘b’, ‘c’, ‘b’]
>>> [Link](‘b’) # index of 1st occurrence
1
>>> [Link](‘b’) # number of occurrences
2
>>> [Link](‘b’) # remove 1st occurrence
>>> li
[‘a’, ‘c’, ‘b’]
Operations on Lists Only
>>> li = [5, 2, 6, 8]
>>> [Link]() # reverse the list *in place*
>>> li
[8, 6, 2, 5]
>>> [Link]() # sort the list *in place*
>>> li
[2, 5, 6, 8]
>>> [Link](some_function)
# sort in place using user-defined comparison
Tuple details
· The comma is the tuple creation operator, not parens
>>> 1,
(1,)
· Python shows parens for clarity (best practice)
>>> (1,)
(1,)
· Don't forget the comma!
>>> (1)
1
· Trailing comma only required for singletons others
· Empty tuples have a special syntactic form
>>> ()
()
>>> tuple()
()
Summary: Tuples vs. Lists
· Lists slower but more powerful than tuples
• Lists can be modified, and they have lots of
handy operations and mehtods
• Tuples are immutable and have fewer
features
· To convert between tuples and lists use the
list() and tuple() functions:
li = list(tu)
tu = tuple(li)