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

Python-If Else

The document discusses Python conditional statements like if, elif and else. It provides syntax and examples to check conditions and execute code blocks based on boolean expressions. Nested conditional statements and if-elif-else ladders are also demonstrated.

Uploaded by

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

Python-If Else

The document discusses Python conditional statements like if, elif and else. It provides syntax and examples to check conditions and execute code blocks based on boolean expressions. Nested conditional statements and if-elif-else ladders are also demonstrated.

Uploaded by

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

Python - if, elif, else Conditions

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

Any Boolean expression evaluating to True or False appears after the if keyword. Use the : symbol


and press Enter after the expression to start a block with an increased indent. One or more
statements written with the same level of indent will be executed if the Boolean expression
evaluates to True.

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")

Example: Multiple Statements in the if Block


price = 50
quantity = 5
if price*quantity < 500:
print("price*quantity is less than 500")
print("price = ", price)
print("quantity = ", quantity)

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

if price > 100:


print("price is greater than 100")
elif price == 100:
print("price is 100")
elif price < 100:
print("price is less than 100")

Example: if-elif-else Conditions


price = 50

if price > 100:


print("price is greater than 100")
elif price == 100:
print("price is 100")
else price < 100:
print("price is less than 100")
Example: Nested if-elif-else Conditions
price = 50
quantity = 5
amount = price*quantity

if amount > 100:


if amount > 500:
print("Amount is greater than 500")
else:
if amount < 500 and amount > 400:
print("Amount is")
elif amount < 500 and amount > 300:
print("Amount is between 300 and 500")
else:
print("Amount is between 200 and 500")
elif amount == 100:
print("Amount is 100")
else:
print("Amount is less than 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

if a > b and a> c and a> d:

print(a)

elif b > c and b > d:

print(b)

elif c> d:

print(c)

else:

print(d)

Practice:

A school has following rules for grading system:


a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.

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)

You might also like