Programmer Coding

Loop in Python

Python Loops

The following loops are to be had in Python to fulfil the looping needs. Python offers three selections for strolling the loops. The fundamental functionality of all of the techniques is the identical, even though the syntax and the amount of time required for checking the circumstance vary.

we are able to run a single statement or set of statements time and again the use of a loop command.

The range() Function

With the help of the range() characteristic, we may additionally produce a chain of numbers. range(10) will produce values among zero and nine. (10 numbers).

we are able to deliver specific begin, stop, and step length values inside the way variety(start, prevent, step size). If the step size isn’t always designated, it defaults to 1.

since it doesn’t create every price it “carries” when we assemble it, the range item can be characterized as being “slow.” It does provide in, len, and __getitem__ moves, but it isn’t an iterator.

The subsequent varieties of loops are available inside the Python programming language.

  1. For Loop
  2. While Loop
  3. Nested Loop

For Loop

Python’s for loop is designed to again and again execute a code block at the same time as iterating via a list, tuple, dictionary, or different iterable gadgets of Python. The procedure of traversing a sequence is referred to as new release.

Syntax

  1. for value in sequence:
    { code block }

Example

# Example 1: Iterating over a list

name = ["Ramkrishna", "Vishnu", "Shubham Kashyap"]

for friends in name:

    print(friends)

Output

Ramkrishna

Vishnu

Shubham Kashyap

# Example 2: Iterating over a string

for char in "Python":

    print(char)

Output

P

y

t

h

o

n

# Example 3: Iterating over a range of numbers

for i in range(1, 5):

    print(i)

Output

1

2

3

4

While Loop

While loops are used in Python to iterate till a specified situation is met. however, the statement in the program that follows the while loop is performed as soon as the circumstance modifications to false.

Syntax

  1. while<condition>:
    { code block }

Example of Factorial

# Number for which factorial is to be calculated
num = 5
# Initialize the factorial and a counter variable
fact = 1
i = 1
# Calculate factorial using a while loop
while i <= num:
    fact *= i
    i += 1
# Print the factorial
print("Factorial of", num, "is:", fact)

Output

Factorial of 5 is: 120

Nested Loop

These are basically loops inside loops. In other words, a nested loop refers to a loop inside a loop. The “inner loop” will execute one time for each iteration of the “outer loop”:

Example

# Nested loop to print a multiplication table
for i in range(1, 6):  # Outer loop for the rows
    for j in range(1, 6):  # Inner loop for the columns
        print(i * j, end="\t")  # Print the product of i and j with tab separation
    print()  # Move to the next line after printing each row

Output

1          2          3          4          5

2          4          6          8          10

3          6          9          12        15

4          8          12        16        20

5          10        15        20        25

Leave a Comment

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

Scroll to Top