You are on page 1of 18

Conditional

Statements
in
Python
Conditional Statement is a statement that decides
whether the other statements should be executed
or not.

Usually in Python Programming Language, code


executes in a sequential manner like the first line
will be executed first followed by second line and
so on until the end of the code.

Conditional statements come into picture when we


must decide that a certain part of code should run
only if the condition is True and certain part of
code should run only if the condition is False.
Conditional Statements in Python

if
elif
if-else
nested-if
if statement is used when we must execute a code
block only if a given condition is True.

First the program will evaluate the conditional


expression and will only execute the code block if
the conditional expression is True.

Syntax

if conditional expression:
Statement 1
Statement 2
Indentation in Python

Indentation refers to the spaces at the beginning of a code


line.

In other programming languages the indentation in code is


only for readability, but the indentation in Python is very
important.

Python uses indentation to indicate a block of code.

Python will give you an error if you skip the indentation.


You can surround conditional expression with round
brackets.

if (conditional expression):
Statement 1
Statement 2

The number of spaces is up to you, but it has to be at least


one.

You have to use the same number of spaces in the same


block of code, otherwise Python will give you an error.

We can use following operators in conditional expression


of if statement.
elif statement is used when the previous
condition was false.

elif is short for else if.

It allows us to check for multiple expressions.

If the condition of if block is False, it checks


the condition of the next elif block and so on.
Syntax

if conditional expression:
statement1
statement2
elif conditional expression:
statement1
statement2
else statement is used when all the conditions
were False.

Only one block among the several if...elif...else


blocks is executed according to the condition.

The if block can have only one else block, But it can
have multiple elif blocks.

There will be no conditional expression for else


block.
Syntax

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.

We can have an if...elif...else statement inside another


if...elif...else statement.

Any number of these statements can be nested inside one


another.

Indentation is the only way to figure out the level of


nesting.

we use nested if statement to check multiple conditions.


Python is Object Oriented Programming Language,
so everything in Python is an Object.

The id() function returns an unique id for the


specified object.

All objects in Python has its own unique id.

The id is assigned to the object when it is created.

The id is the object's memory address, and will be


different for each time you run the program.
Comparison Operators
Comparison operators are used to compare multiple
values.

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

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


Logical Operator Description

Returns True
and
if both statements are true

Returns True
or
if one of the statements is true

Reverse the result


not
returns False if the result is True
Identity
Description
Operator

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

If you have only one if statement to execute, you


can write in one line.

This way of writing conditional statement is known


as Ternary Operator.

You might also like