Saturday, February 9, 2019

For loops in Python

Loops are used to run a specific code a number of times over a sequence or range of values. Like any other programming language, there is for loop in Python too, but it is implemented in a totally different way.
Here are some examples of using for loop to traverse through each item in a List or each character in a String.

Looping through a list of items:

colors = ["red","green","blue","yellow","orange"]
for x in colors:
    print(x)


Looping thorough a string:

text = "Bombay"
for x in text:
    print(x)


The range() function:

Python doesn't use an initialization or a conditional expression to specify the start and end of a for loop. Instead it uses the range() function to iterate over a sequence of numbers.
The range() function starts from 0 by default if only one argument is passed to it and ends at one less than the number passed to it.

Example:

This code will print 0 to 9 serially.

for x in range(10):
    print(x)




It is also possible to specify a starting and an ending value in the range() function.

Example:

This code will print 4 to 9 serially:

for x in range(4,10):
    print(x)




By default the range() function increments the sequence by 1. However, it is possible to increment the sequence by other values or even in the reverse order with a negative number. Following are a few examples showing this.

Example:

The following will print 0, 2, 4, 6, 8

for x in range(0,10,2):
    print(x)


The following will print 8,5,2

for x in range(8,0,-3):
    print(x)


Using else with for loop:

Python lets us use an else block with for loops. The else block is executed after the for loop finishes all the iteration. The else block won't be executed if the loop exits abruptly through a break statement.

for x in range(10):
    print(x)
else:
    print('Tasks to do after the loop finishes')


The break statement:

The break statement is used to stop the for loop in between the iterations.

Example:

The following code will print from 0 to 4

for x in range(10):
    if x == 5:
        break
    print(x)


The continue statement:

The continue statement skips the current iteration and moves to the next iteration of the for loop.

Example:

The following code will skip 2 and print 0, 1, 3, 4

for x in range(5):
    if x == 2:
        continue
    print(x)


Creating a list using for loop:

Another interesting feature of the for loop is that we can create lists in Python using for loops in the way shown below.


numList = [x for x in range(1, 10)]
print(numList)

The above code will print [1, 2, 3, 4, 5, 6, 7, 8, 9]


Using the enumerate() function with for loop:

We can use the enumerate() function with for loop to get the index of each elements while traversing through a sequence.

colors = ["red","green","blue","yellow","orange"]
for i, x in enumerate(colors):
    print(x, 'is at index', i)


This will print:

red is at index 0
green is at index 1
blue is at index 2
yellow is at index 3
orange is at index 4
 


No comments: