Loop Statements in Python
Before we jump into Loops let’s take a quick look at Conditional Statements
The conditional statements are : If, Else, and Elif
Loops are repetitions of these statements and they make lives easier. Instead of writing 100 if statements you can just write 1 loop of your choice. Keep in mind there are 2 loops, For and While.
What are while loops? The while loop in Python is used to iterate over a block of code as long as the while expression (condition) is true.
→Body of while
In Python, the body of the while loop is determined by indentation
How a While Loop works :
- The expression is the first thing that is checked
- The body of the loop is executed only if the expression evaluates to True
- After one iteration, the expression is checked again
- This process continues until the expression evaluates to False
This is how a While Loop flowchart would look
Example of code :
Now suppose you want to print numbers from 1–1000 will you write 1000 print statements or use 3 lines of code with a while loop?
print(“While Loop Finished”)
Do not forget to update variable in while expression otherwise the loop will run forever and never exits…. like below
→print(start)print(“While Loop Finished”)
We need to increase the value of the counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop)
Making shapes using a While Loop :
import turtlemy_turtle=turtle.Turtle()my_turtle.speed(1)a=1while a<=4:→my_turtle.forward(100)→my_turtle.right(90)a=a+1
You can also make shapes that are more complicated
import turtleangle=91a=0turtle.speed(15)while a<100:→turtle.circle(a)→turtle.left(angle)a=a+1
The for loop in Python is used to iterate over a sequence ( list, tuple, string ) or other iterable objects. Iterating over a sequence is called traversal.
for var in sequence:
→Body of for
This is a program to find the sum of all numbers stored in a list
num = [ 5, 6, 3, 4, 8, 4, 11, 2, 5]
sum = 0for value in num:
→sum = sum+value
→print(sum)print(“The sum is”, sum)
You can iterate over a string, tuple, and list. You can do this in the same way as the code above. Replace the value of num with any sequence data type.
Range Function :
Let’s say you want to iterate over 1000 numbers using for loop it is not feasible to first create a list of 1000 numbers and then iterate
You can generate a sequence of numbers using range() function.
The range function is “lazy” because it doesn’t generate every number that it “contains” when we create it.
This function does not store all the values in memory; it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go
Syntax of Range :
If you want all of the numbers stored in the range function you can use this
This will print the numbers 1–10 in a list
Example of Code using Range :
This code iterates over sequence that range() has created
for x in range(10):→print(“The Number is”,x)print(“For Loop Ended”)