Loop Statements in Python

Navya Gunda
3 min readJun 21, 2021

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.

While Loops

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.

while expression:
→Body of while

In Python, the body of the while loop is determined by indentation

How a While Loop works :

  1. The expression is the first thing that is checked
  2. The body of the loop is executed only if the expression evaluates to True
  3. After one iteration, the expression is checked again
  4. 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?

start=1while (start<=1000):
→ print(start)
→ start=start+1
print(“While Loop Finished”)

Do not forget to update variable in while expression otherwise the loop will run forever and never exits…. like below

start=1while (start<=1000):
→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

For Loops

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 = 0
for 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 :

print(range(1,10))

If you want all of the numbers stored in the range function you can use this

print(list(range(10)))

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”)

--

--