SlideShare a Scribd company logo
13
Most read
16
Most read
17
Most read
Python Control Statements
Indentation in Python
Indentation in Python
• In Python, indentation is used to declare a block. If two
statements are at the same indentation level, then they are
the part of the same block.
• For the ease of programming and to achieve simplicity, python
doesn't allow the use of curly braces or parentheses for the
block level code.
• Indentation is the most used part of the python programming
language.
• Generally, a tab space or four spaces are given to indent the
statements in python.
Conditional Statements in Python
Conditional Statements in Python
• Conditional Statements performs different computations or
actions depending on conditions.
• In python, the following are conditional statements
o if
o if –else
o if – elif –else
If statement:
• The if statement is used to test a specific condition. If the
condition is true, a block of code (if-block) will be executed.
Syntax:
if condition:
statement1
statement2
Conditional Statements in Python Cont..
Example: ifdemo.py
a = 33
b = 200
if b > a:
print ("b is greater than a")
print ("done…")
Remember:
input () function is used to get input from user.
Example:
a=input (“Enter a value”)
Output:
python ifdemo.py
b is greater than a
done…
Conditional Statements in Python Cont..
If-else statement:
• The if-else statement provides an else block combined with the if
statement which is executed in the false case of the condition.
Syntax:
if condition:
#block of statements
else:
#another block of statements (else-block)
Example: ifelsedemo.py
age = int(input("Enter your age : "))
if age>=18:
print("You are eligible to vote !!")
else:
print("Sorry! you have to wait !!"))
Output:
python ifelsedemo.py
Enter your age: 19
You are eligible to vote!!
Conditional Statements in Python Cont..
If-elif-else statement:
• The elif statement enables us to check multiple conditions and
execute the specific block of statements depending upon the true
condition among them.
Syntax:
if condition1:
# block of statements
elif condition2:
# block of statements
elif condition3:
# block of statements
else:
# block of statements
Conditional Statements in Python Cont..
Example: maxnum.py
a=int(input("Enter a value : "))
b=int(input("Enter b value : "))
c=int(input("Enter c value : "))
if (a>b) and (a>c):
print("Maximum value is :",a)
elif (b>a) and (b>c):
print("Maximum value is :",b)
else:
print("Maximum value is :",c)
Output:
python maxnum.py
Enter a value: 10
Enter b value: 14
Enter c value: 9
Maximum value is: 14
Loop Statements in Python
Loop Statements in Python
• Sometimes we may need to alter the flow of the program. If the
execution of a specific code may need to be repeated several
numbers of times then we can go for loop statements.
• In python, the following are loop statements
o while loop
o for loop
while loop:
• With the while loop we can execute a set of statements as long
as a condition is true. The while loop is mostly used in the case
where the number of iterations is not known in advance.
Syntax:
while expression:
Statement(s)
Loop Statements in Python Cont..
Using else with while loop
• Python enables us to use the while loop with the else block
also. The else block is executed when the condition given in
the while statement becomes false.
Example: whiledemo.py
i=1;
while i<=3:
print(i);
i=i+1;
Output:
python whiledemo.py
1
2
3
Example: wedemo.py
i=1;
while i<=3:
print(i);
i=i+1;
else:
print("while loop terminated")
Output:
python wedemo.py
1
2
3
while loop terminated
Loop Statements in Python Cont..
for loop:
• The for loop in Python is used to iterate the statements or a
part of the program several times. It is frequently used to
traverse the data structures like list, tuple, or dictionary.
Syntax:
for iterating_var in sequence:
statement(s)
Example: fordemo.py
i=1
n=int(input("Enter n value : "))
for i in range(i,n+1):
print(i,end = ' ')
Output:
python fordemo.py
Enter n value: 5
1 2 3 4 5
Loop Statements in Python Cont..
Using else with for loop
• Python allows us to use the else statement with the for loop
which can be executed only when all the iterations are
exhausted.
• Here, we must notice that if the loop contains any of the
break statement then the else statement will not be executed.
Example: fedemo.py
for i in range(1,5):
print(i,end=' ')
else:
print("for loop completely exhausted");
Output:
python fedemo.py
1 2 3 4
for loop completely exhausted
Jump Statements in Python
Jump Statements in Python
• Jump statements in python are used to alter the flow of a loop
like you want to skip a part of a loop or terminate a loop.
• In python, the following are jump statements
o break
o continue
break:
• The break is a keyword in python which is used to bring the
program control out of the loop.
• The break statement breaks the loops one by one, i.e., in the
case of nested loops, it breaks the inner loop first and then
proceeds to outer loops.
• The break is commonly used in the cases where we need to
break the loop for a given condition.
Syntax: break
Jump Statements in Python Cont..
continue:
• The continue statement in python is used to bring the program
control to the beginning of the loop.
• The continue statement skips the remaining lines of code inside
the loop and start with the next iteration.
• It is mainly used for a particular condition inside the loop so
that we can skip some specific code for a particular condition.
Syntax: continue
Example: breakdemo.py
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Output:
python breakdemo.py
1
2
3
Jump Statements in Python Cont..
Example: continuedemo.py
str =input("Enter any String : ")
for i in str:
if i == 'h':
continue;
print(i,end=" ");
Output:
python continuedemo.py
Enter any String : python
p y t o n

More Related Content

PPTX
Full Python in 20 slides
rfojdar
 
PPTX
Python Flow Control
Kamal Acharya
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
PDF
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
Python
Sangita Panchal
 
PDF
Python basic
Saifuddin Kaijar
 
Full Python in 20 slides
rfojdar
 
Python Flow Control
Kamal Acharya
 
Python functions
Prof. Dr. K. Adisesha
 
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
Functions in Python
Kamal Acharya
 
Python basic
Saifuddin Kaijar
 

What's hot (20)

PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PPTX
Loops in Python
Arockia Abins
 
PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PDF
Function in Python
Yashdev Hada
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
Function arguments In Python
Amit Upadhyay
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Python ppt
Anush verma
 
PPTX
Chapter 16 Dictionaries
Praveen M Jigajinni
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Generators In Python
Simplilearn
 
PPTX
Variables in python
Jaya Kumari
 
PPTX
Python Data-Types
Akhil Kaushik
 
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
PDF
Python unit 3 m.sc cs
KALAISELVI P
 
PDF
Chapter2 Encapsulation (Java)
Dyah Fajar Nur Rohmah
 
PDF
06. operator overloading
Haresh Jaiswal
 
PDF
Introduction To Python | Edureka
Edureka!
 
Looping Statements and Control Statements in Python
PriyankaC44
 
Loops in Python
Arockia Abins
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Function in Python
Yashdev Hada
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Function arguments In Python
Amit Upadhyay
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Python ppt
Anush verma
 
Chapter 16 Dictionaries
Praveen M Jigajinni
 
9. Input Output in java
Nilesh Dalvi
 
Generators In Python
Simplilearn
 
Variables in python
Jaya Kumari
 
Python Data-Types
Akhil Kaushik
 
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Python unit 3 m.sc cs
KALAISELVI P
 
Chapter2 Encapsulation (Java)
Dyah Fajar Nur Rohmah
 
06. operator overloading
Haresh Jaiswal
 
Introduction To Python | Edureka
Edureka!
 
Ad

Similar to Control_Statements_in_Python.pptx (20)

PPTX
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
PDF
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
PPT
Control structures pyhton
Prakash Jayaraman
 
PDF
basic of desicion control statement in python
nitamhaske
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPTX
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 
PPTX
Python Flow Control & use of functions.pptx
pandyahm47
 
PDF
Python unit 2 M.sc cs
KALAISELVI P
 
PPTX
Lecture on Fundamentals of Python Programming-2
JannatulFerdouse15
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PDF
if else python.pdf
Gshs6
 
PPTX
Conditional and control statement
narmadhakin
 
DOCX
controlstatementspy.docx
manohar25689
 
PPTX
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
PPTX
Conditional Statements.pptx
Gautam623648
 
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
Control structures pyhton
Prakash Jayaraman
 
basic of desicion control statement in python
nitamhaske
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 
Python Flow Control & use of functions.pptx
pandyahm47
 
Python unit 2 M.sc cs
KALAISELVI P
 
Lecture on Fundamentals of Python Programming-2
JannatulFerdouse15
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
if else python.pdf
Gshs6
 
Conditional and control statement
narmadhakin
 
controlstatementspy.docx
manohar25689
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
Conditional Statements.pptx
Gautam623648
 
Ad

More from Koteswari Kasireddy (20)

PPTX
DA Syllabus outline (2).pptx
Koteswari Kasireddy
 
PDF
Chapter-7-Sampling & sampling Distributions.pdf
Koteswari Kasireddy
 
PDF
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
PDF
unit-3_Chapter1_RDRA.pdf
Koteswari Kasireddy
 
PDF
DBMS_UNIT_1.pdf
Koteswari Kasireddy
 
PDF
business analytics
Koteswari Kasireddy
 
PPTX
Relational Model and Relational Algebra.pptx
Koteswari Kasireddy
 
PPTX
CHAPTER -12 it.pptx
Koteswari Kasireddy
 
PPTX
WEB_DATABASE_chapter_4.pptx
Koteswari Kasireddy
 
PPTX
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Koteswari Kasireddy
 
PPTX
Database System Concepts AND architecture [Autosaved].pptx
Koteswari Kasireddy
 
PPTX
Evolution Of WEB_students.pptx
Koteswari Kasireddy
 
PPTX
Presentation1.pptx
Koteswari Kasireddy
 
PPTX
Algorithm.pptx
Koteswari Kasireddy
 
PPTX
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
PPTX
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
PPTX
linked_list.pptx
Koteswari Kasireddy
 
PPTX
matrices_and_loops.pptx
Koteswari Kasireddy
 
PPTX
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
PPTX
Control_Statements.pptx
Koteswari Kasireddy
 
DA Syllabus outline (2).pptx
Koteswari Kasireddy
 
Chapter-7-Sampling & sampling Distributions.pdf
Koteswari Kasireddy
 
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
unit-3_Chapter1_RDRA.pdf
Koteswari Kasireddy
 
DBMS_UNIT_1.pdf
Koteswari Kasireddy
 
business analytics
Koteswari Kasireddy
 
Relational Model and Relational Algebra.pptx
Koteswari Kasireddy
 
CHAPTER -12 it.pptx
Koteswari Kasireddy
 
WEB_DATABASE_chapter_4.pptx
Koteswari Kasireddy
 
Unit 4 chapter - 8 Transaction processing Concepts (1).pptx
Koteswari Kasireddy
 
Database System Concepts AND architecture [Autosaved].pptx
Koteswari Kasireddy
 
Evolution Of WEB_students.pptx
Koteswari Kasireddy
 
Presentation1.pptx
Koteswari Kasireddy
 
Algorithm.pptx
Koteswari Kasireddy
 
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
linked_list.pptx
Koteswari Kasireddy
 
matrices_and_loops.pptx
Koteswari Kasireddy
 
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
Control_Statements.pptx
Koteswari Kasireddy
 

Recently uploaded (20)

DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 

Control_Statements_in_Python.pptx

  • 3. Indentation in Python • In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block. • For the ease of programming and to achieve simplicity, python doesn't allow the use of curly braces or parentheses for the block level code. • Indentation is the most used part of the python programming language. • Generally, a tab space or four spaces are given to indent the statements in python.
  • 5. Conditional Statements in Python • Conditional Statements performs different computations or actions depending on conditions. • In python, the following are conditional statements o if o if –else o if – elif –else If statement: • The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. Syntax: if condition: statement1 statement2
  • 6. Conditional Statements in Python Cont.. Example: ifdemo.py a = 33 b = 200 if b > a: print ("b is greater than a") print ("done…") Remember: input () function is used to get input from user. Example: a=input (“Enter a value”) Output: python ifdemo.py b is greater than a done…
  • 7. Conditional Statements in Python Cont.. If-else statement: • The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. Syntax: if condition: #block of statements else: #another block of statements (else-block) Example: ifelsedemo.py age = int(input("Enter your age : ")) if age>=18: print("You are eligible to vote !!") else: print("Sorry! you have to wait !!")) Output: python ifelsedemo.py Enter your age: 19 You are eligible to vote!!
  • 8. Conditional Statements in Python Cont.. If-elif-else statement: • The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax: if condition1: # block of statements elif condition2: # block of statements elif condition3: # block of statements else: # block of statements
  • 9. Conditional Statements in Python Cont.. Example: maxnum.py a=int(input("Enter a value : ")) b=int(input("Enter b value : ")) c=int(input("Enter c value : ")) if (a>b) and (a>c): print("Maximum value is :",a) elif (b>a) and (b>c): print("Maximum value is :",b) else: print("Maximum value is :",c) Output: python maxnum.py Enter a value: 10 Enter b value: 14 Enter c value: 9 Maximum value is: 14
  • 11. Loop Statements in Python • Sometimes we may need to alter the flow of the program. If the execution of a specific code may need to be repeated several numbers of times then we can go for loop statements. • In python, the following are loop statements o while loop o for loop while loop: • With the while loop we can execute a set of statements as long as a condition is true. The while loop is mostly used in the case where the number of iterations is not known in advance. Syntax: while expression: Statement(s)
  • 12. Loop Statements in Python Cont.. Using else with while loop • Python enables us to use the while loop with the else block also. The else block is executed when the condition given in the while statement becomes false. Example: whiledemo.py i=1; while i<=3: print(i); i=i+1; Output: python whiledemo.py 1 2 3 Example: wedemo.py i=1; while i<=3: print(i); i=i+1; else: print("while loop terminated") Output: python wedemo.py 1 2 3 while loop terminated
  • 13. Loop Statements in Python Cont.. for loop: • The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary. Syntax: for iterating_var in sequence: statement(s) Example: fordemo.py i=1 n=int(input("Enter n value : ")) for i in range(i,n+1): print(i,end = ' ') Output: python fordemo.py Enter n value: 5 1 2 3 4 5
  • 14. Loop Statements in Python Cont.. Using else with for loop • Python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. • Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed. Example: fedemo.py for i in range(1,5): print(i,end=' ') else: print("for loop completely exhausted"); Output: python fedemo.py 1 2 3 4 for loop completely exhausted
  • 16. Jump Statements in Python • Jump statements in python are used to alter the flow of a loop like you want to skip a part of a loop or terminate a loop. • In python, the following are jump statements o break o continue break: • The break is a keyword in python which is used to bring the program control out of the loop. • The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. • The break is commonly used in the cases where we need to break the loop for a given condition. Syntax: break
  • 17. Jump Statements in Python Cont.. continue: • The continue statement in python is used to bring the program control to the beginning of the loop. • The continue statement skips the remaining lines of code inside the loop and start with the next iteration. • It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition. Syntax: continue Example: breakdemo.py i = 1 while i < 6: print(i) if i == 3: break i += 1 Output: python breakdemo.py 1 2 3
  • 18. Jump Statements in Python Cont.. Example: continuedemo.py str =input("Enter any String : ") for i in str: if i == 'h': continue; print(i,end=" "); Output: python continuedemo.py Enter any String : python p y t o n