What's a sequence, and what are some operations and built-in functions that sequences support? →
A sequence is an ordered collection of elements. Sequences support operations like:
We know two sequence data types. What are they? →
What's a list? What elements are allowed in a list? →
In code, what's one way to create a list (as in, using a list literal), and how is an empty list represented? →
"""
a list is delimited by brackets, with each value separated by a comma
"""
items = ["some", "stuff", "between", "brackets"]
"""
an empty list is two square brackets - open and close
"""
an_empty_list = []
How do we index into and slice out substrings of a list? →
>>> items = ['foo', 'bar', 'baz', 'qux']
>>> items[0]
'foo'
>>> items[1:3]
['bar', 'baz']
>>>
Speaking of indexes, what type should an index be (in both indexing and slicing)? →
>>> """ indexes are integers """
>>> items = [True, False, False]
>>> items[0]
True
>>> items[0.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not float
Given a list items, what are three ways to retrieve the last element (operations, functions and methods are valid)? →
>>> items = [True, False, False]
>>> items[-1]
False
>>> items[len(items) - 1]
False
>>> items.pop()
False
By the way, name the two theings that pop does when called on a list. →
What will happen when the code below is executed? Is anything printed out? Is there an error? Does nothing happen? →
items = [1, 2, 3]
items[0] = "boo!"
print(items)
Using the same list:
stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
stuff[1][1]
stuff[2][2] = 'something'
How can I continuously retrieve every item in a list, one item at a time? →
""" use a for loop """
for number in [24, 48, 12]:
print "%s more hours to go!" % number
What is the value of the loop variable, number, during each iteration? →
for number in [24, 48, 12]:
print "%s more hours to go!" % (number)
How do for loops work with nested lists? What does this print out and what type is another_list in each iteration? →
stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
for another_list in stuff:
print another_list
""" another list is always a list in this case! """
[9, 16, 25]
['a', 'b', 'c']
[[], "", None]
With the same list, how do I print out every element of every list on its own line with an exclamation point? →
stuff = [[9, 16, 25], ['a', 'b', 'c'], [[], "", None]]
for inner_list in stuff:
for item in inner_list:
print "%s!" % (item)
What will the following slices return? →
numbers = [5, 11, 17, 19]
print(numbers[0:3])
print(numbers[2:])
print(numbers[:2])
print(numbers[0:100])
[5, 11, 17]
[17, 19]
[5, 11]
[5, 11, 17, 19]
What will the following slices print? →
nonsense = ['foo', 'bar', 'baz', 'qux']
print(nonsense[1:3])
print(nonsense)
['bar', 'baz']
['foo', 'bar', 'baz', 'qux']
What will the following code output? →
a = [1, 2, 3]
b = [1, 2, 3]
c = [1, 6, 3]
d = [1, 6, 3, 2]
print(a != b)
print(b > c)
print(d > c)
False
False
True
What will the following code output? →
a = [1, 2, 3]
b = ['x', 'y', 'z']
print(a + b)
print(a * 3)
[1, 2, 3, 'x', 'y', 'z']
[1, 2, 3, 1, 2, 3, 1, 2, 3]
What operators and/or functions would I use to (and how would I use them):
>>> nonsense = ['foo', 'bar', 'baz', 'qux']
>>> len(nonsense)
4
>>> del nonsense[0]
>>> print(nonsense)
['bar', 'baz', 'qux']
>>> del nonsense[20]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> 'baz' in nonsense
True
Lists are objects, and they have a slew of methods that you can call on them. However, because lists are mutable, many methods actually change the list in place! Let's see how this differs from strings:
>>> s = 'hello'
>>> s.upper()
'HELLO'
>>> print(s)
hello
>>> numbers = [5, 3, 4]
>>> numbers.sort()
>>> print(numbers)
[3, 4, 5]
Name as many differences between lists and strings as you can! →
Because lists are mutable, how do list methods typically work? Do they return values? Change the original object? →
Name three methods that add elements to a list. What does each method do? What are each method's inputs and return value? →
Name two methods that delete elements from a list. What does each method do? What are each method's inputs and return value? →
Are there any other list methods that we know of? What does each method do? What are each method's inputs and return value? →