Friday 4 December 2015

Python: for statement

Python’s for statement is used to iterate over the items of any sequence like a list, string.

Syntax
for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

‘else’ clause is optional, it is executes, once the loop terminates.


test.py
names=["Phalgun", "Sambith", "Mahesh", "swapna"]

for name in names:
 print(name)
else:
 print("Exiting from loop")

print("Finsihed Execution")

$ python3 test.py
Phalgun
Sambith
Mahesh
swapna
Exiting from loop
Finsihed Execution



Previous                                                 Next                                                 Home

No comments:

Post a Comment