0% found this document useful (0 votes)
4 views

6_Loops

The document provides an overview of loops in Python, including types such as for loops, while loops, and nested loops. It explains loop control statements like break, continue, and pass, along with their syntax and examples. Additionally, it highlights the difference between pass statements and comments, emphasizing their respective purposes in code.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

6_Loops

The document provides an overview of loops in Python, including types such as for loops, while loops, and nested loops. It explains loop control statements like break, continue, and pass, along with their syntax and examples. Additionally, it highlights the difference between pass statements and comments, emphasizing their respective purposes in code.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Loops in Python

Learning objective
• Introduction to Loops
• Types of Loops
• For loop
• While loop
• Nested loop
• Loop control statements
• Break, continue and pass statement
• Difference between pass statement and comments
Introduction to Loops
• There could be a situation where you have to execute a block of
code several numbers of times.

• Generally, statements are executed sequentially.

• The primary statement during a function is executed first, followed


by the second, and so on.

• A loop statement allows us to execute a statement or group of


statements multiple times.
For loop
• Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.

• A for loop is used for iterating over a sequence.

• It is like an iterator method as found in other object-orientated


programming languages.
For loop syntax
for var in sequence:
statements(s)

• var is a variable that is used for iterating over a sequence. On every


iteration it takes the next value from sequence until the end of
sequence is reached.

• sequence : A sequence of values that will be assigned to the


target variable var. Values are provided using a list or a string or
from the built-in function range().
For loop example

for letter in 'Expert': for x in range (10,20):


print("current letter is: ", letter) print(x)

for x in range (10):


print(x) for x in range (10,20,2):
print(x)
Code to print the frequency of each digit
num = input("Enter a number : ")
print("The number entered is :", num)

uniqueDigit = set(num)
#print(uniqueDigit)

for elem in uniqueDigit:


print(elem, "occurs", num.count(elem), "times")
While loop
• Repeats a statement or group of statements while a given
condition is TRUE.

• It tests the condition before executing the loop body.

• While Loop is used to execute number of statements or body till


the condition passed in while is true.

• Once the condition is false, the control will come out of the loop.
While loop syntax
while expression:
statement(s)

• Here, statement(s) may be a single statement or a block of


statements.
• The condition may be any expression, and true is any non-zero value.
• The loop iterates while the condition is true.
• When the condition becomes false, program control passes to the line
immediately following the loop.
While loop example
count = 0 table =8
while (count < 6): while table < 81 :
print ('The count is:', count) print (table)
count = count + 1 table+=8
Nested loop
• The placing of one loop inside the body of another loop is named
nesting.

• Once you “nest” two loops, the outer loop takes control of the
amount of complete repetitions of the inner loop.

• Thus inner loop is executed N- times for each execution of outer
loop.

• Python programming language allows to use one loop inside


another loop .
Nested loop syntax
Syntax for nested for loop : Syntax for nested while loop :

for iterating_var in sequence: while expression:


for iterating_var in sequence: while expression:
statements(s) statement(s)
statements(s) statement(s)
Nested loop example
for x in range(1,6):
for y in range (1, x+1): print("Hi", end=" ")
print(x) print("Arif")

for x in range(1,6):
for y in range (1, x+1):
print(x, end=" ")

end parameter specifies what should be printed at the end of the output, instead of the default newline (\n)
Write a program to display the following
output
1
22
333
4444
55555
Infinite loop
• A loop becomes infinite loop if a condition never becomes FALSE.

• You must use caution when using while loops because of the
possibility that this condition never resolves to a FALSE value.

• This results in a loop that never ends. Such a loop is called an


infinite loop.

• An infinite loop might be useful in client/server programming


where the server needs to run continuously so that client
programs can communicate with it as and when required.
Infinite loop example
cnt = 10
while (cnt > 9):
print("the count is :", cnt)
Loop control statements
• Loop control statements change execution from its normal
sequence.

• When execution leaves a scope, all automatic objects that were


created in that scope are destroyed.

• Python supports the following control statements.


• break
• continue
• pass
Break statement
• Terminates the loop statement and transfers execution to the
statement immediately following the loop.

• The most common use for break is when some external condition
is triggered requiring a hasty exit from a loop.

• The break statement can be used in both while and for loops.
Break statement example
for x in range(11,21):
if(x==17):
break
print(x, end=" ")
Continue statement
• Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.

• It returns the control to the beginning of the while loop.

• The continue statement rejects all the remaining statements in the


current iteration of the loop and moves the control back to the top
of the loop.

• The continue statement can be used in both while and for loops.
Continue statement example
for x in range(11,21):
if(x==17):
continue
print(x, end=" ")
Pass statement
• The pass statement in Python is used when a statement is
required syntactically but you do not want any code to execute.

• The pass statement is a null operation; nothing happens when it


executes.

• It serves as a placeholder for code that you may plan to


implement later, allowing your program to run without throwing an
error for an empty block.
Pass statement example
for x in range(11,21):
pass
print ("loop with just placeholder")

age = 10
if age > 10:
pass
else:
print(“if with placeholder for future condition")
Pass vs Comment
Pass vs Comment

Feature pass Comment


Execution Executed (does nothing) Ignored by Python
Purpose Placeholder in empty blocks Documentation
Syntax Standalone statement Preceded by #
You have Learnt:
• Introduction to Loops
• Types of Loops
• For loop
• While loop
• Nested loop
• Loop control statements
• Break, continue and pass statement
• Difference between pass statement and comments

You might also like