working with for loop in python

working with for loop in python

Let's start working with for loop in python. For loop is used when want to execute a block of code repeatedly a fixed number of times.  Python also comes with the support of iterating the sequences using the for loop. we can use any number of for loops one inside another. we call it as nested for loops. We can skip the execution of code that is inside for loop using the "continue" statement. We can also stop the for loop based on some condition using the "break" statement.

Syntax of "for" loop:

for item in sequence:
    # .....    

Usage of keyword "range" in python:

"range" is used to create an iterator of integers from start number to end number with an defined shift number. By default start number is zero(0) and shift number is 1. Let's see some examples  how it can be used.

# syntax: range(start_number=0, end_number, shift_number=1)
one_to_ten = range(10)
# output: range(0, 10)
# output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(3, 9, 3)))
# output: [3, 6]

From the above code we can understand that range returns an iterator generator. so, it didn't print the actual output. But, after converting it to list we can see the output. If observe above outputs we can find it will not include the end number in the output.

working with "for" loop with "range"

Case: Write a program to print the 9th table.

for num in range(1, 11):
    print(9, "X", num, "=", 9 * num)
# Output:
# 9 X 1 = 9
# 9 X 2 = 18
# 9 X 3 = 27
# 9 X 4 = 36
# 9 X 5 = 45
# 9 X 6 = 54
# 9 X 7 = 63
# 9 X 8 = 72
# 9 X 9 = 81
# 9 X 10 = 90

working with "for" loop on iterables ("list", "tuple",  "dict", "set", "string")

Case: Write a program that say's "Hello" to given names("Ram", "Shera", "Katlin")

names = ("Ram", "Shera", "Katlin")
for name in names:
    print("Hello, ", name)
# output:
# Hello,  Ram
# Hello,  Shera
# Hello,  Katlin

It works same with all the iterables. For simplicity we have used a tuple in above example. You can try it with all the iterables mentioned above.

accessing index and item in from ordered iterables ("list", "tuple")

Case: Write a program to print a merit student list with a rank. cosider the order of the students for ranking (i.e, students = ["Ajax", "Mangli", "Bob"])

students = ["Ajax", "Mangli", "Bob"]
rank = 1
for student in students:
    print("Name: ", student, " Rank: ", rank)
    rank = rank + 1
# output: 
# Name:  Ajax  Rank:  1
# Name:  Mangli  Rank:  2
# Name:  Bob  Rank:  3

The above can be simplified using python's bultin function "enumerate"

students = ["Ajax", "Mangli", "Bob"]
for rank, student in enumerate(students):
    print("Name: ", student, " Rank: ", rank + 1)
# output: 
# Name:  Ajax  Rank:  1
# Name:  Mangli  Rank:  2
# Name:  Bob  Rank:  3

In python "enumerate" will take the iterable and finds it's length and iterates the index and object. Note: index starts with zero(0). That's the reason we used "rank + 1" in the above code. I've named index as "rank" in above code.

Read Next: getting started with django first app

Read Prev: Working with if ... elif ... else conditions in python

Blog Archive