Python-If Else
Python-If Else
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 in two ways:
Python uses the if keyword to implement decision control. Python's syntax for executing a block
conditionally is as below:
Syntax:
if [boolean expression]:
statement1
statement2
...
statementN
To end the block, decrease the indentation. Subsequent statements after the block will be
executed out of the if condition. The following example demonstrates the if condition.
Example: if Condition
price = 50
if price < 100:
print("price is less than 100")
price = 50
quantity = 5
if price*quantity < 100:
print("price is less than 500")
print("price = ", price)
print("quantity = ", quantity)
print("No if block executed.")
if [boolean expression]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
else:
[statements]
price = 100
a=100
b=202
c=30
if a>b:
if a> c:
print(a)
elif b>c:
print(b)
else:
print(c)
a=10000000000
b=2020000000000
c=3000000000000000
d=50000000
print(a)
print(b)
elif c> d:
print(c)
else:
print(d)
Practice:
Write a program to calculate the electricity bill (accept number of unit from
user) according to the following criteria:
Unit Price
First 100 units no charge
Next 100 units Rs 5 per unit
After 200 units Rs 10 per unit
(For example if input unit is 350 than total bill amount is Rs2000)