Python for loop

Python for loop

The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.

The syntax of for loop in python is given below.

The for loop flowchart

Python for loop

For loop Using Sequence

Example-1: Iterating string using for loop

Output:

P
y
t
h
o
n

Example- 2: Program to print the table of the given number .

Output:

5
10
15
20
25
30
35
40
45
50s

Example-4: Program to print the sum of the given list.

Output:

The sum is: 183

For loop Using range() function

The range() function

The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below.

Syntax:

  • The start represents the beginning of the iteration.
  • The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
  • The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.

Consider the following examples:

Example-1: Program to print numbers in sequence.

Output:

0 1 2 3 4 5 6 7 8 9 

Example – 2: Program to print table of given number.

Output:

Enter the number 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

Example-3: Program to print even number using step size in range().

Output:

Enter the number 20
2
4
6
8
10
12
14
16
18

We can also use the range() function with sequence of numbers. The len() function is combined with range() function which iterate through a sequence using indexing. Consider the following example.

Output:

Hello Peter
Hello Joseph
Hello Ricky
Hello Devansh

Nested for loop in python

Python allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax is given below.

Syntax

Example- 1: Nested for loop

Output:

Enter the rows:5
*
**
***
****
*****

Example-2: Program to number pyramid.

Output:

1
22
333
4444
55555

Using else statement with for loop

Unlike other languages like C, C++, or Java, Python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed.

Example 1

Output:

0
1
2
3
4
for loop completely exhausted, since there is no break.

The for loop completely exhausted, since there is no break.

Example 2

In the above example, the loop is broken due to the break statement; therefore, the else statement will not be executed. The statement present immediate next to else block will be executed.

Output:

0

The loop is broken due to the break statement…came out of the loop. We will learn more about the break statement in next tutorial.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263697.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论