Last class, which is on 11th of April, we've continued learning about Python with Dr. KB. This time we've learned about Repeating Actions with Loops.
An example task that we might want to repeat is printing each character in a word on a line of its own.
word = 'lead'
We can access a character in a string using its index. For example, we can get the first character of the word ‘lead’, by using word[0]. One way to print each character is to use four
print
statements:
print(word[0])
print(word[1])
print(word[2])
print(word[3])
l
e
a
d
However, the above example is a bad approach. Here’s a better approach:word = 'lead'
for char in word:
print(char)
l
e
a
d
word = 'oxygen'
for char in word:
print(char)
o
x
y
g
e
n
for element in variable:
do things with element
Using the oxygen example above, the loop might look like this:

where each character (
char
) in the variable word
is looped through and printed one character after another. The numbers in the diagram denote which loop cycle the character was printed in (1 being the first loop, and 6 being the final loop).In the example above, the loop variable was given the name
char
as a mnemonic; it is short for ‘character’. ‘Char’ is not a keyword in Python that pulls the characters from words or strings. In fact when a similar loop is run over a list rather than a word, the output would be each member of that list printed in order, rather than the characters.
list = ['oxygen','nitrogen','argon']
for char in list:
print(char)
oxygen
nitrogen
argon
We can choose any name we want for variables. We might just as easily have chosen the name
banana
for the loop variable, as long as we use the same name when we invoke the variable inside the loop:word = 'oxygen'
for banana in word:
print(banana)
o
x
y
g
e
n
It is a good idea to choose variable names that are meaningful so that it is easier to understand what the loop is doing.
Here’s another loop that repeatedly updates a variable:
length = 0
for vowel in 'aeiou':
length = length + 1
print('There are', length, 'vowels')
There are 5 vowels
It’s worth tracing the execution of this little program step by step. Since there are five characters in
'aeiou'
, the statement on line 3 will be executed five times. The first time around, length
is zero (the value assigned to it on line 1) and vowel
is 'a'
. The statement adds 1 to the old value of length
, producing 1, and updates length
to refer to that new value. The next time around, vowel
is 'e'
and length
is 1, so length
is updated to be 2. After three more updates, length
is 5; since there is nothing left in 'aeiou'
for Python to process, the loop finishes and the print
statement on line 4 tells us our final answer.Note that a loop variable is just a variable that’s being used to record progress in a loop. It still exists after the loop is over, and we can re-use variables previously defined as loop variables as well:
letter = 'z'
for letter in 'abc':
print(letter)
print('after the loop, letter is', letter)
a
b
c
after the loop, letter is c
Phew!! That's sure a lot to digest in a class >.< in the next class, we will learn about a new topic and meet another lecturer! Stay tuned! <3
No comments:
Post a Comment