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

PPTX
Operators and expressions in c language
PDF
Object oriented programming c++
PPT
Strings
PPTX
Operators in Python
PPT
Variables in C Programming
PPTX
Two dimensional arrays
PPT
Structure of a C program
PPTX
Python Functions
Operators and expressions in c language
Object oriented programming c++
Strings
Operators in Python
Variables in C Programming
Two dimensional arrays
Structure of a C program
Python Functions

What's hot (20)

PPT
RECURSION IN C
PPTX
Data types in java
PPT
File handling in c
PPTX
User defined functions in C
PPTX
Functions in c language
PPTX
PPTX
Looping Statements and Control Statements in Python
PPTX
Constructor and Types of Constructors
PDF
Python Flow Control
PDF
Python functions
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PPTX
Call by value or call by reference in C++
PPTX
07. Virtual Functions
PPTX
C++ string
PDF
10. switch case
PPTX
Programming in c Arrays
PPTX
Decision making statements in C programming
PPTX
Loops in C Programming Language
RECURSION IN C
Data types in java
File handling in c
User defined functions in C
Functions in c language
Looping Statements and Control Statements in Python
Constructor and Types of Constructors
Python Flow Control
Python functions
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Call by value or call by reference in C++
07. Virtual Functions
C++ string
10. switch case
Programming in c Arrays
Decision making statements in C programming
Loops in C Programming Language
Ad

Similar to Loops in Python (20)

PDF
xii cs practicals class 12 computer science.pdf
PPTX
Csci101 lect02 selection_andlooping
PDF
Numerical analysis
PDF
III MCS python lab (1).pdf
PPTX
lecture 2.pptx
PPTX
PPt Revision of the basics of python1.pptx
PDF
introduction to python programming course 2
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
PPTX
made it easy: python quick reference for beginners
PDF
678676286-CLASS-12-COMPUTER-SCIENCE-PRACTICAL-FILE-2023-24.pdf
PPTX
Introduction-to-Iteration (2).pptx
PDF
python practicals-solution-2019-20-class-xii.pdf
PDF
Xi CBSE Computer Science lab programs
PPTX
Python notes for students to learn and develop
PPT
Functions and pointers_unit_4
PPTX
Keep it Stupidly Simple Introduce Python
PDF
Problem solving using computers - Unit 1 - Study material
PDF
CLISP Lab Manual - Dr.J.VijiPriya
DOC
Programs.doc
xii cs practicals class 12 computer science.pdf
Csci101 lect02 selection_andlooping
Numerical analysis
III MCS python lab (1).pdf
lecture 2.pptx
PPt Revision of the basics of python1.pptx
introduction to python programming course 2
TN 12 computer Science - ppt CHAPTER-6.pptx
made it easy: python quick reference for beginners
678676286-CLASS-12-COMPUTER-SCIENCE-PRACTICAL-FILE-2023-24.pdf
Introduction-to-Iteration (2).pptx
python practicals-solution-2019-20-class-xii.pdf
Xi CBSE Computer Science lab programs
Python notes for students to learn and develop
Functions and pointers_unit_4
Keep it Stupidly Simple Introduce Python
Problem solving using computers - Unit 1 - Study material
CLISP Lab Manual - Dr.J.VijiPriya
Programs.doc
Ad

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
DOCX
573137875-Attendance-Management-System-original
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
PPT on Performance Review to get promotions
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
additive manufacturing of ss316l using mig welding
PPT
Mechanical Engineering MATERIALS Selection
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
573137875-Attendance-Management-System-original
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CH1 Production IntroductoryConcepts.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Construction Project Organization Group 2.pptx
PPT on Performance Review to get promotions
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
R24 SURVEYING LAB MANUAL for civil enggi
UNIT-1 - COAL BASED THERMAL POWER PLANTS
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Embodied AI: Ushering in the Next Era of Intelligent Systems
additive manufacturing of ss316l using mig welding
Mechanical Engineering MATERIALS Selection
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

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.