In Python, we can represent text (a sequence of characters) using
the string data type. A string in Python is created
like so:
my_name = "John Doe"
Python will treat anything between two quotation marks (double
quotes "" or single quotes '') as a
string.
Strings can be indexed just like lists (think of them as lists of characters). For example:
So far, we have learned a few operations that you can perform on strings:
For example:
One of Python's many useful functions is the range() function. This function can be used to create a list of integers. The sequence can be nearly anything, as long as we are adding or subtracting by the same amount each step. We could create:
The range() function uses three pieces of information to create the list: the start, the stop, and the step. Of the three, only the stop is required; if we don't provide them, the other two will be assumed.
Using these three pieces of information, the range() function can be run in three different ways, each with their own possible results.
If only one number is provided, Python will assume it is the value
for "stop." It will assume that you want to "start" at 0, and that
you want to "step" by 1. For example, the code
print( list(range(8)) )
will print out
[0, 1, 2, 3, 4, 5, 6, 7]
Why did we not print out 8? When using range(), the "stop" is exclusive — it's excluded from the final list. Think of the "stop" as a stop sign when you're driving — you are supposed to stop the car before you get to the stop sign. In the same way, the "stop" in the range() function is counted up to, but is not included in the final list.
If two numbers are provided, Python will assume they are the values for "start" and "stop" (in that order). Again, it will assume that you want to "step" by 1. For example, the code
print( list(range(5, 15)) )
will print out
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Finally, if all three numbers are provided, Python will assume they are the values for "start," "stop," and "step" (in that order). Specifying the step allows you to do things like count backwards, or to print out only the odd numbers. For example, the code
print( list(range(20, 9, -1)) )
will print out
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
while the code
print( list(range(1, 11, 2)) )
will print out
[1, 3, 5, 7, 9]
But don't forget, Python is stupid! So if you want to count from 0 up to 10, and only print out the even numbers, the code print( list(range(10, 2)) ) will not do this! Python will do exactly what you tell it to do, so it will think that 10 is your "start" and "2" is your "stop," because you only gave it two numbers. In order to do what you originally intended, you need to give it the "start" of 0, even though that is the default start value: print( list(range(0, 10, 2)) )
We can even cause the range() function to count backwards,
by giving it a negative number as the step. So the code
print( list(range(100, 9, -10)) )
will print out
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
We can use for loops to iterate over a list; iterate means to move through a list, one element at a time.
For example:
list_of_fruits = ["kiwi", "banana", "peach"] for fruit in list_of_fruits: print("I ate a", fruit)
I ate a kiwi
I ate a banana
I ate a peach
Notice how the above loop executes for each item in the list, replacing the value of the variable fruit with the next item in the list.
The variable fruit is a copy of the current element of the list, and changing the variable fruit will not change anything in the original list (list_of_fruits).
A for loop can also be used to iterate through the characters in a string, effectively treating it as a list of characters.
For example:
name = "SAM" for character in name: print("Give me an", character + "!")
Give me an S!
Give me an A!
Give me an M!
Notice how the loop executes for each character in name. It starts with the first character in the string, executes the loop body (the single print() statement) using that character, and then moves on to the next character.
If we want to be able change the original list inside a loop, however, we need to be able to access each element of list, and not a copy. To do that, we use a for loop that loops over the index of each element of a list (or the index for each character of a string). To create the list of indexes that we need, we use the range() function we mentioned earlier.
We can use the range() function to create the list of indexes that we want to loop over. We can combine this with the len() function on a list to get the correct number of indexes.
For example:
greetings = ["Hello", "Hola", "Ciao", "Salut"]
The list greetings has four elements, so its indexes are [0, 1, 2, 3]. We can use the len() function to get the number "4". If we then pass that to the range() function, it will see that single number as the "stop" value, and will generate the needed list of indexes: [0, 1, 2, 3]
So this code:
greetings = ["Hello", "Hola", "Ciao", "Salut"] for i in range(len(greetings)): print("At index", i, "we say:", greetings[i])
At index 0 we say: Hello
At index 1 we say: Hola
At index 2 we say: Ciao
At index 3 we say: Salut
Since we are using the index, we are now directly accessing the actual elements of the list, rather than making a copy of them, like we did before. With this, we can use the for loop to change our list.
So this code, which changes each of the greetings to "Goodbye":
for i in range(len(greetings)): greetings[i] = "Goodbye" print(greetings)
['Goodbye', 'Goodbye', 'Goodbye', 'Goodbye']
The range() function can also be used to make the loop variable change in very specific ways. If we give the range() function three numbers, and the last number is negative, the loop variable will count down from the "start" to the "stop," decreasing by the "step" each time.
If you use this, make sure that your starting number is higher than your ending number, and make sure that the end of the range you specify is one less than where you actually want to stop.
For example:
courseOrder = ["CMSC 201", "CMSC 202", "CMSC 203", "CMSC 341"] for i in range(3, -1, -1): print("Course at index", i, "is", courseOrder[i])
Course at index 3 is CMSC 341
Course at index 2 is CMSC 203
Course at index 1 is CMSC 202
Course at index 0 is CMSC 201
One important thing to note is the names that we are using for the two styles of for loops. These are common, traditional choices (even in the "real world"), and they give you a hint about whether the loop iterates over the contents or the indexes of the list. (The contents means you can't make any changes, but the indexes let you directly access the list.)
When iterating over the indexes, programmers tend to use "i" (for "index") or "j" (if you need a second index-based loop).
When iterating over the contents, programmers tend to use a single letter derived from the list itself: "f" for a list called "fruits", "g" for a list called "greetings", or even "c" if the list is iterating over a string ("c" is short for character).
It's not required that you follow these conventions, but it is handy to know them.