For & While Loops in Python. Topic – 13

for-and-while-loops-in-python

Introduction to Loops:

Sometimes a programmer wants to execute a group of statements a certain number of times. This can be done using loops. Based on this loops are further classified into the following main types.

  • for loop
  • while loop

The For Loop:

The for loop in Python is used to iterate over each element of a sequence. This sequence may be a string, list, tuple, dictionary, or any itrable object. Let’s understand through some examples:

Syntax
for variable in sequence:
    # Code to execute for each element in the sequence

Example:

Example:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)
  • fruits is variable and in this variable, we assign lists.
  • The for loop is used to iterate over each element of the fruits list. Every iteration
  • In each iteration, the fruit variable is equal to the current element of the fruits list.
  • The print(fruit) line prints the value of the fruit variable during each iteration.

The Output Of This Code Will Be:

Output
apple
banana
cherry

So, overall, this code prints each element of the list of the fruits one by one, resulting in the output ‘apple’, ‘banana’, and ‘cherry’. The for loop helps you repeat a specific task for each element in the sequence.

Example 2: Iterating through a String:

word = "Python"

for letter in word:
    print(letter)

Here, word is a variable and we store a word in this variable. We iterate every letter that is stored in the word variable. When we add a word variable in the loop, after every iteration the value will stored in the letter variable, and print the letter in every iteration.

The Output Of This Code Will Be:

Output
P
y
t
h
o
n

Example 3: Using range() for a Numeric Range:

In Python programming range() is a built-in function, that generates a numeric range. Using range() function we can generate a sequence of numbers, So that can be used with for loop.

Syntax
Syntax:
range(start, stop, step)

start: The starting value. This means from where we want to start the loop. By default, it starts from 0, but if we specify the value then the loop will start from the provided value.

Stop: Last value. The sequence up to the last value will be generated. This value is not included.

Step: The size of the interval. By default, this value is 1. If we give this value 2, then it will be skipping one item per iteration.

Example:

for number in range(1, 5):
    print(number)

The Output Of This Code Will Be:

Output
1
2
3
4

Example:

Syntax
for number in range(0, 10, 2):
    print(number)

The Output Of This Code Will Be:

Output
0
2
4
6
8

Here, a sequence was generated using range(0, 10, 2) with an output ranging from 0 up to 8, as 10 was not included. The step value was 2, so each iteration advanced by 2 steps.

The While Loop:

The While Loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The loop continues as long as this condition is true. Here is the structure of a basic while loop in Python.

Syntax
while condition:
    # Code to be executed as long as the condition is true
    # The loop continues until the condition becomes false

Here’s a simple example to illustrate the concept:

count = 0

while count < 5:
    print("Count is", count)
    count = count + 1

Breakdown:

Iteration 1:

  • count is initially set to 0.
  • The condition `count < 5` is true (0 is less than 5).
  • Inside the loop: Print “Count is 0“, then increment `count` by 1.
  • After the iteration, `count` becomes 1.

Iteration 2:

  • The loop checks the condition again (`count < 5`), which is true (1 is less than 5).
  • Inside the loop: Print “Count is 1“, then increment `count` by 1.
  • After the iteration, `count` becomes 2.

Iteration 3:

  • The loop checks the condition (`count < 5`), which is true (2 is less than 5).
  • Inside the loop: Print “Count is 2“, then increment `count` by 1.
  • After the iteration, `count` becomes 3.

Iteration 4:

  • The loop checks the condition (`count < 5`), which is true (3 is less than 5).
  • Inside the loop: Print “Count is 3“, then increment `count` by 1.
  • After the iteration, `count` becomes 4.

Iteration 5:

  • The loop checks the condition (`count < 5`), which is true (4 is less than 5).
  • Inside the loop: Print “Count is 4“, then increment `count` by 1.
  • After the iteration, `count` becomes 5.

Iteration 6:

  • The loop checks the condition again (`count < 5`), which is false (5 is not less than 5).
  • The loop exits, and the program moves to the next statement after the while loop.

In summary, the loop runs 5 times, printing the current value of `count` in each iteration and incrementing it by 1 until `count` reaches 5.

Output
5
4
3
2
1
counter is 0

If you don’t know the most important topic from the interview point of view “Match Case Statements In Python” just click on read more.

I hope you understand For & While Loops in Python. If you do not understand this topic just comment below with your email ID, and I will mail back to you.

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog