SlideShare a Scribd company logo
LOOPING STATEMENTS AND
CONTROL STATEMENTS IN PYTHON
Ms.C.PRIYANKA
AP/CSE
KIT-KALAIGNARKARUNANIDHI INSTITUTE OF
TECHNOLOGY ,
COIMBATORE
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
While loop statement in Python is used to
repeatedly executes set of statement as long
as a given condition is true.
 In while loop, test expression is checked first.
The body of the loop is entered only if the
test_expression is True. After one iteration, the
test expression is checked again.
 This process continues until the
test_expression evaluates to False.
In Python, the body of the while loop is
determined through indentation.
The statements inside the while starts with
indentation and the first unindented line marks
the end.
Syntax
Looping Statements and Control Statements in Python
1. Program to find sum of n numbers:
2. Program to find factorial of a number
3. Program to find sum of digits of a number:
4. Program to Reverse the given number:
5. Program to find number is Armstrong
number or not
6. Program to check the number is palindrome
or not
n=eval(input("enter n"))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print(sum)
enter n
10
55
Factorial of a numbers: output
n=int(input("enter n"))
i=1
fact=1
while(i<=n):
fact=fact*i
i=i+1
print(fact)
enter n
5
120
Sum of digits of anumber: output
n= int (input("enter anumber"))
sum=0
while(n>0):
a=n%10
sum=sum+a
n=n//10
print(sum)
enter a number
123
6
Reverse the given number: output
n= int (input("enter a number")) sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
print(sum)
enter a number 123
321
Armstrong number ornot output
n=eval(input("enter a number")) enter a number153
org=n The given number is Armstrong number
sum=0
while(n>0):
a=n%10
sum=sum+a*a*a
n=n//10
if(sum==org):
print("The given number is Armstrong
number")
else:
print("The given number is not
Armstrong number")
Palindrome or not output
n=eval(input("enter a number")) enter a number121
org=n The given no is palindrome
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n //10
if(sum==org):
print("The given no is palindrome")
else:
print("The given no is not palindrome")
for in range:
 We can generate a sequence of numbers using range()
function. range(10) will generate numbers from 0 to 9 (10
numbers).
 In range function have to define the start, stop and step size
 as range(start,stop,step size). step size defaults to 1 if not
provided.
Syntax:
Looping Statements and Control Statements in Python
For in sequence
The for loop in Python is used to iterate over a
sequence (list, tuple, string).
Iterating over a sequence is called traversal.
Loop continues until we reach the last
element in the sequence.
The body of for loop is separated from the rest
of the code using indentation
Sequence can be a list, strings or
tuples:
S.No Sequences Example Output
1. For loop in string for i in "Ramu":
print(i)
R
A
M
U
2. For loop in list for i in [2,3,5,6,9]: print(i)
2
3
5
6
9
3. For loop in tuple
for i in (2,3,1): print(i) 2
3
1
1. print nos divisible by 5 not by 10
2. Program to print fibonacci series
3. Program to find factors of a given number
4. check the given number is perfect number or
not
5. check the no is prime or not
6. Print first n prime numbers
7. Program to print prime numbers in range
Fibonacci series output
a=0
b=1
n=int(input("Enter the number of terms: "))
print("Fibonacci Series: ")
print(a,b)
for i in range(1,n,1):
c=a+b
print(c)
a=b
b=c
Enter the number of terms: 6
Fibonacci Series:
0 1
1
2
3
5
8
find factors of a number Output
n=int(input("enter a number:"))
for i in range(1,n+1,1):
if(n%i==0):
print(i)
enter a number:10 1
2
5
10
Check The No Is Prime Or Not Output
n=int(input("enter a number"))
for i in range(2,n):
if(n%i==0):
print("The num is not a prime") break
else:
print("The num is a prime number.")
enter a no:7
The num is a prime number.
Check A Number Is Perfect Number Or Not Output
n=int(input("enter a number:")) sum=0
for i in range(1,n,1):
if(n%i==0):
sum=sum+i if(sum==n):
print("the number is perfect number")
else:
print("the number is not perfect number")
enter a number:6
the number is perfect number
• Python programming language allows using
one loop inside another loop.
• We can use one or more loop inside any
another while, for or do...while loop.
Looping Statements and Control Statements in Python
else in for loop:
• If else statement is used in for loop, the else
statement is executed when the loop has
reached the limit.
• The statements inside for loop and statements
inside else will also execute
example output
for i in range(1,6):
print(i)
else:
print("the number greater than6")
1
2
3
4
5 the number greater than 6
else in while loop:
• If else statement is used within while loop ,
the else part will be executed when the
condition become false.
• The statements inside for loop and statements
inside else will also execute
Program output
i=1
while(i<=5
):
print
(i)
i=i+
1
else:
print("the number greater than5")
1
2
3
4
5
the number greater than 5
Break statements can alter the flow of a loop.
It terminates the current loop and executes
the remaining statement outside the loop.
If the loop has else statement, that will also
gets terminated and come out of the loop
completely.
Syntax:
Flowchart
example Output
for i in "welcome":
if(i=="c"):
break
print(i)
w
e
l
• It terminates the current iteration and transfer
the control to the next iteration in the loop.
Syntax:
Continue
Looping Statements and Control Statements in Python
Example: Output
for i in
"welcome":
if(i=="c"):
continue
print(i)
w
e
l
o
m
e
It is used when a statement is required
syntactically but you don’t want any code to
execute.
It is a null statement, nothing happens when it is
executed.
Syntax:
pass
break
Example Output
for i in “welcome”:
if (i == “c”):
pass
print(i)
w
e
l
c
o
m
e
Break Continue
It terminates the current loop and
executes the remaining statement outside
the loop.
It terminates the current iteration and
transfer the control to the next iteration
in
the loop.
syntax:
break
syntax:
continue
for i in"welcome":
if(i=="c"):
break
print(i)
for i in
"welcome":
if(i=="c"):
continue
print(i)
w
e l
w
e
l
m
e

More Related Content

PDF
140+ Basic Python Programs This resource can assist you in preparing for your...
PPTX
Looping statement in python
PPT
Algoritmos
PDF
Strings in python
PPTX
Mathematics 7: Angles (naming, types and how to measure them)
PPT
Interact with customers
PDF
Python final ppt
PDF
Python programming : Classes objects
140+ Basic Python Programs This resource can assist you in preparing for your...
Looping statement in python
Algoritmos
Strings in python
Mathematics 7: Angles (naming, types and how to measure them)
Interact with customers
Python final ppt
Python programming : Classes objects

What's hot (20)

PDF
Python programming : Control statements
PDF
Control Structures in Python
PPTX
Python Functions
PPTX
Functions in python
PPTX
Python for loop
PPSX
Modules and packages in python
PPTX
Data Structures in Python
PPTX
Conditionalstatement
PDF
Python list
PDF
Python recursion
PDF
Python Flow Control
PDF
Python tuple
PPTX
Variables in python
PDF
Arrays in python
PPTX
Regular expressions in Python
PDF
Python basic
PPTX
Loops in Python
PPTX
Python variables and data types.pptx
PDF
Datatypes in python
Python programming : Control statements
Control Structures in Python
Python Functions
Functions in python
Python for loop
Modules and packages in python
Data Structures in Python
Conditionalstatement
Python list
Python recursion
Python Flow Control
Python tuple
Variables in python
Arrays in python
Regular expressions in Python
Python basic
Loops in Python
Python variables and data types.pptx
Datatypes in python
Ad

Similar to Looping Statements and Control Statements in Python (20)

PDF
Python Decision Making And Loops.pdf
PPTX
Python notes for students to learn and develop
PPTX
This is all about control flow in python intruducing the Break and Continue.pptx
PPT
Control structures pyhton
PPTX
While_for_loop presententationin first year students
PDF
While-For-loop in python used in college
PPTX
Chapter08.pptx
PPTX
Python if_else_loop_Control_Flow_Statement
PPTX
Chapter 2-Python and control flow statement.pptx
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
PPTX
Unit - 2 CAP.pptx
PPTX
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PPTX
Introduction to Python Part-1
PPTX
loopin gstatement in python using .pptx
PDF
Python_Module_2.pdf
PPTX
Python Session - 4
PDF
loopingstatementinpython-210628184047 (1).pdf
PDF
Python Loop
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
Python Decision Making And Loops.pdf
Python notes for students to learn and develop
This is all about control flow in python intruducing the Break and Continue.pptx
Control structures pyhton
While_for_loop presententationin first year students
While-For-loop in python used in college
Chapter08.pptx
Python if_else_loop_Control_Flow_Statement
Chapter 2-Python and control flow statement.pptx
Unit 1- Part-2-Control and Loop Statements.pdf
Unit - 2 CAP.pptx
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Introduction to Python Part-1
loopin gstatement in python using .pptx
Python_Module_2.pdf
Python Session - 4
loopingstatementinpython-210628184047 (1).pdf
Python Loop
Chapter 9 Conditional and Iterative Statements.pptx
Ad

Recently uploaded (20)

PPT
High Data Link Control Protocol in Data Link Layer
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
International Journal of Information Technology Convergence and Services (IJI...
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
Soil science - sampling procedures for soil science lab
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Glazing at Facade, functions, types of glazing
PPTX
Chapter----five---Resource Recovery.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
TE-AI-Unit VI notes using planning model
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
flutter Launcher Icons, Splash Screens & Fonts
PPTX
AgentX UiPath Community Webinar series - Delhi
PPT
Ppt for engineering students application on field effect
PPTX
Ship’s Structural Components.pptx 7.7 Mb
PPT
SCOPE_~1- technology of green house and poyhouse
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
High Data Link Control Protocol in Data Link Layer
anatomy of limbus and anterior chamber .pptx
International Journal of Information Technology Convergence and Services (IJI...
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Soil science - sampling procedures for soil science lab
ETO & MEO Certificate of Competency Questions and Answers
Glazing at Facade, functions, types of glazing
Chapter----five---Resource Recovery.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
TE-AI-Unit VI notes using planning model
Strings in CPP - Strings in C++ are sequences of characters used to store and...
flutter Launcher Icons, Splash Screens & Fonts
AgentX UiPath Community Webinar series - Delhi
Ppt for engineering students application on field effect
Ship’s Structural Components.pptx 7.7 Mb
SCOPE_~1- technology of green house and poyhouse
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Chapter 6 Design in software Engineeing.ppt
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx

Looping Statements and Control Statements in Python

  • 1. LOOPING STATEMENTS AND CONTROL STATEMENTS IN PYTHON Ms.C.PRIYANKA AP/CSE KIT-KALAIGNARKARUNANIDHI INSTITUTE OF TECHNOLOGY , COIMBATORE
  • 4. While loop statement in Python is used to repeatedly executes set of statement as long as a given condition is true.  In while loop, test expression is checked first.
  • 5. The body of the loop is entered only if the test_expression is True. After one iteration, the test expression is checked again.  This process continues until the test_expression evaluates to False. In Python, the body of the while loop is determined through indentation. The statements inside the while starts with indentation and the first unindented line marks the end.
  • 8. 1. Program to find sum of n numbers: 2. Program to find factorial of a number 3. Program to find sum of digits of a number: 4. Program to Reverse the given number: 5. Program to find number is Armstrong number or not 6. Program to check the number is palindrome or not
  • 9. n=eval(input("enter n")) i=1 sum=0 while(i<=n): sum=sum+i i=i+1 print(sum) enter n 10 55 Factorial of a numbers: output n=int(input("enter n")) i=1 fact=1 while(i<=n): fact=fact*i i=i+1 print(fact) enter n 5 120 Sum of digits of anumber: output n= int (input("enter anumber")) sum=0 while(n>0): a=n%10 sum=sum+a n=n//10 print(sum) enter a number 123 6
  • 10. Reverse the given number: output n= int (input("enter a number")) sum=0 while(n>0): a=n%10 sum=sum*10+a n=n//10 print(sum) enter a number 123 321 Armstrong number ornot output n=eval(input("enter a number")) enter a number153 org=n The given number is Armstrong number sum=0 while(n>0): a=n%10 sum=sum+a*a*a n=n//10 if(sum==org): print("The given number is Armstrong number") else: print("The given number is not Armstrong number")
  • 11. Palindrome or not output n=eval(input("enter a number")) enter a number121 org=n The given no is palindrome sum=0 while(n>0): a=n%10 sum=sum*10+a n=n //10 if(sum==org): print("The given no is palindrome") else: print("The given no is not palindrome")
  • 12. for in range:  We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers).  In range function have to define the start, stop and step size  as range(start,stop,step size). step size defaults to 1 if not provided. Syntax:
  • 14. For in sequence The for loop in Python is used to iterate over a sequence (list, tuple, string). Iterating over a sequence is called traversal. Loop continues until we reach the last element in the sequence. The body of for loop is separated from the rest of the code using indentation
  • 15. Sequence can be a list, strings or tuples: S.No Sequences Example Output 1. For loop in string for i in "Ramu": print(i) R A M U 2. For loop in list for i in [2,3,5,6,9]: print(i) 2 3 5 6 9 3. For loop in tuple for i in (2,3,1): print(i) 2 3 1
  • 16. 1. print nos divisible by 5 not by 10 2. Program to print fibonacci series 3. Program to find factors of a given number 4. check the given number is perfect number or not 5. check the no is prime or not 6. Print first n prime numbers 7. Program to print prime numbers in range
  • 17. Fibonacci series output a=0 b=1 n=int(input("Enter the number of terms: ")) print("Fibonacci Series: ") print(a,b) for i in range(1,n,1): c=a+b print(c) a=b b=c Enter the number of terms: 6 Fibonacci Series: 0 1 1 2 3 5 8 find factors of a number Output n=int(input("enter a number:")) for i in range(1,n+1,1): if(n%i==0): print(i) enter a number:10 1 2 5 10
  • 18. Check The No Is Prime Or Not Output n=int(input("enter a number")) for i in range(2,n): if(n%i==0): print("The num is not a prime") break else: print("The num is a prime number.") enter a no:7 The num is a prime number. Check A Number Is Perfect Number Or Not Output n=int(input("enter a number:")) sum=0 for i in range(1,n,1): if(n%i==0): sum=sum+i if(sum==n): print("the number is perfect number") else: print("the number is not perfect number") enter a number:6 the number is perfect number
  • 19. • Python programming language allows using one loop inside another loop. • We can use one or more loop inside any another while, for or do...while loop.
  • 21. else in for loop: • If else statement is used in for loop, the else statement is executed when the loop has reached the limit. • The statements inside for loop and statements inside else will also execute
  • 22. example output for i in range(1,6): print(i) else: print("the number greater than6") 1 2 3 4 5 the number greater than 6
  • 23. else in while loop: • If else statement is used within while loop , the else part will be executed when the condition become false. • The statements inside for loop and statements inside else will also execute
  • 24. Program output i=1 while(i<=5 ): print (i) i=i+ 1 else: print("the number greater than5") 1 2 3 4 5 the number greater than 5
  • 25. Break statements can alter the flow of a loop. It terminates the current loop and executes the remaining statement outside the loop. If the loop has else statement, that will also gets terminated and come out of the loop completely.
  • 27. Flowchart example Output for i in "welcome": if(i=="c"): break print(i) w e l
  • 28. • It terminates the current iteration and transfer the control to the next iteration in the loop. Syntax: Continue
  • 30. Example: Output for i in "welcome": if(i=="c"): continue print(i) w e l o m e
  • 31. It is used when a statement is required syntactically but you don’t want any code to execute. It is a null statement, nothing happens when it is executed. Syntax: pass break
  • 32. Example Output for i in “welcome”: if (i == “c”): pass print(i) w e l c o m e
  • 33. Break Continue It terminates the current loop and executes the remaining statement outside the loop. It terminates the current iteration and transfer the control to the next iteration in the loop. syntax: break syntax: continue for i in"welcome": if(i=="c"): break print(i) for i in "welcome": if(i=="c"): continue print(i) w e l w e l m e