SlideShare a Scribd company logo
1 of 33
LOOPING STATEMENTS AND
CONTROL STATEMENTS IN PYTHON
Ms.C.PRIYANKA
AP/CSE
KIT-KALAIGNARKARUNANIDHI INSTITUTE OF
TECHNOLOGY ,
COIMBATORE
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
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:
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.
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
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

What's hot (20)

Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
python Function
python Function python Function
python Function
 
Python basics
Python basicsPython basics
Python basics
 
List in Python
List in PythonList in Python
List in Python
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python strings
Python stringsPython strings
Python strings
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 

Similar to Looping Statements and Control Statements in Python

Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxelezearrepil1
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementAbhishekGupta692777
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopPriyom Majumder
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1Devashish Kumar
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxXhelalSpahiu
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxadihartanto7
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in pythonnitamhaske
 

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

Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptx
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
Chapter08.pptx
Chapter08.pptxChapter08.pptx
Chapter08.pptx
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
 
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
Python Loop
Python LoopPython Loop
Python Loop
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Slide 6_Control Structures.pdf
Slide 6_Control Structures.pdfSlide 6_Control Structures.pdf
Slide 6_Control Structures.pdf
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
Loops in c
Loops in cLoops in c
Loops in c
 

Recently uploaded

CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 

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
  • 2.
  • 3.
  • 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.
  • 7.
  • 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:
  • 13.
  • 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.
  • 20.
  • 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
  • 29.
  • 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