SlideShare a Scribd company logo
3
Most read
6
Most read
10
Most read
PYTHON PROGRAMMING
VEERANAN VEERANAN
Assistant Professor in Information Technology
P.K.N. Arts and Science College
Tirumangalam
Madurai.
Python Control Statements
⬇
Selection
├─ if
├─ if-else
├─ if-elif-else
└─ nested if
⬇
Iteration
├─ while
│ └─ else-with-loop
└─ for
└─ nested loops
⬇
Jump Statements
├─ break
├─ continue
└─ pass
Mr. VV, ASP IT
Overview
if Statement
 Executes a block of code only if the condition is true.
 Syntax
if condition:
# code block
 Real-Time Example:
temperature = 35
if temperature > 30:
print("It's a hot day")
+-----------+
| Condition |
+-----------+
Yes | No
↓ ↓
+--------+ (Skip)
| Block |
+--------+
Mr. VV, ASP IT
if-else Statement
 Executes one block if the condition is true, otherwise executes another.
 Syntax:
if condition:
# code block if true
else:
# code block if false
 Real-Time Example:
age = 17
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
+-----------+
| Condition |
+-----------+
Yes | No
↓ ↓
+--------+ +--------+
| Block1 | | Block2 |
+--------+ +--------+
Mr. VV, ASP IT
Nested if Statement
 An if statement inside another if statement.
 Syntax:
if condition1:
if condition2:
# block
 Real-Time Example:
is_logged_in = True
is_admin = True
if is_logged_in:
if is_admin:
print("Welcome Admin!")
+-----------+
| Cond1 |
+-----------+
Yes | No
↓ ↓
+-----------+ (Skip)
| Cond2 |
+-----------+
Yes | No
↓ ↓
+--------+ (Skip)
| Block |
+--------+
Mr. VV, ASP IT
if-elif-else Statement
 Checks multiple conditions one by one until one is true.
 Syntax:
if condition1:
# block1
elif condition2:
# block2
else:
# block3
 Real-Time Example:
marks = 82
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
+-----------+ → Yes → +--------+
| Cond1 | | Block1 |
+-----------+ +--------+
No
↓
+-----------+ → Yes → +--------+
| Cond2 | | Block2 |
+-----------+ +--------+
No
↓
+-----------+
| Default |
+-----------+
Mr. VV, ASP IT
while Loop
 Repeats a block of code while the condition is true.
 Syntax:
while condition:
# block
 Real-Time Example:
stock = 5
while stock > 0:
print("Selling item")
stock -= 1
+-----------+
| Condition |
+-----------+
Yes | No
↓ ↓
+--------+ Exit
| Block |
+--------+
↑
+-- Repeat
Mr. VV, ASP IT
for Loop
 Iterates over a sequence (list, tuple, string, etc.).
 Syntax:
for variable in sequence:
# block
 Real-Time Example:
cart = ["Shoes", "Shirt", "Watch"]
for item in cart:
print(item)
+-----------+
| Sequence |
+-----------+
↓
+--------+
| Block |
+--------+
↓
Next → End?
Mr. VV, ASP IT
else with Loop
 Executes else block if loop completes normally (no break).
 Syntax:
for variable in sequence:
# loop block
else:
# block executed after loop
 Real-Time Example:
for n in range(3):
print(n)
else:
print("Loop Finished")
Loop Completed (no break)?
↓
+--------+
| else |
+--------+
Mr. VV, ASP IT
Nested Loops
 A loop inside another loop.
 Syntax:
for i in range(n):
for j in range(m):
# block
 Real-Time Example:
matrix = [[1,2,3],[4,5,6]]
for row in matrix:
for value in row:
print(value)
Outer Loop → +--------+
| Inner |
+--------+
↓
+--------+
| Block |
+--------+
Mr. VV, ASP IT
break Statement
 Exits the loop prematurely when condition is met.
 Syntax:
for variable in sequence:
if condition:
break
 Real-Time Example:
for num in [1,2,0,3]:
if num == 0:
break
print(num)
+-----------+
| Condition |
+-----------+
Yes → Exit Loop
No → Continue
Mr. VV, ASP IT
continue Statement
 Skips current iteration and continues with next.
 Syntax:
for variable in sequence:
if condition:
continue
 Real-Time Example:
for num in [-2,-1,0,1,2]:
if num < 0:
continue
print(num)
+-----------+
| Condition |
+-----------+
Yes → Skip Current Iteration
No → Normal Flow
Mr. VV, ASP IT
pass Statement
 Definition: Placeholder for future code, does nothing.
 Syntax:
if condition:
pass
 Real-Time Example:
def future_function():
pass # TODO: implement later
+-----------+
| pass |
+-----------+
(No action)
Mr. VV, ASP IT
 Iteration (Loops)
 while loop → Runs until condition false
Example: while stock>0: stock-=1
 for loop → Iterates over sequence
Example: for item in cart: print(item)
 else in loop → Runs if no break
Example: for n in range(3): print(n) else: print("Done")
 nested loops → Loop inside loop
Example: for r in matrix: for v in r: print(v)
Mr. VV, ASP IT
 Selection (Branching)
 if → Executes only if condition true
Example: if temp > 30: print("Hot day")
 if-else → Executes one block else another
Example: if age>=18: print("Vote") else: print("No")
 if-elif-else → Multiple conditions
Example: if marks>=90: ... elif marks>=75: ... else: ...
 nested if → if inside if
Example: if logged: if admin: print("Admin")
 Jump Statements
 break → Exit loop early
Example: for n in [1,2,0,3]: if n==0: break
 continue → Skip iteration
Example: for n in [-2,-1,1]: if n<0: continue; print(n)
 pass → Placeholder, does nothing
Example: def future(): pass

More Related Content

PDF
basic of desicion control statement in python
nitamhaske
 
PDF
Python unit 2 M.sc cs
KALAISELVI P
 
PDF
CONDITION AND ITERATIVE STATEMENTS CLASS XI
samirparmar24
 
PPTX
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
PPT
UNIT 2 PY.ppt - CONTROL STATEMENT IN PYTHON
drkangurajuphd
 
PDF
PYTHON FULL TUTORIAL WITH PROGRAMMS
Aniruddha Paul
 
PPTX
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 
basic of desicion control statement in python
nitamhaske
 
Python unit 2 M.sc cs
KALAISELVI P
 
CONDITION AND ITERATIVE STATEMENTS CLASS XI
samirparmar24
 
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
UNIT 2 PY.ppt - CONTROL STATEMENT IN PYTHON
drkangurajuphd
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
Aniruddha Paul
 
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 

Similar to Python Programming Unit II Control Statements.ppt (20)

PPTX
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
Python Control Structures.pptx
M Vishnuvardhan Reddy
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
module 2.pptx
mahendranaik18
 
PPTX
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
PPTX
Python for Beginners(v2)
Panimalar Engineering College
 
PPTX
Lecture on Fundamentals of Python Programming-2
JannatulFerdouse15
 
PPTX
Python Flow Control & use of functions.pptx
pandyahm47
 
PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
PPTX
PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT 2
MulliMary
 
PDF
Module 2-Review of Python Decision making and Loops.pdf
percivalfernandez2
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
DOCX
controlstatementspy.docx
manohar25689
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PDF
Engineering PYTHON_LOOPS concept in python.pdf
ChandrashekarReddy98
 
PPTX
Lecture - 2 (Python) E-Notes.pptx important for beginners
shabaghel19
 
PPTX
Lecture - 2 python notes important for beginners.
shabaghel19
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
Python Flow Control
Mohammed Sikander
 
Python Control Structures.pptx
M Vishnuvardhan Reddy
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
module 2.pptx
mahendranaik18
 
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
Python for Beginners(v2)
Panimalar Engineering College
 
Lecture on Fundamentals of Python Programming-2
JannatulFerdouse15
 
Python Flow Control & use of functions.pptx
pandyahm47
 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT 2
MulliMary
 
Module 2-Review of Python Decision making and Loops.pdf
percivalfernandez2
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
controlstatementspy.docx
manohar25689
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
Engineering PYTHON_LOOPS concept in python.pdf
ChandrashekarReddy98
 
Lecture - 2 (Python) E-Notes.pptx important for beginners
shabaghel19
 
Lecture - 2 python notes important for beginners.
shabaghel19
 
Ad

More from CUO VEERANAN VEERANAN (20)

PPT
Digital Logic Fundamentals – Number Systems & Codes
CUO VEERANAN VEERANAN
 
PPT
Cryptography - Unit I | Introduction to Security Concepts
CUO VEERANAN VEERANAN
 
PPT
Python Programming Essentials – A Student-Friendly Approach.ppt
CUO VEERANAN VEERANAN
 
PPT
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
CUO VEERANAN VEERANAN
 
PPT
Master Python Basics Easily – From Zero to Real-World Applications for UG Stu...
CUO VEERANAN VEERANAN
 
PDF
Banakacherla Project and Inter-State Water Politics in India
CUO VEERANAN VEERANAN
 
PPT
Overview of MS Access A Beginner's Guide to Creating and Managing Databases.ppt
CUO VEERANAN VEERANAN
 
PPT
HTML List, Nesting List & Hyperlinks.ppt
CUO VEERANAN VEERANAN
 
PPT
HTML Specific HTML tags also control font styles and colors
CUO VEERANAN VEERANAN
 
PPT
Introduction to Web Development Web Basics & HTML Tags
CUO VEERANAN VEERANAN
 
PPTX
Big Data - large Scale data (Amazon, FB)
CUO VEERANAN VEERANAN
 
PPTX
Fourier Transforms are indispensable tool
CUO VEERANAN VEERANAN
 
PPT
ENHANCING BIOLOGICAL RESEARCH THROUGH DIGITAL TECHNOLOGIES AND COMPUTATIONAL.ppt
CUO VEERANAN VEERANAN
 
PDF
ADS_Unit I_Route Map 2023.pdf
CUO VEERANAN VEERANAN
 
PPT
CS 23 Operating System Design Principles_MULTIPROCESSOR AND REAL TIME SCHEDULING
CUO VEERANAN VEERANAN
 
PPT
Python Unit I MCQ.ppt
CUO VEERANAN VEERANAN
 
PPT
GAC DS Priority Queue Presentation 2022.ppt
CUO VEERANAN VEERANAN
 
PPT
GAC Java Presentation_Server Side Include_Cookies_Filters 2022.ppt
CUO VEERANAN VEERANAN
 
PDF
Lab 3 Python Programming Lab 8-15 MKU.pdf
CUO VEERANAN VEERANAN
 
PDF
Lab 3 Python Programming Lab 1-8 MKU.pdf
CUO VEERANAN VEERANAN
 
Digital Logic Fundamentals – Number Systems & Codes
CUO VEERANAN VEERANAN
 
Cryptography - Unit I | Introduction to Security Concepts
CUO VEERANAN VEERANAN
 
Python Programming Essentials – A Student-Friendly Approach.ppt
CUO VEERANAN VEERANAN
 
From Operators to Arrays – Power Up Your Python Skills for Real-World Coding!
CUO VEERANAN VEERANAN
 
Master Python Basics Easily – From Zero to Real-World Applications for UG Stu...
CUO VEERANAN VEERANAN
 
Banakacherla Project and Inter-State Water Politics in India
CUO VEERANAN VEERANAN
 
Overview of MS Access A Beginner's Guide to Creating and Managing Databases.ppt
CUO VEERANAN VEERANAN
 
HTML List, Nesting List & Hyperlinks.ppt
CUO VEERANAN VEERANAN
 
HTML Specific HTML tags also control font styles and colors
CUO VEERANAN VEERANAN
 
Introduction to Web Development Web Basics & HTML Tags
CUO VEERANAN VEERANAN
 
Big Data - large Scale data (Amazon, FB)
CUO VEERANAN VEERANAN
 
Fourier Transforms are indispensable tool
CUO VEERANAN VEERANAN
 
ENHANCING BIOLOGICAL RESEARCH THROUGH DIGITAL TECHNOLOGIES AND COMPUTATIONAL.ppt
CUO VEERANAN VEERANAN
 
ADS_Unit I_Route Map 2023.pdf
CUO VEERANAN VEERANAN
 
CS 23 Operating System Design Principles_MULTIPROCESSOR AND REAL TIME SCHEDULING
CUO VEERANAN VEERANAN
 
Python Unit I MCQ.ppt
CUO VEERANAN VEERANAN
 
GAC DS Priority Queue Presentation 2022.ppt
CUO VEERANAN VEERANAN
 
GAC Java Presentation_Server Side Include_Cookies_Filters 2022.ppt
CUO VEERANAN VEERANAN
 
Lab 3 Python Programming Lab 8-15 MKU.pdf
CUO VEERANAN VEERANAN
 
Lab 3 Python Programming Lab 1-8 MKU.pdf
CUO VEERANAN VEERANAN
 
Ad

Recently uploaded (20)

PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Understanding operators in c language.pptx
auteharshil95
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 

Python Programming Unit II Control Statements.ppt

  • 1. PYTHON PROGRAMMING VEERANAN VEERANAN Assistant Professor in Information Technology P.K.N. Arts and Science College Tirumangalam Madurai.
  • 2. Python Control Statements ⬇ Selection ├─ if ├─ if-else ├─ if-elif-else └─ nested if ⬇ Iteration ├─ while │ └─ else-with-loop └─ for └─ nested loops ⬇ Jump Statements ├─ break ├─ continue └─ pass Mr. VV, ASP IT Overview
  • 3. if Statement  Executes a block of code only if the condition is true.  Syntax if condition: # code block  Real-Time Example: temperature = 35 if temperature > 30: print("It's a hot day") +-----------+ | Condition | +-----------+ Yes | No ↓ ↓ +--------+ (Skip) | Block | +--------+ Mr. VV, ASP IT
  • 4. if-else Statement  Executes one block if the condition is true, otherwise executes another.  Syntax: if condition: # code block if true else: # code block if false  Real-Time Example: age = 17 if age >= 18: print("Eligible to vote") else: print("Not eligible to vote") +-----------+ | Condition | +-----------+ Yes | No ↓ ↓ +--------+ +--------+ | Block1 | | Block2 | +--------+ +--------+ Mr. VV, ASP IT
  • 5. Nested if Statement  An if statement inside another if statement.  Syntax: if condition1: if condition2: # block  Real-Time Example: is_logged_in = True is_admin = True if is_logged_in: if is_admin: print("Welcome Admin!") +-----------+ | Cond1 | +-----------+ Yes | No ↓ ↓ +-----------+ (Skip) | Cond2 | +-----------+ Yes | No ↓ ↓ +--------+ (Skip) | Block | +--------+ Mr. VV, ASP IT
  • 6. if-elif-else Statement  Checks multiple conditions one by one until one is true.  Syntax: if condition1: # block1 elif condition2: # block2 else: # block3  Real-Time Example: marks = 82 if marks >= 90: print("Grade A") elif marks >= 75: print("Grade B") else: print("Grade C") +-----------+ → Yes → +--------+ | Cond1 | | Block1 | +-----------+ +--------+ No ↓ +-----------+ → Yes → +--------+ | Cond2 | | Block2 | +-----------+ +--------+ No ↓ +-----------+ | Default | +-----------+ Mr. VV, ASP IT
  • 7. while Loop  Repeats a block of code while the condition is true.  Syntax: while condition: # block  Real-Time Example: stock = 5 while stock > 0: print("Selling item") stock -= 1 +-----------+ | Condition | +-----------+ Yes | No ↓ ↓ +--------+ Exit | Block | +--------+ ↑ +-- Repeat Mr. VV, ASP IT
  • 8. for Loop  Iterates over a sequence (list, tuple, string, etc.).  Syntax: for variable in sequence: # block  Real-Time Example: cart = ["Shoes", "Shirt", "Watch"] for item in cart: print(item) +-----------+ | Sequence | +-----------+ ↓ +--------+ | Block | +--------+ ↓ Next → End? Mr. VV, ASP IT
  • 9. else with Loop  Executes else block if loop completes normally (no break).  Syntax: for variable in sequence: # loop block else: # block executed after loop  Real-Time Example: for n in range(3): print(n) else: print("Loop Finished") Loop Completed (no break)? ↓ +--------+ | else | +--------+ Mr. VV, ASP IT
  • 10. Nested Loops  A loop inside another loop.  Syntax: for i in range(n): for j in range(m): # block  Real-Time Example: matrix = [[1,2,3],[4,5,6]] for row in matrix: for value in row: print(value) Outer Loop → +--------+ | Inner | +--------+ ↓ +--------+ | Block | +--------+ Mr. VV, ASP IT
  • 11. break Statement  Exits the loop prematurely when condition is met.  Syntax: for variable in sequence: if condition: break  Real-Time Example: for num in [1,2,0,3]: if num == 0: break print(num) +-----------+ | Condition | +-----------+ Yes → Exit Loop No → Continue Mr. VV, ASP IT
  • 12. continue Statement  Skips current iteration and continues with next.  Syntax: for variable in sequence: if condition: continue  Real-Time Example: for num in [-2,-1,0,1,2]: if num < 0: continue print(num) +-----------+ | Condition | +-----------+ Yes → Skip Current Iteration No → Normal Flow Mr. VV, ASP IT
  • 13. pass Statement  Definition: Placeholder for future code, does nothing.  Syntax: if condition: pass  Real-Time Example: def future_function(): pass # TODO: implement later +-----------+ | pass | +-----------+ (No action) Mr. VV, ASP IT
  • 14.  Iteration (Loops)  while loop → Runs until condition false Example: while stock>0: stock-=1  for loop → Iterates over sequence Example: for item in cart: print(item)  else in loop → Runs if no break Example: for n in range(3): print(n) else: print("Done")  nested loops → Loop inside loop Example: for r in matrix: for v in r: print(v) Mr. VV, ASP IT  Selection (Branching)  if → Executes only if condition true Example: if temp > 30: print("Hot day")  if-else → Executes one block else another Example: if age>=18: print("Vote") else: print("No")  if-elif-else → Multiple conditions Example: if marks>=90: ... elif marks>=75: ... else: ...  nested if → if inside if Example: if logged: if admin: print("Admin")  Jump Statements  break → Exit loop early Example: for n in [1,2,0,3]: if n==0: break  continue → Skip iteration Example: for n in [-2,-1,1]: if n<0: continue; print(n)  pass → Placeholder, does nothing Example: def future(): pass