Python – For Loop:

In this tutorial, we will learn about for loop. In Python, for loop is used to iterate over the sequence of elements (the sequence may be list, tuple or strings.. etc). Below is the syntax.

Syntax:

1

2

3

for_stmt::="for"target_list"in"expression_list":"suite

["else"":"suite]

In the below example, we have given the list of strings as courses and the for loop created to iterate through all the strings to print the course and the length of the course name.

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

>>>courses=['html','c','java','css']

>>>foriincourses:

...print(i,len(i))

...

html4

c1

java4

css3

>>>

In the below example, for loop iterates through the list of numbers. In the immediate step, if statement filters only the numbers less than 50, else it will display “no values” for rest of the iterations.