0% found this document useful (0 votes)
5 views

VKS Pythonselection

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

VKS Pythonselection

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Conditional flow in

Python
What are logical expression
 Logical expression is an expression which
contain relational or logical operators and
return a Boolean value True or False.
 Relational operators are > ,<,<=,>=,== !=
 Relational operators are used to compare 2
values and return a Boolean value True or
False
 Membership operator ‘in’ and identity operator
‘is’ also used to create an expression which
result in True or False
Identity comparison
The operators "is" and "is not" are used to
compare for object identity: "x is y" is True if and
only if x and y are the same object, i.e., they refer
to the same memory location. Object identity is
determined using the "id()" function.

is vs == : is operators checks whether two objects


have the same identity (refer to the same memory
location),
whereas == has nothing to do with the identity, it just
checks whether two objects have the same value.
For example:
a=10 b=10.0
print(a is b) #False
print(a==b) #True
c=10
print(a is c) #True
print(a==c) #True
d=b
print(d is b) #True
print(d==b) #True
in operator checks for memberships.
That is, it checks if its left operand is a
member of its right operand.
Right operand has to be of iterable
/sequence/collection type

>> ‘a’ in ‘abcd’


True
>> ‘A’ in ‘AEIOU’
False
>>10 in (10,20,30)
True
 Mathematically Boolean value
 True equal to 1
 False is equal to 0.
 Any Value other than 0 will return True when
used in conditional statement even if it is
negative
True , False are keywords
What Python’s interpretation of 0 (zero), non-zero, empty
string and non-empty sting in a logical expression (condition)

>>> print(bool(20==20), bool(2.5==2.5),


bool('FAIPS'=='FAIPS’))
Displays True True True
because if a logical expression (condition) is true,
then bool() returns True.

>>> print(bool(10==20), bool(3.5==2.5), bool('FAPS'=='FAIPS’))


Displays False False False
because if a logical expression (condition) is false,
then bool() returns False.

>>> print(bool(8), bool(-9), bool(4.3), bool(-2.5), bool('DPS’))


Displays True True True True True
because any non-zero value or non-empty string is treated as
>>> print(bool(0), bool(0.0), bool(‘’))
Displays False False False because any (0) zero value or
empty string ('') is treated as False by Python.
Python - Decision Control
By default, statements in the script are executed
sequentially from the first to the last.
If the processing logic requires so, the sequential flow
can be altered using decision making statement
In Python, the selection statements are also known as
decision making statements or branching statements.
The selection statements are used to select a part of
the program to be executed based on a condition.
Python language provide the following conditional
(Decision making) statements.

 if statement
 if...else statement
 if...elif...else staement
 Nested if..else statement
if Boolean Expression:
STATEMENT(S)
# indentation for block must
The if statement starts with the if keyword
followed by the conditional expression.
The EXPRESSION must be followed by (:)
colon.

If the EXPRESSION evaluates to True,


the STATEMENT gets executed.
If EXPRESSION returns False, nothing
happens, the STATEMENT gets ignored.
Statement immediately after if is
executed.
STATEMENT be any statement, including
multiple statements or further
nested if statements.
 Elements of the block must all be indented
the same number of spaces/tabs
 Python only recognizes block when they are
indented the same distance (standard is 4
spaces)
 You must be careful to get the indentation
right to get suites right.
if-else statement provides a way to change program flow
based on a condition. It provides two different routines based on
condition Rule1:
if (condition):
statement1/ block1
else:
statement2 / block2
 If the condition is True then the
statement1 or block1 is executed
and the statement or the block
after the else is ignored.

 If the condition is False then the


statement or block after the
condition is ignored and the
statement2 or block2 is executed.

You might also like