Important to note: In loops, as in conditional statements, indentation matters.
For Loop:
The syntax for for-loops is the following:
The following example shows how we can print each letter of a word, using a loop. Since Python is dynamically typed, the program recognizes that word is a string variable, so the loop prints every letter of word. This is an example of strings as an iterable:
We can do something similar with numbers, looping through an array:
We can also nest conditionals inside of loops. Python uses "continue" and "break" statements to control the flow of the loop; the word "continue" causes the loop to skip the current block and return to the top for or while statement, whereas "break" stops the loop. These key words are not mandatory for loops and they can be used alone or together; they can be very useful and provide a quick solution in certain circumstances and a feature that other languages lack. In the example below, we skip even numbers and print the odd numbers in a range from 1-10:
The next example adds two lines to the code above to demonstrate how we can end a loop. Here we end the loop before printing the number 9:
While loops in Python follow a similar syntax to for loops:
In the following program, we add two at the end of each loop, resulting in even numbers 0-8:
We can use "continue" and/or "break" in while loops as well:
Nested Loops:
In Python, we can nest loops. The following program is an algorithm that prints prime numbers from one to one hundred. The outer loop runs through integers (i) two to ninety-nine. The inner loop loops through integers (j) that could be possible divisors of (i). If (j) does not divide (i) and (j) is greater than (i/j), (i) is prime. Until we reach a (j) that divides (i), we loop until (j) is greater than (i/j), so (i) is prime.
We can nest for loops in a similar way. The following example is a simple one that I made up to multiply numbers zero through nine by each number in the list containing one, two, and three:
It is also possible to put a for loop in a while loop and vice versa. Often times, we use nested loops to cycle through items in a list. The following method demonstrates this possibility in Python. Note how the conditional with the boolean here acts like the 'break' statement described previously to end the iterations of the loop:
In coding, there is usually more use for a while loop inside of a for loop than a for loop inside of a while loop, so I will not provide an example of the latter, but it is possible.
Else Statements in Loops:
Unlike languages like C, in Python, we can execute an "else" statement upon the completion of a loop. In the second loop below, the the loop is terminated before the end of it's iterations, so the else statement does not get executed.
Else statements in loops were a new concept to me, and they seem to be useful checks for when a loop has completed its rounds.
Also, in Python loops can be infinite. The following program will run forever unless you stop it, so be careful!
Response to Michelle's first comment:
Great question! In a 'for’ loop, to increment by a number other than one, you add parameters to range. The following code shows an example of this. The 'step' is whatever increment you want to add through every iteration of the loop:
Note if you were to use a while loop, the syntax to increment a variable would be: "i += number_you_want_to_add," and examples of this are included in my post.
Sources:
https://swcarpentry.github.io/python-novice-inflammation/02-loop/
https://www.learnpython.org/en/Loops
https://www.tutorialspoint.com/python/python_nested_loops.htm
https://stackoverflow.com/questions/21930663/while-loop-inside-of-for-loop
Is there no way to change the increments in a 'for' loop? For example, in Java we could increment with i++, i+2, i+10, etc. Based on the examples, it looks like the only option is to loop through each character in a string, element in an array, number in a sequence, etc.
ReplyDeleteCan you change the increment in the for loop by increasing the variable? Like when you looped through the numbers array, can you add number+2 after the print line to skip an index in the array?
I added an explanation of such a case along with an example at the bottom of my post. Great question!
Delete