<class 'int'>
>>>
Iterating Using Character Strings in a List
Besides iterating through numbers in a data list, you can also process through character strings in a
for loop data list. In Listing 7.8, five words are used in the data list instead of numbers.
LISTING 7.8 Character Strings in a Data List
Click here to view code image
>>> for the_word in ['Alpha','Bravo','Charlie','Delta','Echo']:
... print (the_word)
...
Alpha
Bravo
Charlie
Delta
Echo
>>>
The loop iterates through each word in the data list, just as it does through a list of numbers. Notice,
however, that you need to have quotation marks around each word.
By the Way: Quotation Mark Choices
You can use double quotation marks around each word in a data list rather than single
quotes, if you prefer. You can even use single quotation marks on some words in the
data list and double quotation marks on the rest of the words! However, such
inconsistency is considered poor form. So pick one quotation mark style for your data
list strings and stick with it.
Iterating Using a Variable
Data lists are not limited to numbers and character strings alone. You can use variables in a for loop
data list as well. In Listing 7.9, the variable top_number is assigned to the number 10.
LISTING 7.9 Variables in a Data List
Click here to view code image
>>> top_number=10
>>> for the_number in [1,2,3,4,top_number]:
... print (the_number)
...
1
2
3
4
10
>>>