SlideShare a Scribd company logo
3
Most read
5
Most read
15
Most read
Python Control Structures.pptx
M Vishnuvardhan
Control Structures
Control Structures are used to modify the flow of the program.
There are two types of control structures
» Branching statements
» Looping statements
M Vishnuvardhan
Branching statements – if
Syntax:
if condition:
statement1
statement2
:
statementn
Next statement
eg:
if a>b:
print(“a is big”)
NOTE all the statements which need to part of the if should be placed in same
indentation
M Vishnuvardhan
if else
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)
Eg:
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even...")
else:
print("Number is odd...")
M Vishnuvardhan
elif statement
The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them. We
can have any number of elif statements in our program depending upon our need.
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
M Vishnuvardhan
Nested if
Nested if statements mean an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# inner if Block is end here
# outer if Block is end here
M Vishnuvardhan
Shorthand if & if else
If you have only one statement to execute, you can put it on the same line as the
if statement.
Syntax: if condition: #true block
Eg: if a > b: print("a is greater than b")
If you have only one statement to execute, one for if, and one for else, you can
put it all on the same line
Syntax: #true block if condition else #false block
Eg: print("A is big ") if a > b else print("B is big")
Note: This technique is known as Ternary Operators, or Conditional
Expressions.
M Vishnuvardhan
Looping - while
With the while loop we can execute a set of statements as long as a condition is
true.
Syntax: while condition:
#body of the loop
Eg: i = 1
while i < 6:
print(i)
i += 1
Note: remember to increment i, or else the loop will continue
forever.
M Vishnuvardhan
While loop with else
With the else statement we can run a block of code once when the condition no
longer is true
Syntax: while condition:
#body of the loop
else:
#false block
Eg: i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
M Vishnuvardhan
For loop
For loop can used in two ways.
» Using on a sequence
» Using range()
A for loop is used for iterating over a sequence
(i.e, either a list, a tuple, a dictionary, a set,
or a string).
Syntax: for x in sequence:
body of the loop
M Vishnuvardhan
For loop - sequence
Looping through a List
Eg: fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Looping through a String
name=“ssbn”
for x in name:
print(x)
M Vishnuvardhan
For loop – range()
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number.
range (start, stop, step)
start is the starting value and is inclusive optional (default-0)
Stop is the end value and is exclusive and is mandatory
Step is incrementation from start to end optional (default -1)
Syntax: for var in range(start, stop, step):
#body of the loop
Eg: for x in range(0,6,1):
print(x)
M Vishnuvardhan
For loop – range()
» Step can be optional default is 1
for x in range(0, 6):
print(x) #prints 0,1,2,3,4,5
» Start can be optional default is 0
for x in range(6):
print(x) #prints 0,1,2,3,4,5
» Step can be other than 1
for x in range(0,6,3):
print(x) #prints 0,3
M Vishnuvardhan
For loop with else
else keyword in a for loop specifies a block of code to be executed when the loop
is finished:
Eg: for x in range(6):
print(x)
else:
print("Finally finished!")
M Vishnuvardhan
Nested loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time
for each iteration of the "outer loop":
Eg:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
M Vishnuvardhan
Jump statements - break
The break keyword is used to break out a for loop, or a while loop.
Syntax : break
Eg: i = 1
while i < 9:
print(i)
if i == 3:
break
i += 1
Eg:
i = 1
for i in range(6):
print(i)
if i == 3:
break
i += 1
M Vishnuvardhan
Jump statements - continue
The continue keyword is used to end the current iteration in a for
loop (or a while loop), and continues to the next iteration.
Syntax: continue
Eg: for i in range(1,11):
if i%2!=0:
continue
print(i)
M Vishnuvardhan
Jump statements - pass
The pass statement is used as a placeholder for future code. When
the pass statement is executed, nothing happens, but avoids getting
an error when empty code is not allowed. Empty code is not allowed
in loops, function definitions, class definitions, or in if
statements
Syntax: pass
Eg: if(a<b):
pass
else:
print("b<a")
M Vishnuvardhan

More Related Content

PPTX
Python for loop
Aishwarya Deshmukh
 
PDF
Function arguments In Python
Amit Upadhyay
 
PPTX
Python oop third class
Aleksander Fabijan
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
Pass by value and pass by reference
TurnToTech
 
PPTX
Python Functions
Mohammed Sikander
 
PPTX
String Manipulation in Python
Pooja B S
 
Python for loop
Aishwarya Deshmukh
 
Function arguments In Python
Amit Upadhyay
 
Python oop third class
Aleksander Fabijan
 
Input and output in C++
Nilesh Dalvi
 
Pass by value and pass by reference
TurnToTech
 
Python Functions
Mohammed Sikander
 
String Manipulation in Python
Pooja B S
 

What's hot (20)

PPTX
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PDF
Python Programming - Files & Exceptions
Omid AmirGhiasvand
 
PPTX
Python comments and variables.pptx
adityakumawat625
 
PDF
Python exception handling
Mohammed Sikander
 
PDF
Python list
Mohammed Sikander
 
PPTX
Functions in Python
Shakti Singh Rathore
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
Python dictionary
eman lotfy
 
PPTX
Library functions in c++
Neeru Mittal
 
PPSX
python Function
Ronak Rathi
 
PPTX
Python-04| Fundamental data types vs immutability
Mohd Sajjad
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Unit 4 python -list methods
narmadhakin
 
PPTX
Loops in Python
Arockia Abins
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPTX
C++ IF STATMENT AND ITS TYPE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PPTX
Packages In Python Tutorial
Simplilearn
 
PPTX
Python Data-Types
Akhil Kaushik
 
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Modules and packages in python
TMARAGATHAM
 
Python Programming - Files & Exceptions
Omid AmirGhiasvand
 
Python comments and variables.pptx
adityakumawat625
 
Python exception handling
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Functions in Python
Shakti Singh Rathore
 
Functions in Python
Kamal Acharya
 
Python dictionary
eman lotfy
 
Library functions in c++
Neeru Mittal
 
python Function
Ronak Rathi
 
Python-04| Fundamental data types vs immutability
Mohd Sajjad
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Unit 4 python -list methods
narmadhakin
 
Loops in Python
Arockia Abins
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Packages In Python Tutorial
Simplilearn
 
Python Data-Types
Akhil Kaushik
 
Ad

Similar to Python Control Structures.pptx (20)

PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PDF
Notes2
hccit
 
PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
PPTX
control statements in python.pptx
Anshu Varma
 
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
FahmaFamzin
 
PDF
Python unit 2 M.sc cs
KALAISELVI P
 
PPTX
Loops in c language
tanmaymodi4
 
PPTX
Loops in c language
Tanmay Modi
 
PDF
Python_Module_2.pdf
R.K.College of engg & Tech
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PDF
Chapter 13.1.5
patcha535
 
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PPTX
CONTROL STRUCTURE IN VB
classall
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
While_for_loop presententationin first year students
SIHIGOPAL
 
PDF
While-For-loop in python used in college
ssuser7a7cd61
 
PDF
basic of desicion control statement in python
nitamhaske
 
PPTX
While loop
RabiyaZhexembayeva
 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Notes2
hccit
 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
control statements in python.pptx
Anshu Varma
 
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
FahmaFamzin
 
Python unit 2 M.sc cs
KALAISELVI P
 
Loops in c language
tanmaymodi4
 
Loops in c language
Tanmay Modi
 
Python_Module_2.pdf
R.K.College of engg & Tech
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
Chapter 13.1.5
patcha535
 
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
CONTROL STRUCTURE IN VB
classall
 
Loops and conditional statements
Saad Sheikh
 
While_for_loop presententationin first year students
SIHIGOPAL
 
While-For-loop in python used in college
ssuser7a7cd61
 
basic of desicion control statement in python
nitamhaske
 
While loop
RabiyaZhexembayeva
 
Ad

More from M Vishnuvardhan Reddy (20)

PPTX
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
PPTX
Lists_tuples.pptx
M Vishnuvardhan Reddy
 
PPTX
Python Strings.pptx
M Vishnuvardhan Reddy
 
PPTX
Python Basics.pptx
M Vishnuvardhan Reddy
 
PPTX
Python Operators.pptx
M Vishnuvardhan Reddy
 
PPTX
Python Datatypes.pptx
M Vishnuvardhan Reddy
 
PPTX
DataScience.pptx
M Vishnuvardhan Reddy
 
PPT
Html forms
M Vishnuvardhan Reddy
 
PPT
Cascading Style Sheets
M Vishnuvardhan Reddy
 
PPT
Java Threads
M Vishnuvardhan Reddy
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PPT
Scanner class
M Vishnuvardhan Reddy
 
PPT
Polymorphism
M Vishnuvardhan Reddy
 
PPT
Java intro
M Vishnuvardhan Reddy
 
PPT
Java applets
M Vishnuvardhan Reddy
 
PPT
Exception handling
M Vishnuvardhan Reddy
 
PPT
Control structures
M Vishnuvardhan Reddy
 
PPT
Constructors
M Vishnuvardhan Reddy
 
PPT
Classes&amp;objects
M Vishnuvardhan Reddy
 
PPS
Shell sort
M Vishnuvardhan Reddy
 
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Lists_tuples.pptx
M Vishnuvardhan Reddy
 
Python Strings.pptx
M Vishnuvardhan Reddy
 
Python Basics.pptx
M Vishnuvardhan Reddy
 
Python Operators.pptx
M Vishnuvardhan Reddy
 
Python Datatypes.pptx
M Vishnuvardhan Reddy
 
DataScience.pptx
M Vishnuvardhan Reddy
 
Cascading Style Sheets
M Vishnuvardhan Reddy
 
Java Threads
M Vishnuvardhan Reddy
 
Java Streams
M Vishnuvardhan Reddy
 
Scanner class
M Vishnuvardhan Reddy
 
Polymorphism
M Vishnuvardhan Reddy
 
Java applets
M Vishnuvardhan Reddy
 
Exception handling
M Vishnuvardhan Reddy
 
Control structures
M Vishnuvardhan Reddy
 
Constructors
M Vishnuvardhan Reddy
 
Classes&amp;objects
M Vishnuvardhan Reddy
 

Recently uploaded (20)

PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
The Future of Artificial Intelligence (AI)
Mukul
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 

Python Control Structures.pptx

  • 2. M Vishnuvardhan Control Structures Control Structures are used to modify the flow of the program. There are two types of control structures » Branching statements » Looping statements
  • 3. M Vishnuvardhan Branching statements – if Syntax: if condition: statement1 statement2 : statementn Next statement eg: if a>b: print(“a is big”) NOTE all the statements which need to part of the if should be placed in same indentation
  • 4. M Vishnuvardhan if else 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) Eg: num = int(input("enter the number?")) if num%2 == 0: print("Number is even...") else: print("Number is odd...")
  • 5. M Vishnuvardhan elif statement The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. Syntax: if expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements
  • 6. M Vishnuvardhan Nested if Nested if statements mean an if statement inside another if statement. Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # inner if Block is end here # outer if Block is end here
  • 7. M Vishnuvardhan Shorthand if & if else If you have only one statement to execute, you can put it on the same line as the if statement. Syntax: if condition: #true block Eg: if a > b: print("a is greater than b") If you have only one statement to execute, one for if, and one for else, you can put it all on the same line Syntax: #true block if condition else #false block Eg: print("A is big ") if a > b else print("B is big") Note: This technique is known as Ternary Operators, or Conditional Expressions.
  • 8. M Vishnuvardhan Looping - while With the while loop we can execute a set of statements as long as a condition is true. Syntax: while condition: #body of the loop Eg: i = 1 while i < 6: print(i) i += 1 Note: remember to increment i, or else the loop will continue forever.
  • 9. M Vishnuvardhan While loop with else With the else statement we can run a block of code once when the condition no longer is true Syntax: while condition: #body of the loop else: #false block Eg: i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6")
  • 10. M Vishnuvardhan For loop For loop can used in two ways. » Using on a sequence » Using range() A for loop is used for iterating over a sequence (i.e, either a list, a tuple, a dictionary, a set, or a string). Syntax: for x in sequence: body of the loop
  • 11. M Vishnuvardhan For loop - sequence Looping through a List Eg: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) Looping through a String name=“ssbn” for x in name: print(x)
  • 12. M Vishnuvardhan For loop – range() The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. range (start, stop, step) start is the starting value and is inclusive optional (default-0) Stop is the end value and is exclusive and is mandatory Step is incrementation from start to end optional (default -1) Syntax: for var in range(start, stop, step): #body of the loop Eg: for x in range(0,6,1): print(x)
  • 13. M Vishnuvardhan For loop – range() » Step can be optional default is 1 for x in range(0, 6): print(x) #prints 0,1,2,3,4,5 » Start can be optional default is 0 for x in range(6): print(x) #prints 0,1,2,3,4,5 » Step can be other than 1 for x in range(0,6,3): print(x) #prints 0,3
  • 14. M Vishnuvardhan For loop with else else keyword in a for loop specifies a block of code to be executed when the loop is finished: Eg: for x in range(6): print(x) else: print("Finally finished!")
  • 15. M Vishnuvardhan Nested loops A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop": Eg: adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)
  • 16. M Vishnuvardhan Jump statements - break The break keyword is used to break out a for loop, or a while loop. Syntax : break Eg: i = 1 while i < 9: print(i) if i == 3: break i += 1 Eg: i = 1 for i in range(6): print(i) if i == 3: break i += 1
  • 17. M Vishnuvardhan Jump statements - continue The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration. Syntax: continue Eg: for i in range(1,11): if i%2!=0: continue print(i)
  • 18. M Vishnuvardhan Jump statements - pass The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but avoids getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements Syntax: pass Eg: if(a<b): pass else: print("b<a")