Python Conditional Statements
Python Conditional Statements
Statements
in
Python
Conditional Statement is a statement that decides
whether the other statements should be executed
or not.
if
elif
if-else
nested-if
if statement is used when we must execute a code
block only if a given condition is True.
Syntax
if conditional expression:
Statement 1
Statement 2
Indentation in Python
if (conditional expression):
Statement 1
Statement 2
if conditional expression:
statement1
statement2
elif conditional expression:
statement1
statement2
else statement is used when all the conditions
were False.
The if block can have only one else block, But it can
have multiple elif blocks.
if conditional expression:
Body of if
elif conditional expression:
Body of elif
elif conditional expression:
Body of elif
else:
Body of else
if statement inside if statement is called as nested if
statement.
Logical Operators
Logical operators are used to combine conditional
statements.
Identity Operators
Identity operators are used to compare the objects, not if
they are equal, but if they are actually the same object,
with the same memory location.
Membership Operators
Membership operators are used to test if a sequence is
presented in an object.
Comparison Operator Name
== Equal
!= Not equal
Returns True
and
if both statements are true
Returns True
or
if one of the statements is true
Returns True
is if both variables are the same
object
Returns True
is not if both variables are not the same
object
Membership
Description
Operator
Returns True
in if a sequence with the specified
value is present in the object
Returns True
not in if a sequence with the specified
value is not present in the object
Short Hand If Statement