SlideShare a Scribd company logo
Presented by
A.Arockia Abins
Department of CSE
Two modes
1.Interactive Mode
2.Script Mode
Interactive Mode
It is a command line which gives immediate result for
each statement.
Ex:
Script Mode
A script mode can store code in a file and it uses the
interpreter to execute the contents of file.
Ex:
Solving a problem
Input
Process
Output
Ex: Addition of two numbers
Input: x=10 y=5
Process: z = x + y
Output: z=15
Input / Output statements:
Input
input() function is used to get the input from the
user.
Ex:
Output
print() function is used to display the result to the
user.
Ex:
Input / Output statements:
Ex: addition of two numbers
x=int(input("Enter 1st no:"))
y=int(input("Enter 2nd no:"))
z=x+y;
print("Sum is ",z)
Q:write a program to find area of circle
Program:
r=float(input("Enter a radius:"))
a=3.14*r*r
print("Area of circle is ",a)
Q:Write a program to convert
Celsius to Fahrenheit
Program:
c=float(input("Enter a celsius value:"))
f=1.8*c+32
print("Fahrenheit value is: ",f)
Solve the following:
•Find the Error:
1)>>>1+’2’+3+’4’
2)>>>’15’*’95’
•Find the Output:
1)>>>4*’2’
2)>>>x=“hello,”
>>>x+=“world!”
>>>print(x)
Control flow statements
Decision Control
Statements
Sequence
control
statements
Selection
control
statements
Iterative control
statements
Sequence control statements
• In this case program is executed
sequentially from the first line to last line.
Example:
//addition of two numbers
x=int(input("Enter 1st no:"))
y=int(input("Enter 2nd no:"))
z=x+y;
print("Sum is ",z)
Selection control statements
• Execute only a selected set of statements
Example:
//find the greatest of two numbers
x=int(input("Enter 1st no:"))
y=int(input("Enter 2nd no:"))
if x>y:
print(x, "is greatest number") //Note:Indentation
else:
print(y, "is greatest number")
Q:write a program to find whether the given
number is even or odd.
Program:
x=int(input("Enter a no:"))
if x%2==0:
print(x, "is even number")
else:
print(x, "is odd number")
Q:write a program to find the greatest number
from three numbers.
Program:
y=int(input("Enter 2nd no:"))
z=int(input("Enter 3rd no:"))
if x>y:
if x>z:
print(x, "is a greatest number")
else:
if y>z:
print(y, "is a greatest number")
else:
print(z, "is a greatest number")
Iterative control statements
or
Loop Structures
• Iterative statements are decision control
statements that are used to repeat the
execution of a list of statements.
• Two types:
• while loop
• for loop
while Loop
-execute one or more statements while a particular
condition is True.
Syntax
.
while(condition): .
Body of the loop
Key points:
• Initialization
• Condition
• Increment / decrement
Body of The loop
condition
False
True
while loop
Example:
//Program to print first 10 numbers using
a while loop
i=0
while(i<=10):
print(i)
i=i+1
while loop
Example:
//Program to print first 10 numbers using
a while loop
i=0 //Initialization
while(i<=10): //Condition
print(i)
i=i+1 //Increment
Q:write a program to find the sum of N natural
numbers.
Input:5
Process:1+2+3+4+5
Output:15
Q:write a program to find the sum of N natural
numbers.
Program:
x=int(input("Enter a number:"))
i=0
s=0
while(i<=x):
s=s+i;
i=i+1
print("Sum is ",s)
Q:write a program to find the factorial of a given
number.
Input:5
Process:1*2*3*4*5
Output:120
Q:write a program to find the factorial of a given
number.
x=int(input("Enter a number:"))
i=1
f=1
while(i<=x):
f=f*i;
i=i+1
print("Factorial is ",f)
for loop
Syntax:
for variable in sequence:
statements block
Variable – used to store a value
Sequence – string , collection of integers or
range()
for loop
Example:
for i in 1,2,3,4,5:
print(i)
range() function
• It is a built-in function in python
that is used to iterate over a
sequence of numbers.
• Syntax:
• range(beg, end, step)
range() function
• Syntax:
• range(beg, end, step)
• Example:
• for i in range(1,11):
• print(i,end=" ")
Print of N natural numbers
while
•i=0
•while(i<6):
• print(i)
• i=i+1
for
•for i in range(6):
• print(i)
Q:write a program to find the sum of N natural
numbers.
Program:
s=0
for i in range(1,11):
s=s+i
print("Sum is",s)
Q:write a program to find the factorial of a given
number.
f=1
for i in range(1,6):
f=f*i
print("Factorial is",f)
Q:write a program to print the multiplication
table of n, where n is entered by user.
Input:
Enter any number:5
Output:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
…
10 * 5 = 50
Q:write a program to print the multiplication
table of n, where n is entered by user.
n=int(input("Enter any number:"))
print("Multiplication table of", n)
print("*********************")
for i in range(1,11):
print(i,"*",n,"=",i*n)
Looping through an iterable:
for i in "welcome":
print(i);
names=["abc","def","ghi"]
for i in names:
print(i);
numbers=[10,25,7,46,73,16,49]
for i in numbers:
print(i);
Programming Problems
1) Write a program that prints numbers from 20 to 1.
2) Write a program to print the sum of all odd numbers from 1
to 100.
3) Write a program to generate Fibonacci series of N terms.
33
THANK YOU

More Related Content

What's hot (20)

PDF
Arrays in python
moazamali28
 
PPTX
Datastructures in python
hydpy
 
PPTX
Control statements in c
Sathish Narayanan
 
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
PPTX
Functions in Python
Kamal Acharya
 
PPT
Arrays
SARITHA REDDY
 
PPTX
Python
Shivam Gupta
 
PPTX
Python for loop
Aishwarya Deshmukh
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPT
Python Control structures
Siddique Ibrahim
 
PDF
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
Arrays in C language
Shubham Sharma
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
PDF
Applications of stack
eShikshak
 
PDF
Python Decision Making
Soba Arjun
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPTX
Python in 30 minutes!
Fariz Darari
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Arrays in python
moazamali28
 
Datastructures in python
hydpy
 
Control statements in c
Sathish Narayanan
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
Functions in Python
Kamal Acharya
 
Arrays
SARITHA REDDY
 
Python
Shivam Gupta
 
Python for loop
Aishwarya Deshmukh
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Python Control structures
Siddique Ibrahim
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
Arrays in C language
Shubham Sharma
 
Tuple in python
Sharath Ankrajegowda
 
Applications of stack
eShikshak
 
Python Decision Making
Soba Arjun
 
Dynamic memory allocation in c
lavanya marichamy
 
Python in 30 minutes!
Fariz Darari
 
Python Modules
Nitin Reddy Katkam
 
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 

Similar to Loops in Python (20)

PPTX
loops _
SwatiHans10
 
PDF
ppt python notes list tuple data types ope
SukhpreetSingh519414
 
PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PDF
Python_Module_2.pdf
R.K.College of engg & Tech
 
PPTX
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
PDF
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
PDF
python 34💭.pdf
AkashdeepBhattacharj1
 
PDF
python notes.pdf
RohitSindhu10
 
PPTX
made it easy: python quick reference for beginners
SumanMadan4
 
PPTX
Python programming workshop session 2
Abdul Haseeb
 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
PDF
While-For-loop in python used in college
ssuser7a7cd61
 
PPTX
While_for_loop presententationin first year students
SIHIGOPAL
 
PPTX
Python programming –part 3
Megha V
 
PPTX
python ppt.pptx
ssuserd10678
 
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
maheshnanda14
 
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
PPTX
PYTHON PROGRAMMING
indupps
 
PPTX
Python programing
hamzagame
 
loops _
SwatiHans10
 
ppt python notes list tuple data types ope
SukhpreetSingh519414
 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Python_Module_2.pdf
R.K.College of engg & Tech
 
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python 34💭.pdf
AkashdeepBhattacharj1
 
python notes.pdf
RohitSindhu10
 
made it easy: python quick reference for beginners
SumanMadan4
 
Python programming workshop session 2
Abdul Haseeb
 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
While-For-loop in python used in college
ssuser7a7cd61
 
While_for_loop presententationin first year students
SIHIGOPAL
 
Python programming –part 3
Megha V
 
python ppt.pptx
ssuserd10678
 
Chapter 9 Conditional and Iterative Statements.pptx
maheshnanda14
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
PYTHON PROGRAMMING
indupps
 
Python programing
hamzagame
 
Ad

Recently uploaded (20)

PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Distribution reservoir and service storage pptx
dhanashree78
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Ad

Loops in Python

  • 2. Two modes 1.Interactive Mode 2.Script Mode Interactive Mode It is a command line which gives immediate result for each statement. Ex: Script Mode A script mode can store code in a file and it uses the interpreter to execute the contents of file. Ex:
  • 3. Solving a problem Input Process Output Ex: Addition of two numbers Input: x=10 y=5 Process: z = x + y Output: z=15
  • 4. Input / Output statements: Input input() function is used to get the input from the user. Ex: Output print() function is used to display the result to the user. Ex:
  • 5. Input / Output statements: Ex: addition of two numbers x=int(input("Enter 1st no:")) y=int(input("Enter 2nd no:")) z=x+y; print("Sum is ",z)
  • 6. Q:write a program to find area of circle Program: r=float(input("Enter a radius:")) a=3.14*r*r print("Area of circle is ",a)
  • 7. Q:Write a program to convert Celsius to Fahrenheit Program: c=float(input("Enter a celsius value:")) f=1.8*c+32 print("Fahrenheit value is: ",f)
  • 8. Solve the following: •Find the Error: 1)>>>1+’2’+3+’4’ 2)>>>’15’*’95’ •Find the Output: 1)>>>4*’2’ 2)>>>x=“hello,” >>>x+=“world!” >>>print(x)
  • 9. Control flow statements Decision Control Statements Sequence control statements Selection control statements Iterative control statements
  • 10. Sequence control statements • In this case program is executed sequentially from the first line to last line. Example: //addition of two numbers x=int(input("Enter 1st no:")) y=int(input("Enter 2nd no:")) z=x+y; print("Sum is ",z)
  • 11. Selection control statements • Execute only a selected set of statements Example: //find the greatest of two numbers x=int(input("Enter 1st no:")) y=int(input("Enter 2nd no:")) if x>y: print(x, "is greatest number") //Note:Indentation else: print(y, "is greatest number")
  • 12. Q:write a program to find whether the given number is even or odd. Program: x=int(input("Enter a no:")) if x%2==0: print(x, "is even number") else: print(x, "is odd number")
  • 13. Q:write a program to find the greatest number from three numbers. Program: y=int(input("Enter 2nd no:")) z=int(input("Enter 3rd no:")) if x>y: if x>z: print(x, "is a greatest number") else: if y>z: print(y, "is a greatest number") else: print(z, "is a greatest number")
  • 14. Iterative control statements or Loop Structures • Iterative statements are decision control statements that are used to repeat the execution of a list of statements. • Two types: • while loop • for loop
  • 15. while Loop -execute one or more statements while a particular condition is True. Syntax . while(condition): . Body of the loop Key points: • Initialization • Condition • Increment / decrement Body of The loop condition False True
  • 16. while loop Example: //Program to print first 10 numbers using a while loop i=0 while(i<=10): print(i) i=i+1
  • 17. while loop Example: //Program to print first 10 numbers using a while loop i=0 //Initialization while(i<=10): //Condition print(i) i=i+1 //Increment
  • 18. Q:write a program to find the sum of N natural numbers. Input:5 Process:1+2+3+4+5 Output:15
  • 19. Q:write a program to find the sum of N natural numbers. Program: x=int(input("Enter a number:")) i=0 s=0 while(i<=x): s=s+i; i=i+1 print("Sum is ",s)
  • 20. Q:write a program to find the factorial of a given number. Input:5 Process:1*2*3*4*5 Output:120
  • 21. Q:write a program to find the factorial of a given number. x=int(input("Enter a number:")) i=1 f=1 while(i<=x): f=f*i; i=i+1 print("Factorial is ",f)
  • 22. for loop Syntax: for variable in sequence: statements block Variable – used to store a value Sequence – string , collection of integers or range()
  • 23. for loop Example: for i in 1,2,3,4,5: print(i)
  • 24. range() function • It is a built-in function in python that is used to iterate over a sequence of numbers. • Syntax: • range(beg, end, step)
  • 25. range() function • Syntax: • range(beg, end, step) • Example: • for i in range(1,11): • print(i,end=" ")
  • 26. Print of N natural numbers while •i=0 •while(i<6): • print(i) • i=i+1 for •for i in range(6): • print(i)
  • 27. Q:write a program to find the sum of N natural numbers. Program: s=0 for i in range(1,11): s=s+i print("Sum is",s)
  • 28. Q:write a program to find the factorial of a given number. f=1 for i in range(1,6): f=f*i print("Factorial is",f)
  • 29. Q:write a program to print the multiplication table of n, where n is entered by user. Input: Enter any number:5 Output: 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 … 10 * 5 = 50
  • 30. Q:write a program to print the multiplication table of n, where n is entered by user. n=int(input("Enter any number:")) print("Multiplication table of", n) print("*********************") for i in range(1,11): print(i,"*",n,"=",i*n)
  • 31. Looping through an iterable: for i in "welcome": print(i); names=["abc","def","ghi"] for i in names: print(i); numbers=[10,25,7,46,73,16,49] for i in numbers: print(i);
  • 32. Programming Problems 1) Write a program that prints numbers from 20 to 1. 2) Write a program to print the sum of all odd numbers from 1 to 100. 3) Write a program to generate Fibonacci series of N terms.