What's a type? What are the types that we've seen so far? →
A type is a set of values. It's a way of classifying them. Some examples of types that we've learned include… →
strings (str)
integers (int)
floating point numbers (float)
complex numbers (complex)
booleans (bool)
Types of Types
Types can actually be classified as well. Like types of types! How are the following types related, and what do they have in common?
int
float
complex
all of these types are numeric
they support similar operations…
for example addition, multiplication, subtraction, division
Sequences
We've seen numeric types, like int, float and complex. Another kind/classification of type is a sequence. A sequence:
consists of an ordered collection of elements
with each element identified by an index
Sequences support operations like:
indexing to retrieve an element
slicing to retrieve a subset of elements
concatenation and multiplication
We've Seen This Before
What data type have we seen that supports these operations? →
A string is an ordered sequence of characters. It is a sequence type. It supports:
Lists
A list is another sequence type.
instead of characters, it's an ordered collection of values
any values!
the following is a list that consists of ints and strings
List Literals
Constructing a list using a list literal looks like this:
delimited by brackets… [1, 2, 3]
each value is separated by a comma
a list with no elements is an empty list
an empty list is []
again, mixed types are allowed (even other lists) - ['pie', 3, 3.14, ['apple', ['cherry']]]
Printing Lists
You can pass lists directly to the built-in print function. It will output the list as if it were a string literal:
You can also use str() or string formatting to create a string representation of a list:
Built-in List Operators and Functions
Because a list is just another sequence type many of the built-in operators and functions that work with strings behave similarly with lists.
indexing operators
iteration
slicing
comparison operators
multiplication/addition
len
Indexing Operations
What will the following code output? →
Indexing Operations Continued
What will the following code output? →
Indexing Operations Continued Some More!
Using the list below, How would you retrieve "homer"… once using a positive index, and another time using a negative index? →
Indexing Operations Summary
just like characters in a string, elements in a list can be accessed by their index (place)
indexes start at 0
use the value (a variable, a list literial, etc) followed by the square brackets
… with the index enclosed: [1, 2, 3][0] → 1
negative indexes start at the end of the list: [1, 2, 3][-1] → 3
Mutability
Unlike strings, however, lists are mutable. That means that you can reassign a value at an index! What will the following code print out? →
Indexing Into a List of Lists
Lists can be composed of any objects, including other lists:
you can index into the outside list as usual…
stuff[0] gives back ['foo', 'bar', 'baz'] →
to get into an element in the inner list, you can index again…
stuff[0][1] gives back 'bar'! →
you can even change an element in the inner list…
stuff[0][1] = "I'm in your list" →
List of Lists Exercises
Using the same list:
what indexing syntax would I use to get the number 3?
what line of code would I use to change 0.25 to 0?
Iterating Over a List
Just like iterating over a sequence of characters…
you can use a for loop to iterate over a sequence of values in a list
your loop variable contains the value of a list element
that changes to the next element after each iteration
loop continues until every element of list is exhausted
if list is empty, there will be no iterations
Iterating Over a List Example
What will the following for loop print out? →
Iterating Over a List of Lists
How do you think we can print out every element in a line of its own in this list of lists?
Slicing
Slicing also works on lists. What will the following code output? →
Slicing Summary
Again… like slicing strings.
use square brackets
…with start index and end index separated by a colon: [2, 3, 5, 7][1:3] → [3, 5]
start and end can be omitted: [2, 3, 5, 7][:2] → [2, 3]
Equality Operators
lists are compared lexicographically using comparison of corresponding elements
to be equal, each element must compare equal and the two sequences must be of the same type and have the same length.
Equal or not equal? →
Greater Than, Less Than
sequences are ordered the same as their first differing elements.
comparing [1,2,'x'] and [1,2,'y'] is essentially just comparing 'x' and 'y').
example: [1, 2, 3] > [1, 2, 1] → True
if the corresponding element does not exist, the shorter sequence is ordered first
example: [1,2] < [1,2,3] → True
Addition and Multiplication
Multiplication and addition of lists are similar to the same operations on strings. What will the following code output? →
Addition and Multiplication Summary
list concatenation
+
adds the elements of one list to another list
[1, 2] + [3, 4] → [1, 2, 3, 4]
list multiplication
*
repeats a list the given number of times
[1, 2] * 3 → [1, 2, 1, 2, 1, 2]
len()
You can still use the built-in function, len, on lists. What do you think the following code will output? →
Looking for something?
Testing for membership within a list is similar to testing for a character in a string. Use the in operator. It returns True if the operand on the left is an element in the list on right. What does the following code print out? →
Deleting List Items
You can delete list items using del statement:
the del statement is immediately followed by an indexed list or a list slice
the value or values of the indexed list or list slice are removed from the original list
What does the following code print out? →
Some Exercises
using this list: numbers = [1, 2, 3, 4, 11, 12, 13, 14]
Part 1: print out number with an exclamation point (no need to define a function)
Part 2: only do this for every other element starting with the 2nd element:
2!
4!
12!
14!
write a function that takes a list and returns the first half of the elements (it's ok to round down)