SlideShare a Scribd company logo
1 of 38
Download to read offline
Ms.Mhaske N.R.(PPS) 1
Unit II
Decision Control Statements
Ms.Mhaske N.R.(PPS) 2
Decision Control Statements
There comes situations in real life when we need to make some decisions and based on these
decisions, we decide what should we do next. Similar situations arises in programming also where we
need to make some decisions and based on these decision we will execute the next block of code.
Decision making statements in programming languages decides the direction of flow of program execution.
Decision making statements available in python are:
● if statement
● if..else statements
● nested if statements
● if-elif ladder
Ms.Mhaske N.R.(PPS) 3
if statement
● if statement is the most simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e if a certain condition is true then a block of
statement is executed otherwise not.
● Syntax:
if condition:
# Statements to execute if
# condition is true
●
Here, condition after evaluation will be either true or false. if statement accepts boolean values – if
the value is true then it will execute the block of statements below it otherwise not. We can use
condition with bracket ‘(‘ ‘)’ also.
●
As we know, python uses indentation to identify a block. So the block under an if statement will be identified
as shown in the below example:
if condition:
statement1
statement2
● # Here if the condition is true, if block
●
# will consider only statement1 to be inside
● # its block.
Ms.Mhaske N.R.(PPS) 4
Flow chart
Ms.Mhaske N.R.(PPS) 5
●Python Conditions and If
statements
● Python supports the usual logical conditions from mathematics:
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
● These conditions can be used in several ways, most commonly in "if statements" and loops.
● An "if statement" is written by using the if keyword.
● Example
● If statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
Ms.Mhaske N.R.(PPS) 6
Indentation
● Python relies on indentation (whitespace at the beginning of
a line) to define scope in the code. Other programming
languages often use curly-brackets for this purpose.
● Example
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Ms.Mhaske N.R.(PPS) 7
nested-if
●
A nested if is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement. Yes, Python allows us to nest if
statements within if statements. i.e, we can place an if statement inside another if statement.
●
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Ms.Mhaske N.R.(PPS) 8
Flowchart for nested if
Ms.Mhaske N.R.(PPS) 9
# python program to illustrate nested If statement
i = 10
if (i == 10):
# First if statement
if (i < 15):
print ("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
Ms.Mhaske N.R.(PPS) 10
if- else
● The if statement alone tells us that if a condition is true it will execute
a block of statements and if the condition is false it won’t. But what if
we want to do something else if the condition is false. Here comes the
else statement. We can use the else statement with if statement to
execute a block of code when the condition is false.
● Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Ms.Mhaske N.R.(PPS) 11
Flow chart for if-else
Ms.Mhaske N.R.(PPS) 12
Program for if-else :to check greatest number
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Ms.Mhaske N.R.(PPS) 13
if-elif-else ladder
● Here, a user can decide among multiple options. The if statements are executed
from the top down. As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed.
● Syntax:-
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
Ms.Mhaske N.R.(PPS) 14
Flowchart
Ms.Mhaske N.R.(PPS) 15
● Python program to illustrate if-elif-else ladder
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
Output:
i is 20
Ms.Mhaske N.R.(PPS) 16
And
● The and keyword is a logical operator, and is used to combine conditional
statements:
● Example
Test if a is greater than b, AND if c is greater than a:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Ms.Mhaske N.R.(PPS) 17
Or
● The or keyword is a logical operator, and is used to combine conditional
statements:
● Example
Test if a is greater than b, OR if a is greater than c:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Ms.Mhaske N.R.(PPS) 18
For loop
● The for loop in Python is used to iterate over a sequence (list,
tuple, string) or other iterable objects. Iterating over a sequence is
called traversal.
● Syntax of for Loop
for val in sequence:
Body of for
● Here, val is the variable that takes the value of the item inside the
sequence on each iteration.
● Loop continues until we reach the last item in the sequence. The
body of for loop is separated from the rest of the code using
indentation.
Ms.Mhaske N.R.(PPS) 19
Flowchart
Ms.Mhaske N.R.(PPS) 20
Example
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
# Output: The sum is 48
print("The sum is", sum)
Output :
The sum is 48
Ms.Mhaske N.R.(PPS) 21
The range() function
● We can generate a sequence of numbers using
range() function. range(10) will generate numbers
from 0 to 9 (10 numbers).
● We can also define the start, stop and step size as
range(start,stop,step size). step size defaults to 1 if
not provided.
● This function does not store all the values in memory,
it would be inefficient. So it remembers the start, stop,
step size and generates the next number on the go.
● To force this function to output all the items, we can
use the function list().
Ms.Mhaske N.R.(PPS) 22
The following example will clarify
this.
# Output: range(0, 10)
print(range(10))
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(10)))
# Output: [2, 3, 4, 5, 6, 7]
print(list(range(2, 8)))
# Output: [2, 5, 8, 11, 14, 17]
print(list(range(2, 20, 3)))
Ms.Mhaske N.R.(PPS) 23
For with The break Statement
● With the break statement we can stop the loop before it has looped through all the items:
● Example
● Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
Break
Output :apple
banana
● Example
● Exit the loop when x is "banana", but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Output :
Apple
Ms.Mhaske N.R.(PPS) 24
For with The continue Statement
● With the continue statement we can stop the current iteration of
the loop, and continue with the next:
● Example
● Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Output :apple
cherry
Ms.Mhaske N.R.(PPS) 25
for loop with else
● A for loop can have an optional else block as well. The else part is executed if the
items in the sequence used in for loop exhausts.
● break statement can be used to stop a for loop. In such case, the else part is
ignored.
● Hence, a for loop's else part runs if no break occurs.
● Here is an example to illustrate this.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
Output :
0
1
5
No items left.
Ms.Mhaske N.R.(PPS) 26
While loop
● The while loop in Python is used to iterate over a block of code as long
as the test expression (condition) is true.
● We generally use this loop when we don't know beforehand, the
number of times to iterate.
● Syntax of while Loop in Python
while test_expression:
Body of while
● In while loop, test expression is checked first. The body of the loop is
entered only if the test_expression evaluates to True. After one
iteration, the test expression is checked again. This process continues
until the test_expression evaluates to False.
● In Python, the body of the while loop is determined through indentation.
● Body starts with indentation and the first unindented line marks the
end.
● Python interprets any non-zero value as True. None and 0 are
interpreted as False.
Ms.Mhaske N.R.(PPS) 27
Flowchart
Ms.Mhaske N.R.(PPS) 28
Example
# Program to add natural
# numbers upto
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
OUTPUT :
Enter n: 10
The sum is 55
Ms.Mhaske N.R.(PPS) 29
while loop with else
● Same as that of for loop, we can have an
optional else block with while loop as well.
● The else part is executed if the condition in the
while loop evaluates to False.
● The while loop can be terminated with a break
statement. In such case, the else part is
ignored. Hence, a while loop's else part runs if
no break occurs and the condition is false.
Ms.Mhaske N.R.(PPS) 30
Example to illustrate
# the use of else statement
# with the while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
Output:
Inside loop
Inside loop
Inside loop
Inside else
Ms.Mhaske N.R.(PPS) 31
While with The break Statement
● The break Statement
● With the break statement we can stop the loop even if the while condition
is true:
● Example
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
I += 1
Output:
1
2
3
Ms.Mhaske N.R.(PPS) 32
While with The continue Statement
● With the continue statement we can stop the current iteration,
and continue with the next:
● Example
Continue to the next iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Output:
1 2 4 5 6
Ms.Mhaske N.R.(PPS) 33
What is the use of break and
continue in Python?
● In Python, break and continue statements can alter the flow of a
normal loop.
● Loops iterate over a block of code until test expression is false, but
sometimes we wish to terminate the current iteration or even the
whole loop without checking test expression.
● The break and continue statements are used in these cases.
● Python break statement
● The break statement terminates the loop containing it. Control of the
program flows to the statement immediately after the body of the
loop.
● If break statement is inside a nested loop (loop inside another loop),
break will terminate the innermost loop.
● Syntax of break
break
Ms.Mhaske N.R.(PPS) 34
Flowchart
Ms.Mhaske N.R.(PPS) 35
Python continue statement
● The continue statement is used to skip the rest
of the code inside a loop for the current iteration
only. Loop does not terminate but continues on
with the next iteration.
● Syntax of Continue
continue
Ms.Mhaske N.R.(PPS) 36
Flowchart
Ms.Mhaske N.R.(PPS) 37
What is pass statement in Python?
● In Python programming, pass is a null statement. The
difference between a comment and pass statement in Python
is that, while the interpreter ignores a comment entirely, pass
is not ignored.
● However, nothing happens when pass is executed. It results
into no operation (NOP).
● Syntax of pass
pass
● We generally use it as a placeholder.
● Suppose we have a loop or a function that is not implemented
yet, but we want to implement it in the future. They cannot
have an empty body. The interpreter would complain. So, we
use the pass statement to construct a body that does nothing.
Ms.Mhaske N.R.(PPS) 38
Example
# pass is just a placeholder for
# functionality to be added later.
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass

More Related Content

What's hot

RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in cvampugani
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedurepragya ratan
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAMaulik Borsaniya
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityAakash Singh
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Jaganadh Gopinadhan
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiersNilimesh Halder
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...Maulik Borsaniya
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming LanguageExplore Skilled
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on pythonwilliam john
 

What's hot (20)

RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
Loops in c
Loops in cLoops in c
Loops in c
 
C functions
C functionsC functions
C functions
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Python for loop
Python for loopPython for loop
Python for loop
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 

Similar to basic of desicion control statement in python

Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc csKALAISELVI P
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfDrDineshenScientist
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5patcha535
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfsdvdsvsdvsvds
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSclass12sci
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
Hello!Can someone help me to answer Task4 and Task7Complete T.pdf
Hello!Can someone help me to answer Task4 and Task7Complete T.pdfHello!Can someone help me to answer Task4 and Task7Complete T.pdf
Hello!Can someone help me to answer Task4 and Task7Complete T.pdfforwardcom41
 

Similar to basic of desicion control statement in python (20)

Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
PRESENTATION.pptx
PRESENTATION.pptxPRESENTATION.pptx
PRESENTATION.pptx
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Python session3
Python session3Python session3
Python session3
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CS
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
 
Hello!Can someone help me to answer Task4 and Task7Complete T.pdf
Hello!Can someone help me to answer Task4 and Task7Complete T.pdfHello!Can someone help me to answer Task4 and Task7Complete T.pdf
Hello!Can someone help me to answer Task4 and Task7Complete T.pdf
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 

Recently uploaded

An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

basic of desicion control statement in python

  • 1. Ms.Mhaske N.R.(PPS) 1 Unit II Decision Control Statements
  • 2. Ms.Mhaske N.R.(PPS) 2 Decision Control Statements There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arises in programming also where we need to make some decisions and based on these decision we will execute the next block of code. Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are: ● if statement ● if..else statements ● nested if statements ● if-elif ladder
  • 3. Ms.Mhaske N.R.(PPS) 3 if statement ● if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. ● Syntax: if condition: # Statements to execute if # condition is true ● Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also. ● As we know, python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example: if condition: statement1 statement2 ● # Here if the condition is true, if block ● # will consider only statement1 to be inside ● # its block.
  • 5. Ms.Mhaske N.R.(PPS) 5 ●Python Conditions and If statements ● Python supports the usual logical conditions from mathematics: ● Equals: a == b ● Not Equals: a != b ● Less than: a < b ● Less than or equal to: a <= b ● Greater than: a > b ● Greater than or equal to: a >= b ● These conditions can be used in several ways, most commonly in "if statements" and loops. ● An "if statement" is written by using the if keyword. ● Example ● If statement: a = 33 b = 200 if b > a: print("b is greater than a")
  • 6. Ms.Mhaske N.R.(PPS) 6 Indentation ● Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose. ● Example If statement, without indentation (will raise an error): a = 33 b = 200 if b > a: print("b is greater than a") # you will get an error
  • 7. Ms.Mhaske N.R.(PPS) 7 nested-if ● A nested if is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement. ● Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here # if Block is end here
  • 9. Ms.Mhaske N.R.(PPS) 9 # python program to illustrate nested If statement i = 10 if (i == 10): # First if statement if (i < 15): print ("i is smaller than 15") # Nested - if statement # Will only be executed if statement above # it is true if (i < 12): print ("i is smaller than 12 too") else: print ("i is greater than 15") Output: i is smaller than 15 i is smaller than 12 too
  • 10. Ms.Mhaske N.R.(PPS) 10 if- else ● The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false. ● Syntax: if (condition): # Executes this block if # condition is true else: # Executes this block if # condition is false
  • 11. Ms.Mhaske N.R.(PPS) 11 Flow chart for if-else
  • 12. Ms.Mhaske N.R.(PPS) 12 Program for if-else :to check greatest number a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a")
  • 13. Ms.Mhaske N.R.(PPS) 13 if-elif-else ladder ● Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. ● Syntax:- if (condition): statement elif (condition): statement . . else: statement
  • 15. Ms.Mhaske N.R.(PPS) 15 ● Python program to illustrate if-elif-else ladder i = 20 if (i == 10): print ("i is 10") elif (i == 15): print ("i is 15") elif (i == 20): print ("i is 20") else: print ("i is not present") Output: i is 20
  • 16. Ms.Mhaske N.R.(PPS) 16 And ● The and keyword is a logical operator, and is used to combine conditional statements: ● Example Test if a is greater than b, AND if c is greater than a: a = 200 b = 33 c = 500 if a > b and c > a: print("Both conditions are True")
  • 17. Ms.Mhaske N.R.(PPS) 17 Or ● The or keyword is a logical operator, and is used to combine conditional statements: ● Example Test if a is greater than b, OR if a is greater than c: a = 200 b = 33 c = 500 if a > b or a > c: print("At least one of the conditions is True")
  • 18. Ms.Mhaske N.R.(PPS) 18 For loop ● The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal. ● Syntax of for Loop for val in sequence: Body of for ● Here, val is the variable that takes the value of the item inside the sequence on each iteration. ● Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
  • 20. Ms.Mhaske N.R.(PPS) 20 Example numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11] # variable to store the sum sum = 0 # iterate over the list for val in numbers: sum = sum+val # Output: The sum is 48 print("The sum is", sum) Output : The sum is 48
  • 21. Ms.Mhaske N.R.(PPS) 21 The range() function ● We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers). ● We can also define the start, stop and step size as range(start,stop,step size). step size defaults to 1 if not provided. ● This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go. ● To force this function to output all the items, we can use the function list().
  • 22. Ms.Mhaske N.R.(PPS) 22 The following example will clarify this. # Output: range(0, 10) print(range(10)) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list(range(10))) # Output: [2, 3, 4, 5, 6, 7] print(list(range(2, 8))) # Output: [2, 5, 8, 11, 14, 17] print(list(range(2, 20, 3)))
  • 23. Ms.Mhaske N.R.(PPS) 23 For with The break Statement ● With the break statement we can stop the loop before it has looped through all the items: ● Example ● Exit the loop when x is "banana": fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": Break Output :apple banana ● Example ● Exit the loop when x is "banana", but this time the break comes before the print: fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": break print(x) Output : Apple
  • 24. Ms.Mhaske N.R.(PPS) 24 For with The continue Statement ● With the continue statement we can stop the current iteration of the loop, and continue with the next: ● Example ● Do not print banana: fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": continue print(x) Output :apple cherry
  • 25. Ms.Mhaske N.R.(PPS) 25 for loop with else ● A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts. ● break statement can be used to stop a for loop. In such case, the else part is ignored. ● Hence, a for loop's else part runs if no break occurs. ● Here is an example to illustrate this. digits = [0, 1, 5] for i in digits: print(i) else: print("No items left.") Output : 0 1 5 No items left.
  • 26. Ms.Mhaske N.R.(PPS) 26 While loop ● The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. ● We generally use this loop when we don't know beforehand, the number of times to iterate. ● Syntax of while Loop in Python while test_expression: Body of while ● In while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False. ● In Python, the body of the while loop is determined through indentation. ● Body starts with indentation and the first unindented line marks the end. ● Python interprets any non-zero value as True. None and 0 are interpreted as False.
  • 28. Ms.Mhaske N.R.(PPS) 28 Example # Program to add natural # numbers upto # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum) OUTPUT : Enter n: 10 The sum is 55
  • 29. Ms.Mhaske N.R.(PPS) 29 while loop with else ● Same as that of for loop, we can have an optional else block with while loop as well. ● The else part is executed if the condition in the while loop evaluates to False. ● The while loop can be terminated with a break statement. In such case, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.
  • 30. Ms.Mhaske N.R.(PPS) 30 Example to illustrate # the use of else statement # with the while loop counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print("Inside else") Output: Inside loop Inside loop Inside loop Inside else
  • 31. Ms.Mhaske N.R.(PPS) 31 While with The break Statement ● The break Statement ● With the break statement we can stop the loop even if the while condition is true: ● Example Exit the loop when i is 3: i = 1 while i < 6: print(i) if i == 3: break I += 1 Output: 1 2 3
  • 32. Ms.Mhaske N.R.(PPS) 32 While with The continue Statement ● With the continue statement we can stop the current iteration, and continue with the next: ● Example Continue to the next iteration if i is 3: i = 0 while i < 6: i += 1 if i == 3: continue print(i) Output: 1 2 4 5 6
  • 33. Ms.Mhaske N.R.(PPS) 33 What is the use of break and continue in Python? ● In Python, break and continue statements can alter the flow of a normal loop. ● Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression. ● The break and continue statements are used in these cases. ● Python break statement ● The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. ● If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop. ● Syntax of break break
  • 35. Ms.Mhaske N.R.(PPS) 35 Python continue statement ● The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration. ● Syntax of Continue continue
  • 37. Ms.Mhaske N.R.(PPS) 37 What is pass statement in Python? ● In Python programming, pass is a null statement. The difference between a comment and pass statement in Python is that, while the interpreter ignores a comment entirely, pass is not ignored. ● However, nothing happens when pass is executed. It results into no operation (NOP). ● Syntax of pass pass ● We generally use it as a placeholder. ● Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter would complain. So, we use the pass statement to construct a body that does nothing.
  • 38. Ms.Mhaske N.R.(PPS) 38 Example # pass is just a placeholder for # functionality to be added later. sequence = {'p', 'a', 's', 's'} for val in sequence: pass