PYTHON LOOPS

Gorantla Hemanth Kumar
5 min readApr 25, 2021

python loops

python3

loops: The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.

For this purpose, The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the following diagram to understand the working of a loop statement.

Why we use loops in python?

The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop which runs up to 10 iterations.

Advantages of loops:

There are the following advantages of loops in Python.

  • It provides code re-usability.
  • Using loops, we do not need to write the same code again and again.
  • Using loops, we can traverse over the elements of data structures (array or linked lists). There are the following loop statements in Python:

Loop Statement:

Description:

  • for loop: The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
  • while loop: The while loop is to be used in the scenario where we don’t know the number of iterations in advance. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.
  • do-while loop: The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs).

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.

for iterating_var in sequence:
statement(s)

The for loop flowchart:

For loop Using Sequence

Example-1: Iterating string using for loop

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

Example-3:

Program to print the sum of the given list.

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:

range(start,stop,step size)

  • 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 -: Program to print table of given number.

Python While loop:

The Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop.

It can be viewed as a repeating if statement. When we don’t know the number of iterations then the while loop is most effective to use.

The syntax is given below.

while expression:
statements

Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0.

While loop Flowchart:

Loop Control Statements

We can change the normal sequence of while loop’s execution using the loop control statement. When the while loop’s execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop.

  1. Continue Statement — When the continue statement is encountered, the control transfer to the beginning of the loop. Let’s understand the following example.

Example:

2. Break Statement — When the break statement is encountered, it brings control out of the loop.

Example:

Using else with while loop

Python allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop. Consider the following example.

Example 1:

In the above code, when the break statement encountered, then while loop stopped its execution and skipped the else statement.

Example-2

Program to print Fibonacci numbers to given limit:

terms = int(input("Enter the terms "))  
# first two intial terms
a = 0
b = 1
count = 0

# check if the number of terms is Zero or negative
if (terms <= 0):
print("Please enter a valid integer")
elif (terms == 1):
print("Fibonacci sequence upto",limit,":")
print(a)
else:
print("Fibonacci sequence:")
while (count < terms) :
print(a, end = ' ')
c = a + b
# updateing values
a = b
b = c

count += 1
Enter the terms 10
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34

Conclusion: I hope you got a clear understanding on what is loops and what are the different types of loops and how to use them in programs.

--

--

Gorantla Hemanth Kumar
0 Followers

hi I am a engineering student studying in Gitam University .