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:
indexing to retrieve an element
slicing to retrieve a subset of elements
iteration to loop through every element
concatenation and multiplication
len()
Sequences Continued
We know two sequence data types. What are they? →
strings
lists
Lists
What's a list? What elements are allowed in a list? →
it's an ordered collection of values
any values!
List Syntax
In code, what's one way to create a list (as in, using a list literal), and how is an empty list represented? →
Indexing and Slicing Operations
How do we index into and slice out substrings of a list? →
About Indexes
Speaking of indexes, what type should an index be (in both indexing and slicing)? →
Indexing
Given a list items, what are three ways to retrieve the last element (operations, functions and methods are valid)? →
An Aside on Pop
By the way, name the two theings that pop does when called on a list. →
removes the last element of the list it is called on (mutates it)
returns a value, which is the value of the last element of the list that it is called on
Speaking of Mutability
What will happen when the code below is executed? Is anything printed out? Is there an error? Does nothing happen? →
lists are mutable
consequently, ['boo!', 2, 3] is printed out
List of Lists
Using the same list:
what indexing syntax would I use to get the letter 'b'?
what line of code would I use to change None to 'something'?
Every Item in a List
How can I continuously retrieve every item in a list, one item at a time? →
For Loops
What is the value of the loop variable, number, during each iteration? →
24
48
12
Nested Lists
How do for loops work with nested lists? What does this print out and what type is another_list in each iteration? →
Nested Lists Continued
With the same list, how do I print out every element of every list on its own line with an exclamation point? →
Slicing
What will the following slices return? →
Slicing Returns a New Sub List, It Does Not Alter the Original List!
What will the following slices print? →
Comparison Operators
What will the following code output? →
Addition and Multiplication
What will the following code output? →
Length, Deletion and Membership
What operators and/or functions would I use to (and how would I use them):
get the length of a list →
delete a list item →
determine if a value is a member (or a sublist) of another list →
Length, Deletion and Membership Continued
List are (Mutable) Objects
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:
Lists vs Strings
Name as many differences between lists and strings as you can! →
syntax (brackets []s versus quotes ""s)
mutability (strings are immutable, lists are mutable)
strings a are a sequence of characters, lists are a sequence of any value
Back to Mutability
Because lists are mutable, how do list methods typically work? Do they return values? Change the original object? →
most list methods modify the list in place
most list methods don't return a value!
if you're getting None… you're probably assigning the return result of a list method to a variable
Adding Elements
Name three methods that add elements to a list. What does each method do? What are each method's inputs and return value? →
append(object) - append object to end of list, even another list; returns None
extend(iterable) - appends all items of one iterable (list, string, etc.) to the original list; returns None
insert(index, object) - insert object before index; returns None
(also + operator)
Removing Elements
Name two methods that delete elements from a list. What does each method do? What are each method's inputs and return value? →
remove(object) - removes first occurrence of object in list, causes an error if object doesn't exist; returns None
pop() - returns and removes the last element, takes an optional argument to specify index, causes an error if index is out of range
(also del operator)
Miscellaneous Methods
Are there any other list methods that we know of? What does each method do? What are each method's inputs and return value? →
sort() - sorts a list in place; returns None
count(object) - counts the number of occurrences of object in the original list; returns the count
index(object) - returns the index of the object supplied; causes an error if the object doesn't exist; returns the index