google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Looping Statement in Python

Looping Statement in Python

0



To manage looping requirements, the Python programming language supports the following types of loops. The loops can be executed in three different ways in Python. While all of the methods provide comparable fundamental functionality, they differ in syntax and the time it takes to check for conditions.

While Loop:

While loops are used in Python to run a block of statements repeatedly until a condition is met. When the condition in the programme turns false, the line immediately following the loop is performed.

Syntax

 

while expression:

    statement(s)

 

 

After a programming construct, any statements indented by the same number of character spaces are regarded to be part of a single block of code. Python's way of grouping statements is indentation.

 

#Write a Program to illustrate while loop in Python

 

count = 0

while (count < 3):

            count = count + 1

            print("www.a2pstudy.com")

 

Output:

 

www.a2pstudy.com

www.a2pstudy.com

www.a2pstudy.com

 

 

While Loop Using else Statement

As explained previously, the while loop performs the block until a condition is met. The statement immediately following the loop is performed when the condition turns false.

When your while condition is false, the else clause is performed. It will not be run if you exit the loop or if an exception is triggered.

Syntax

 

while condition:

            # execute these statements

else:

            # execute these statements

 

 

Example:

 

#Write a Program to illustrate combining else with while in Python

count = 0

while (count < 3):

            count = count + 1

            print("www.a2pstudy.com")

else:

            print("Else Block Executed")

 

Output:

www.a2pstudy.com

www.a2pstudy.com

www.a2pstudy.com

Else Block Executed

 

For Loop in Python

For sequential traversal, loops are utilized. For instance, traversing a list, string, or array There is no C style for looping in Python, i.e. for (i=0; in; i++). There is a "for in" loop, which is analogous to other languages' for each loop. Let's have a look at how to utilise the for in loop to do sequential traversals.

Syntax

 

for var in sequence:

    statements(s)

 

 

On each iteration, var is the variable that takes the value of the item inside the sequence.

The loop will continue until the last item in the sequence is reached. Indentation is used to separate the body of the for loop from the remainder of the code.

 

# Program to find the sum of all numbers stored in a list

 

# List of numbers

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

 

# variable to store the sum

sum = 0

 

# iterate over the list

for val in numbers:

    sum = sum+val

 

print("The sum is", sum)

 

 

Output:

The sum is 48

 

 

The range() function

The range() method can be used to create a numeric series. range(10) generates a range of values from 0 to 9. (10 numbers).

Range(start, stop, step size) may also be used to define the start, stop, and step size. If step size is not specified, it defaults to 1.


# Write a Python Program to illustrate Iterating over range 0 to n-1

n = 4

for i in range(0, n):

            print(i)

 

 

Output:

0

1

2

3

 

 Example:

 

# Write a Python Program to illustrate  Iterating over a list

 

print("List Iteration")

l = ["www.a2pstudy.com", "for", "www.a2pstudy.com"]

for i in l:

            print(i)

           

# Iterating over a tuple (immutable)

print("\nTuple Iteration")

t = ("www.a2pstudy.com", "for", "www.a2pstudy.com")

for i in t:

            print(i)

           

# Iterating over a String

print("\nString Iteration")

s = " a2pstudy"

for i in s :

            print(i)

           

# Iterating over dictionary

print("\nDictionary Iteration")

d = dict()

d['xyz'] = 123

d['abc'] = 345

for i in d :

            print("%s %d" %(i, d[i]))

 

 

Output:

 

List Iteration

www.a2pstudy.com

for

www.a2pstudy.com

 

Tuple Iteration

www.a2pstudy.com

for

www.a2pstudy.com

 

String Iteration

a

2

p

s

t

u

d

y

 

Dictionary Iteration

xyz  123

abc  345

 

 

Iterating by index of sequences

It is also possible to iterate using the sequence's index. The fundamental concept is to compute the list's length first, then loop over the sequence within that range.


# Write a Python Program to illustrate Iterating by index

list = ["Study", "with", "a2pstudy"]

for index in range(len(list)):

            print list[index]

 

 

Output:

Study

with

a2pstudy

 

 

For loop with else Statement

Like with a while loop, we may combine an else statement with a for loop. However, because there is no condition in the for loop that will cause the execution to end, the otherwise block will be run immediately after the for block.


# Write a Python Program to illustrate Iterating by index with else block

list = ["Study", "with", "a2pstudy"]

for index in range(len(list)):

            print list[index]

else:

               print(“This is else Block”)

 

 

Output:

Study

with

a2pstudy

This is else Block

 

 

Nested Loop in Python

The Python programming language allows you to nest loops inside loops. A few examples are provided in the next section to demonstrate the idea.

Nested for Loop

Syntax

 

for var in sequence:

            for var in sequence:

                        statements(s)

                        statements(s)

 

 

Nested while Loop

The syntax for a nested while loop statement in Python programming language is as follows:

Syntax

 

while expression:

            while expression:

                        statement(s)

                        statement(s)

 

 

Example:

 

# Write a Program to illustrate nested for loops in Python

 

for i in range(1, 5):

            for j in range(j):

                        print(i, end=' ')

            print()

 

Output:

1

1 2

1 2 3

1 2 3 4

 

At last, we can say that we can use the loop inside a same or different type of loop. Like while inside for or for inside while and vice-versa.

 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top