Join my course at Udemy (Python Programming Bible-From beginner to advanced )

Blogger templates

Wednesday 20 May 2020

Python FOR statement





Python For loop is similar to While loop and it loops through the items in ordered sequence. There is optional Else  statement with For loop that executes after the For statement block is executed. Following is the syntax for the Python For Loop.

For Loop Syntax

Rules

Following are the rules for For Loop statement:
  • At every iteration one item from sourceObject is assigned to targetObjectItem variable.
  • After assigning to targetObjectItem, block of code inside For loop is executed.
  • else  statement is optional in For Loop.
  • If else statement is defined, then it will be executed after For loop unless the control has come out of the for block with break statement.
  • Control comes out of For loop when all items from sourceObject is assigned to targetObjectItem.
Note: else statement in While and For loop should not be confused with the working with If/else block in Python where else  is only executed when If  is not executed. For While and For loop else code block is always executed unless the break statement is executed in For block.

Examples with List, Tuples and String

The below code shows the example of For loop with list, string and tuple. All three are sequence data types and  at every iteration For loop takes element one by one from left to right from the sequence container and print the element. Note that syntax for all three are similar and there is no change in because of different sequence objects.


##Printing items in list
for x in ["first", "second", "third"]:
print (x)

first second third

##Separate character from string
str = 'HelloPython'
for x in str:
print (x, end=" ")

H e l l o P y t h o n

##With Tuple
T = [(10,20), (30,40), (50,60)]
for (x,y) in T:

10 20
30 40
50 60



Examples with Dictionary

For loop integrates well with dictionary as well with similar syntax as that for list and tuple.  Following code prints the key  and keys and values from dictionary.

##Print Key value from dictionary.
##printing ‘key’ from dictionary
D = {"first":1, "second":2, "third":3}
for key in D:
    print(key, end = " ")

first second third 

##printing (key,value) from dictionary
D = {"first":1, "second":2, "third":3}
for (key,value) in D.items():
    print(key,value, sep=",", end = "\n")

first,1
second,2
third,3

Examples with Extended Assignment

For loops integrates well with extended assignment as well. For details on extended assignment please check my post at Python Assignment.

##With extended assignment
S = [(1,2,3,4,5), (6,7,8,9,10)]
for (a, *b, c) in S:
    print(a,b,c, sep=",", end="\n")

1,[2, 3, 4],5
6,[7, 8, 9],10



Conclusion

We have seen how For loop works and how easily it integrates with any container object with almost similar syntax of target and source objects. For loop may not be the faster option in Python and that's why Python provides functionality like List Comprehension, map, filter, reduce etc  that can imitate   the functionality of For loop with faster execution. We will discuss all alternatives of for loop  in later posts.
.

Share:

0 comments:

Post a Comment

Feature Top (Full Width)

Pageviews

Search This Blog