Python Programming Language
(handout 2)
Loops and their importance
In a programming language, a loop is a statement that contains
instructions that continually repeats until a certain condition is
reached.
Loops help us remove the redundancy of code when a task has to be
repeated several times. With the use of loops, we can cut short those
hundred lines of code to a few. Suppose you want to print the text
“Hello, World!” 10 times. Rather than writing a print statement 10
times, you can make use of loops by indicating the number of
repetitions needed.
Loop Types
The three types of loops in Python programming are:
1. for loop
2. while loop
3. nested loops
For loop
What is for loop in Python?
The for loop in Python is used to iterate over a sequence (list, string) or other
iterable objects. Iterating over a sequence is called traversal.
For loop with range()
The range() function returns a sequence of numbers starting from 0 (by
default) if the initial limit is not specified and it increments by 1 (by
default) until a final limit is reached.
The range() function is used with a loop to specify the range (how many
times) the code block will be executed.
Examples
Code 1
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
the above program will print out fruits
Code 2
To print numbers from 0 to 4
for i in range (5):
print (i)
here range is used to specify till where you want the numbers to print. If you won’t
specify then numbers will start from 0 by default.
Code 3
To print numbers from 1 to 99
for i in range (1,100):
print (i)
here range will start from 1 and end till 99, 100 won’t show.
Code 4
To print “hello world” five times
list = [1, 2, 3, 4, 5]
for num in list:
print("Hello, World!")
Code 5
Calculate the square of each number of list
numbers = {1, 2, 3, 4, 5}
# iterate over each element in list num
for i in numbers:
# ** exponent operator
square = i ** 2
print("Square of:", i, "is:", square)
For Loop Questions to practice
Q1. Print first 10 numbers using a for loop.
Q2. Print first 20 even numbers using a for loop.
for i in range(0,21,2):
print(i)
Q3. Calculate the square of first ten numbers.
Q4. Write a For loop so that every item in the list is printed.[**hint see code 1 for
help]
list={"koala", "cat", "fox", "panda", "chipmunk", "sloth", "penguin", "dolphin"}
While Loop
In Python, The while loop statement repeatedly executes a code block
while a particular condition is true.
In simple words, The while loop enables the Python program to repeat a
set of operations while a particular condition is true. When the
condition becomes false, execution comes out of the loop immediately,
and the first statement after the while loop is executed.
Syntax for while loop
while condition:
# Block of statement(s)
Examples of while loop in Python
Code 1
Print numbers less than 5
count = 1
# run loop till count is less than 5
while count < 5:
print(count)
count = count + 1
Code 2
Print numbers less than and equal to 20
count = 1
# condition: Run loop till count is equal to 20
while count <= 20:
print(count)
count = count + 1
Note: The loop with continuing forever if you forgot to increment counter
in the above example
Code 3
Print all even numbers between 10 to 30 from given list using while loop
count = 10
# condition: Run loop till count is equal to 30
while count <= 30:
print(count)
count = count + 2
Code 4
Find the sum of all number below and equal to the number entered by user
While Loop Questions to practice
Q1. Print first 20 numbers using a while loop.
Q2. Print odd numbers from 20 to 50 using a while loop.
Q3. Calculate the square of first five numbers.
count = 1
square = 1
while count<= 5:
square = count**2
print(square)
count = count+1
Q4. Find the sum of all number below and equal to 10.