SlideShare a Scribd company logo
Python Programming
Unit – II (Part II)
(Lecture 11)
Conditionals and Recursion
Recursion Infinite Recursion Keyboard input
Python Programming
Unit – II (Part II)
(Lecture 9)
Conditionals and Recursion
Syllabus
Lecture 9
Lecture 10
Lecture 11
Floor division and
modulus
Boolean expressions
Logical operators
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
Recursion
Infinite Recursion
Keyboard input
Syllabus
Lecture 9
Floor division and
modulus
Boolean expressions
Logical operators
• There are three logical
operators:
1. and
2. or
3. not
Relational operators:
• A = = B(Equal to)
• A! = B (Not equal to)
• A > B (Greater than)
• A < B (Less than)
• A > = B (Greater than or equal to)
• A < = B (Less than or equal to)
A Boolean expression is either
true or false.
• Ex: A = 5, B = 5, C = 6
• A = = B returns True
• A = = C returns False
Floor division:
• Digits after the decimal point
are removed.
• Symbol for Floor division: //
• Ex: 9 / 2 is 4.5 (Division)
9 // 2 is 4 (Floor division)
Modulus:
• Returns the remainder value
• Symbol for Modulus: %
• Ex: 9 % 2 is 1
Floor division and
modulus
Boolean expressions Logical operators
• There are three logical
operators:
1. and
2. or
3. not
Relational operators:
• A = = B(Equal to)
• A! = B (Not equal to)
• A > B (Greater than)
• A < B (Less than)
• A > = B (Greater than or equal to)
• A < = B (Less than or equal to)
A Boolean expression is either
true or false.
• Ex: A = 5, B = 5, C = 6
• A = = B returns True
• A = = C returns False
Floor division:
• Digits after the decimal point
are removed.
• Symbol for Floor division: //
• Ex: 9 / 2 is 4.5 (Division)
9 // 2 is 4 (Floor division)
Modulus:
• Returns the remainder value
• Symbol for Modulus: %
• Ex: 9 % 2 is 1
Floor division and
modulus
Boolean expressions Logical operators
Python Programming
Unit – II (Part II)
(Lecture 10)
Conditionals and Recursion
Syllabus
Lecture 9
Lecture 10
Lecture 11
Floor division and
modulus
Boolean expressions
Logical operators
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
Recursion
Infinite Recursion
Keyboard input
Syllabus
Lecture 10
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
• Conditions change
the flow of program
execution
• if statement is used
in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another Condition
in one condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
Conditionals
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
• Conditions change
the flow of program
execution
• if statement is used
in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another Condition
in one condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
Python Programming
Unit – II (Part II)
(Lecture 11)
Conditionals and Recursion
• There are three logical
operators:
1. and
2. or
3. not
Relational operators:
• A = = B(Equal to)
• A! = B (Not equal to)
• A > B (Greater than)
• A < B (Less than)
• A > = B (Greater than or equal to)
• A < = B (Less than or equal to)
A Boolean expression is either
true or false.
• Ex: A = 5, B = 5, C = 6
• A = = B returns True
• A = = C returns False
Floor division:
• Digits after the decimal point
are removed.
• Symbol for Floor division: //
• Ex: 9 / 2 is 4.5 (Division)
9 // 2 is 4 (Floor division)
Modulus:
• Returns the remainder value
• Symbol for Modulus: %
• Ex: 9 % 2 is 1
Floor division and
modulus
Boolean expressions Logical operators
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
• Conditions change
the flow of program
execution
• if statement is used
in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another Condition
in one condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
Syllabus
Lecture 9
Lecture 10
Lecture 11
Floor division and
modulus
Boolean expressions
Logical operators
Conditional
execution
Alternative
execution
Chained
conditionals
Nested
conditionals
Recursion
Infinite Recursion
Keyboard input
Lecture 11
Recursion
Infinite Recursion
Keyboard input
Recursion
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
4
If
n < 0
False
Print(n)
n = n-1
Output:
4
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
3
If
n < 0
False
Print(n)
n = n-1
Call count down
Output:
4
3
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
2
If
n < 0
False
Print(n)
n = n-1
Call count down
Output:
4
3
2
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
1
If
n < 0
False
Print(n)
n = n-1
Call count down
Output:
4
3
2
1
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
0
If
n < 0
False
Print(n)
n = n-1
Call count down
Output:
4
3
2
1
0
Recursion
• Function to call
itself
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n = n - 1
countdown(n)
Countdown(n)
n
-1
If
n < 0
False
Print(n)
n = n-1
Call count down
Output:
4
3
2
1
0
Blast
Print(“Blast”)
True
Recursion
Lecture 11
Recursion
Infinite Recursion
Keyboard input
Countdown(n)
n
4
If
n < 0
False
Print(n)
n = n+1
Output:
4
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
n=n+1
countdown(n)
Countdown(n)
n
5
If
n < 0
False
Print(n)
n = n+1
Call count down
Output:
4
5
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
countdown(n+1)
Countdown(n)
n
6
If
n < 0
False
Print(n)
n = n+1
Call count down
Output:
4
5
6
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
countdown(n+1)
Countdown(n)
n
7
If
n < 0
False
Print(n)
n = n+1
Call count down
Output:
4
5
6
7
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
countdown(n+1)
Countdown(n)
n
8
If
n < 0
False
Print(n)
n = n+1
Call count down
Output:
4
5
6
7
8
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
countdown(n+1)
Countdown(n)
n
9
If
n < 0
False
Print(n)
n = n+1
Call count down
Output:
4
5
6
7
8
Infinite recursion
Infinite Recursion
• Making recursive
calls forever
• The program never
terminates.
Ex:
def countdown(n):
if n < 0:
print (“Blast”)
else:
print (n)
countdown(n+1)
Recursion
Lecture 11
Recursion
Infinite Recursion
Keyboard input
Keyboard input
• Accept input
from the user
with keyboard.
• input function
is used
A = 5
B = 6
print( A + B ) is 11
Here A & B variables has fixed values (given in
the program)
A = int(input("enter a value:"))
B = int(input("enter b value:"))
print( A + B)
Here A & B variables has no fixed values (given
in runtime)

More Related Content

What's hot (20)

PPTX
Map, Filter and Reduce In Python
Simplilearn
 
PDF
Datatypes in python
eShikshak
 
PPTX
Variables in python
Jaya Kumari
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Compiler Design Unit 4
Jena Catherine Bel D
 
PDF
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
PDF
Python - object oriented
Learnbay Datascience
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPTX
Dictionary in python
vikram mahendra
 
PPTX
virtual function
VENNILAV6
 
PPTX
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
PPTX
Functions in Python
Kamal Acharya
 
PPT
Introduction to Python
Nowell Strite
 
PPTX
Python-Functions.pptx
Karudaiyar Ganapathy
 
PPT
Files in c++ ppt
Kumar
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PPTX
Exception handling
PhD Research Scholar
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Map, Filter and Reduce In Python
Simplilearn
 
Datatypes in python
eShikshak
 
Variables in python
Jaya Kumari
 
Arrays in Java
Abhilash Nair
 
Compiler Design Unit 4
Jena Catherine Bel D
 
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
Python - object oriented
Learnbay Datascience
 
Network programming in java - PPT
kamal kotecha
 
Dictionary in python
vikram mahendra
 
virtual function
VENNILAV6
 
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
Functions in Python
Kamal Acharya
 
Introduction to Python
Nowell Strite
 
Python-Functions.pptx
Karudaiyar Ganapathy
 
Files in c++ ppt
Kumar
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Exception handling
PhD Research Scholar
 
Functions in python slide share
Devashish Kumar
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 

Similar to Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion | (20)

PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PDF
Python Course Lecture Defining Functions
MuhammadIfitikhar
 
PDF
Sample Exam Questions on Python for revision
afsheenfaiq2
 
PPTX
Ch5 Selection Statements
SzeChingChen
 
PPTX
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 
PPTX
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 
PPTX
03. operators and-expressions
Stoian Kirov
 
PDF
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
PPTX
03. Operators Expressions and statements
Intro C# Book
 
PDF
14. Recursion.pdf
VivekBhimajiyani
 
PPTX
Functional programming
Prashant Kalkar
 
PPTX
2.overview of c#
Raghu nath
 
PDF
07 control+structures
baran19901990
 
PPTX
UNIT – 3.pptx for first year engineering
SabarigiriVason
 
PPTX
Loops Branches and control flow in MATLAB
Sufi Yasir Raza
 
PDF
if statements in Python -A lecture class
binzbinz3
 
PPTX
Conditional Statements.pptx
Gautam623648
 
PPT
Control Statement.ppt
sanjay
 
basics of optimizations presentation s
AnkitKumarSharma26
 
Python Course Lecture Defining Functions
MuhammadIfitikhar
 
Sample Exam Questions on Python for revision
afsheenfaiq2
 
Ch5 Selection Statements
SzeChingChen
 
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 
03. operators and-expressions
Stoian Kirov
 
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
03. Operators Expressions and statements
Intro C# Book
 
14. Recursion.pdf
VivekBhimajiyani
 
Functional programming
Prashant Kalkar
 
2.overview of c#
Raghu nath
 
07 control+structures
baran19901990
 
UNIT – 3.pptx for first year engineering
SabarigiriVason
 
Loops Branches and control flow in MATLAB
Sufi Yasir Raza
 
if statements in Python -A lecture class
binzbinz3
 
Conditional Statements.pptx
Gautam623648
 
Control Statement.ppt
sanjay
 
Ad

More from FabMinds (20)

PPTX
Python Programming | JNTUA | UNIT 3 | Lists |
FabMinds
 
PPTX
Python Programming | JNTUA | UNIT 3 | Strings |
FabMinds
 
PPTX
Python Programming | JNTUA | UNIT 3 | Updating Variables & Iteration |
FabMinds
 
PPTX
Python Programming | JNTUA | UNIT 2 | Case Study |
FabMinds
 
PPTX
Python Programming | JNTUA | UNIT 2 | Fruitful Functions |
FabMinds
 
PPTX
Application layer protocols
FabMinds
 
PPTX
Internet connectivity
FabMinds
 
PPTX
Introduction for internet connectivity (IoT)
FabMinds
 
PPTX
web connectivity in IoT
FabMinds
 
PPTX
message communication protocols in IoT
FabMinds
 
PPTX
web communication protocols in IoT
FabMinds
 
PPTX
introduction for web connectivity (IoT)
FabMinds
 
PPTX
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
FabMinds
 
PPTX
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
FabMinds
 
PPTX
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
PPTX
Data enrichment
FabMinds
 
PPTX
Communication technologies
FabMinds
 
PPTX
M2M systems layers and designs standardizations
FabMinds
 
PPTX
Business models for business processes on IoT
FabMinds
 
PPTX
Python Programming | JNTUK | UNIT 1 | Lecture 5
FabMinds
 
Python Programming | JNTUA | UNIT 3 | Lists |
FabMinds
 
Python Programming | JNTUA | UNIT 3 | Strings |
FabMinds
 
Python Programming | JNTUA | UNIT 3 | Updating Variables & Iteration |
FabMinds
 
Python Programming | JNTUA | UNIT 2 | Case Study |
FabMinds
 
Python Programming | JNTUA | UNIT 2 | Fruitful Functions |
FabMinds
 
Application layer protocols
FabMinds
 
Internet connectivity
FabMinds
 
Introduction for internet connectivity (IoT)
FabMinds
 
web connectivity in IoT
FabMinds
 
message communication protocols in IoT
FabMinds
 
web communication protocols in IoT
FabMinds
 
introduction for web connectivity (IoT)
FabMinds
 
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
FabMinds
 
Python Introduction | JNTUA | R19 | UNIT 1 | Functions
FabMinds
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
Data enrichment
FabMinds
 
Communication technologies
FabMinds
 
M2M systems layers and designs standardizations
FabMinds
 
Business models for business processes on IoT
FabMinds
 
Python Programming | JNTUK | UNIT 1 | Lecture 5
FabMinds
 
Ad

Recently uploaded (20)

PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Horarios de distribución de agua en julio
pegazohn1978
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 

Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion |

  • 1. Python Programming Unit – II (Part II) (Lecture 11) Conditionals and Recursion Recursion Infinite Recursion Keyboard input
  • 2. Python Programming Unit – II (Part II) (Lecture 9) Conditionals and Recursion
  • 3. Syllabus Lecture 9 Lecture 10 Lecture 11 Floor division and modulus Boolean expressions Logical operators Conditional execution Alternative execution Chained conditionals Nested conditionals Recursion Infinite Recursion Keyboard input
  • 4. Syllabus Lecture 9 Floor division and modulus Boolean expressions Logical operators
  • 5. • There are three logical operators: 1. and 2. or 3. not Relational operators: • A = = B(Equal to) • A! = B (Not equal to) • A > B (Greater than) • A < B (Less than) • A > = B (Greater than or equal to) • A < = B (Less than or equal to) A Boolean expression is either true or false. • Ex: A = 5, B = 5, C = 6 • A = = B returns True • A = = C returns False Floor division: • Digits after the decimal point are removed. • Symbol for Floor division: // • Ex: 9 / 2 is 4.5 (Division) 9 // 2 is 4 (Floor division) Modulus: • Returns the remainder value • Symbol for Modulus: % • Ex: 9 % 2 is 1 Floor division and modulus Boolean expressions Logical operators
  • 6. • There are three logical operators: 1. and 2. or 3. not Relational operators: • A = = B(Equal to) • A! = B (Not equal to) • A > B (Greater than) • A < B (Less than) • A > = B (Greater than or equal to) • A < = B (Less than or equal to) A Boolean expression is either true or false. • Ex: A = 5, B = 5, C = 6 • A = = B returns True • A = = C returns False Floor division: • Digits after the decimal point are removed. • Symbol for Floor division: // • Ex: 9 / 2 is 4.5 (Division) 9 // 2 is 4 (Floor division) Modulus: • Returns the remainder value • Symbol for Modulus: % • Ex: 9 % 2 is 1 Floor division and modulus Boolean expressions Logical operators
  • 7. Python Programming Unit – II (Part II) (Lecture 10) Conditionals and Recursion
  • 8. Syllabus Lecture 9 Lecture 10 Lecture 11 Floor division and modulus Boolean expressions Logical operators Conditional execution Alternative execution Chained conditionals Nested conditionals Recursion Infinite Recursion Keyboard input
  • 10. Conditional execution Alternative execution Chained conditionals Nested conditionals • Conditions change the flow of program execution • if statement is used in conditions Syntax: if condition: statement 1 … • Two possibilities (else is used) Syntax: if condition: statement 1 … else: statement 2 … • More than two possibilities. • elif is used. Syntax: if condition: statement 1 elif condition: statement 2 elif condition: statement 3 else: statement 4 • Another Condition in one condition Syntax: if condition1: if condition2: statement 1 else: statement 2 else: statement 3 Conditionals
  • 11. Conditional execution Alternative execution Chained conditionals Nested conditionals • Conditions change the flow of program execution • if statement is used in conditions Syntax: if condition: statement 1 … • Two possibilities (else is used) Syntax: if condition: statement 1 … else: statement 2 … • More than two possibilities. • elif is used. Syntax: if condition: statement 1 elif condition: statement 2 elif condition: statement 3 else: statement 4 • Another Condition in one condition Syntax: if condition1: if condition2: statement 1 else: statement 2 else: statement 3
  • 12. Python Programming Unit – II (Part II) (Lecture 11) Conditionals and Recursion
  • 13. • There are three logical operators: 1. and 2. or 3. not Relational operators: • A = = B(Equal to) • A! = B (Not equal to) • A > B (Greater than) • A < B (Less than) • A > = B (Greater than or equal to) • A < = B (Less than or equal to) A Boolean expression is either true or false. • Ex: A = 5, B = 5, C = 6 • A = = B returns True • A = = C returns False Floor division: • Digits after the decimal point are removed. • Symbol for Floor division: // • Ex: 9 / 2 is 4.5 (Division) 9 // 2 is 4 (Floor division) Modulus: • Returns the remainder value • Symbol for Modulus: % • Ex: 9 % 2 is 1 Floor division and modulus Boolean expressions Logical operators
  • 14. Conditional execution Alternative execution Chained conditionals Nested conditionals • Conditions change the flow of program execution • if statement is used in conditions Syntax: if condition: statement 1 … • Two possibilities (else is used) Syntax: if condition: statement 1 … else: statement 2 … • More than two possibilities. • elif is used. Syntax: if condition: statement 1 elif condition: statement 2 elif condition: statement 3 else: statement 4 • Another Condition in one condition Syntax: if condition1: if condition2: statement 1 else: statement 2 else: statement 3
  • 15. Syllabus Lecture 9 Lecture 10 Lecture 11 Floor division and modulus Boolean expressions Logical operators Conditional execution Alternative execution Chained conditionals Nested conditionals Recursion Infinite Recursion Keyboard input
  • 17. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n 4 If n < 0 False Print(n) n = n-1 Output: 4
  • 18. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n 3 If n < 0 False Print(n) n = n-1 Call count down Output: 4 3
  • 19. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n 2 If n < 0 False Print(n) n = n-1 Call count down Output: 4 3 2
  • 20. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n 1 If n < 0 False Print(n) n = n-1 Call count down Output: 4 3 2 1
  • 21. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n 0 If n < 0 False Print(n) n = n-1 Call count down Output: 4 3 2 1 0
  • 22. Recursion • Function to call itself Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n = n - 1 countdown(n) Countdown(n) n -1 If n < 0 False Print(n) n = n-1 Call count down Output: 4 3 2 1 0 Blast Print(“Blast”) True
  • 24. Countdown(n) n 4 If n < 0 False Print(n) n = n+1 Output: 4 Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) n=n+1 countdown(n)
  • 25. Countdown(n) n 5 If n < 0 False Print(n) n = n+1 Call count down Output: 4 5 Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) countdown(n+1)
  • 26. Countdown(n) n 6 If n < 0 False Print(n) n = n+1 Call count down Output: 4 5 6 Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) countdown(n+1)
  • 27. Countdown(n) n 7 If n < 0 False Print(n) n = n+1 Call count down Output: 4 5 6 7 Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) countdown(n+1)
  • 28. Countdown(n) n 8 If n < 0 False Print(n) n = n+1 Call count down Output: 4 5 6 7 8 Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) countdown(n+1)
  • 29. Countdown(n) n 9 If n < 0 False Print(n) n = n+1 Call count down Output: 4 5 6 7 8 Infinite recursion Infinite Recursion • Making recursive calls forever • The program never terminates. Ex: def countdown(n): if n < 0: print (“Blast”) else: print (n) countdown(n+1)
  • 31. Keyboard input • Accept input from the user with keyboard. • input function is used A = 5 B = 6 print( A + B ) is 11 Here A & B variables has fixed values (given in the program) A = int(input("enter a value:")) B = int(input("enter b value:")) print( A + B) Here A & B variables has no fixed values (given in runtime)