Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Python Conditional Statement - if, elif, else Statements

$
0
0

Conditional statement is used to execute a set of statements based on whether the condition is true or not. For conditional execution of statements if statement is used in Python. You can also have conditional branching using if-else and if-elif-else statements in Python.

Python if-elif-else statement syntax

General syntax for if-elif-else statement in Python is as follows-


if boolean_expression1:
suite1
elif boolean_expression2:
suite2
...
elif boolean_expressionN:
suiteN
else:
else-suite

Note that there can be zero or more elif statements, even else clause is optional. So we’ll start with if statement examples and move towards full conditional branching using if-elif-else statement in Python.


if statement in Python

if statement is used to execute the body of code when the condition evaluates to true. If it is false then the indented statements (if suite) is not executed.

Python if statement syntax


if condition:
if-suite

Here condition is a boolean expression that evaluates to either true or false. If it evaluates to true if-suite is executed, if expression evaluates to false then the suite if not executed.

Python if statement example


def myfunction():
x = 10
y = 12
if x < y:
print('x is less than y')
print('After executing if statement')

myfunction()

Output


x is less than y
After executing if statement

Indentation for grouping statements in Python

To understand conditional statements and loops in Python better you should also have an understanding of how Python used indentation for grouping statements.

Indentation refers to the white spaces at the beginning of the statement. In Python statements that are indented to the same level are part of the same group known as suite.

By default Python uses 4 spaces for indentation which is configurable.

Indentation in the context of if statement can be explained as given below-

if-else statement in Python

Moving on from a simple if statement towards conditional branching there is if-else statement in Python. In if-else statement if-suite is executed if the condition evaluates to true otherwise else-suite is executed.

Python if-else statement syntax


if condition:
if-suite
else
else-suite

Python if-else statement example


def myfunction():
x = 15
y = 12
if x < y:
diff = y - x
print('In if difference is-', diff)
else:
diff = x - y
print('In else difference is-', diff)

print('After executing if-else statement')

myfunction()

Output


In else difference is- 3
After executing if-else statement

Condition (x < y) evaluates to false therefore else statement is executed.

if-elif-else statement in Python

If you have conditional branching based on multiple conditions then you can use if-elif-else statement in Python.

Python if-elif-else statement syntax


if condition1:
if-suite-1
elif condition2:
elif-suite-2
elif condition3:
elif-suite-3
.....
.....
elif conditionN:
elif-suite-N
else:
else-suite

Initially condition1 is evaluated if it is true then if-suite is executed, if condition1 is false then condition2 is evaluated if that is true then elif-suite-2 is executed. If condition 2 is false then condition 3 is evaluated and so on. If none of the condition is true then else-suite is executed.

Python if-elif-else statement example


def myfunction():
invoice_amount = 2000
tarrif = 0
if invoice_amount > 2500 and zone == "Americas":
tarrif = 100
elif invoice_amount > 2500 and zone == "EU":
tarrif = 150
elif invoice_amount > 2500 and zone == "APAC":
tarrif = 200
else:
tarrif = 250
print('Tarrif is', tarrif)

myfunction()

Output


Tarrif is 250

Nested if statement in Python

You can have nested if-elif-else statement with in if statement upto any depth, only thing is that each nested if statement has to be indented properly.

Nested if statement Python example


def myfunction():
x = 65
if x >= 50:
# nested if statement
if x > 75 and x <= 99:
print('x is greater than 75 but less than 100')
elif x >= 50 and x <= 75:
print('x is greater than or equal to 50 but less than or equal to 75')
else:
print('x is greater than 100')
else:
print('x is less than 50')

myfunction()

Output


x is greater than or equal to 50 but less than or equal to 75

That's all for this topic Python Conditional Statement - if, elif, else Statements. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Abstraction in Python
  2. Operator Overloading in Python
  3. Magic Methods in Python With Examples
  4. self in Python
  5. Abstract Class in Python

You may also like -

  1. Automatic Numeric Type Promotion in Java
  2. String join() Method And StringJoiner Class in Java
  3. Converting String to int - Java Program
  4. Invoking Getters And Setters Using Reflection - Java Program
  5. Difference Between ArrayList And CopyOnWriteArrayList in Java
  6. equals() And hashCode() Methods in Java
  7. Spring depends-on Attribute
  8. Installing Hadoop on a Single Node Cluster in Pseudo-Distributed Mode

Viewing all articles
Browse latest Browse all 938

Trending Articles