Reading in English Week 2 - Day 6
Theme: Python Lists
A list in Python is any sequence of values surrounded by square brackets (i.e. [ ]). So for
instance [0, 1, 2, 3] is a list. So is [‘a’, 1,‘b’, 4.2]. Lists are any sequence of values inside
square brackets. The items of the list can be of different types, although it is quite common
for all values in a list to be of the same type. The list type is called list in Python as you might
expect.
A list is a sequence too. A list can be iterated over using a for loop just like a string. Each
element of the list is used to execute the body of the for loop once. There are several
operations on sequences that are useful. For instance, len(s) returns the length of a
sequence (the number of elements in the sequence). We can concatenate two sequences
using +. So writing s + t returns a new string which is the juxtaposition of the strings
referenced by s and t. We can get part of a sequence by slicing it. A slice is one or more
contiguous elements of a sequence. It is created by using brackets and a colon. For instance,
if s refers to the string “how are you?”, then s[0:3] is the string “how” and s[4:7] is the string
“are”. You can even get a slice starting at the end of a sequence. So, s[−4:] gives you the last
four items of a sequence, the string “you?” in this case. The length function, concatenation
operator, and slicing apply to either strings or lists since they apply to all types of sequences
in Python.
The list of integers starting from 0 and going to n − 1 is so useful there is a function in Python
that we can use to generate such a list. It is called range. The range function can be called
on an integer, n, and it will generate a list of integers from 0 to n − 1. For instance, range(5)
generates the list [0, 1, 2, 3, 4].
The range function can be used to generate other ranges of integers, too. In general the
range function is called by writing range([start,]stop[,increment]). For example, range(10,
110, 10) generates the list [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] and range(10, 0, −1]
generates the list [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. The indexing operations apply to all sequences,
not just strings. Using indexing and a for loop together we can write some interesting code.
Example 1. This example uses indexing to print each of the characters in a string on separate
lines.
1 s = input ("Please type some characters and press enter:")
2 for i in range ( len ( s ) ):
3 print ( s [ i ] )
4 print (" Done " )
Notice the use of the len function inside the call to the range function. When we wish to go
through all the elements of a list and we need an index into that list, the len function can
be used along with range to generate the proper list of integers for the indices of the list.
Python includes a few methods that make it much easier to process strings in your
programs. One of these methods is called split. The split method splits a string into words.
Each word is defined as a sequence of characters separated by whitespace in your string.
Whitespace are blanks, tabs, and newline characters in your strings. The split method splits
a string into a list of strings.
Example 2. Notice that the for loop contains s.split().
1 s = input ( "Please type some characters and press enter:" )
2 for word in s.split ( ):
3 print ( word )
4 print ( "Done" )
If the user enters “how are you?” the output is:
how are you?. Done
Another useful operator on sequences is the in operator. This operator makes it possible to
check to see if an item is in a sequence. For a string, this means you can ask, “Is a character
in this string?”. For a list it means you can ask if an item is in a list.
Example 3. Consider this code that determines if you like something similar to Sophus Lie.
The in operator let’s you find an item in a list and returns True if it does and False otherwise.
1 activity = input ( "What do you like to do?" )
2 liesActivities = [ "math", "hike", "walk", "gymnastics" ]
3 if activity in liesActivities:
4 print ( "Sopus Lie like to do that, too!" )
5 else:
6 print ("Good for you!")
Reference: Kent D. Lee. Python programming Fundamentals. Second Edition. Springer.