1 of 10

PYTHON LOOPS

2 of 10

What is Loop?

  • A sequence of statement which repeated over a time this process is said to be loops.A loop statement allows us to execute a statement or group of statements multiple times.
  • A loop is used to iterate over the python objects and perform any logic with the data

3 of 10

Types of Loops?

4 of 10

What is for loop?

  • For Loop: Executes a block of code a specific number of times. It's commonly used when the number of iterations is known.

For example:

X = [1,2,3,4,5,8,10]

for i in x:

if i > 5:

print(“yes”)

print(i)

5 of 10

What is while loop?

While Loop: Repeats a block of code as long as a specified condition is true. It's useful when the number of iterations is not known.

For example:

i = 10

while i < 20

print(“yes”)

i = i+1

6 of 10

What is nested loop?

  • A nested loop in Python is a loop inside another loop. The "outer" loop runs once, and for each iteration of the outer loop, the "inner" loop runs completely.

For example:

x = [1,2,3,4,5,6,7]

for i in x:

for j in x:

print(“yes”)

7 of 10

What is loop control statement?

  • Loop control statements in Python are used to change the execution flow of loops.

  • The most common loop control statements are break, continue, and pass. Each serves a different purpose

.

8 of 10

What is break ?

  • The Break statement is used to exit a loop immediately, regardless of the loop's condition.

For Example:

for i in range(10):

if i == 5:

break # Exit the loop when i is 5

print(i)

9 of 10

What is continue?

  • The continue statement is used to skip the current iteration of a loop and move to the next iteration.

For Example:

for i in range(10):

if i % 2 == 0:

continue # Skip the even numbers

print(i)

10 of 10

What is pass?

  • The pass statement is a null operation; it is used when a statement is required syntactically but you do not want any command or code to execute.

For Example:

for i in range(5):

if i == 3:

pass # Do nothing when i is 3

print(i)