Indices!
We can select an element with an index
We can also use a negative number to count from the end of the list
Repeated indecies can be used to retrieve data from lists with more than one dimension.
What will print(table[1][2][0])
show? How about print(table[0, 0, -1])
?
Here the slice 2:4
means start at index 2 and keep going to, but not including, index 4.
What will print(numbers[2:2])
show?
In the slice a:b
, both a
and b
are optional.
my_list[:b]
is equivalent tomy_list[0:b]
.my_list[a:]
is equivalent tomy_list[a:len(my_list)]
what does
my_list[:]
do?
Given a two dimensional table ([[...]...), how would you select the last element of every row?
Strings!
Concatinate them!
Convert to them, from them
Strings can be indexed and iterated over like a list.
There are lots of excellent methods to work with strings:
split
join
strip
lower/upper
replace
startswith
Challenge question: convert the string '1, 2, 3, 4' into the list of integers [1, 2, 3, 4]
String litterals help you write text with newlines in it.
Format Strings!
Python keeps reinventing string formatting, the latest is format strings (Python version >= 3.6)
Everything between the brackets in the format string is evaluated
Formatting options for everyone!
The previous preferred way of formatting strings was the .format method
Always be careful with user input!
You can shoot yourself in the foot.
An exception has occurred, use %tb to see the full traceback.
SystemExit
You can get pretty silly with these
We have fun here
Regular Expressions!
Special characters in regular expressions:
\d any digit
\ escape
. any single character
* between 0 and infinite repetitions of the previous character
+ between 1 and infinite repetitions of the previous character
? between 0 and 1 repetitions of the previous character
{i,j} between i and j repetitions of the previous character
() group that can be operated on, or referenced later with \1 ... \9
lots more ...
Lets make a regular expression that matches a phone number!