Condition Using If
Condition Using If
com
Learning Outcomes
Empty Statement
Simple Statement
Compound Statement
Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
Sequence
It means python statements are executed
one after another i.e. from the first statement
to last statement without any jump.
Selection
It means execution of statement will depend
upon the condition. If the condition is true
then it will execute Action 1 otherwise
Action 2. In Python we use if to perform
selection
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Selection
Action 1
True
Condition ? Statement 1 Statement 2
False
Statement 1
Action 2
Statement 2
Selection
Iteration - Looping
Iteration - Looping
False
Condition ? Exit from iteration
True
Statement 1
Loop Body
Python supports for and
Action 2
Statement 2
while statement for
Looping construct
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Syntax Error
It occurs before the execution of program.
It occurs due to violation of programming language rule
for e.g. missing parenthesis, incorrect use of operators
etc.
Semantic Error
Logical Error
Runtime Error
if Statement of Python
‘if’ statement of python is used to execute
statements based on condition. It tests the
condition and if the condition is true it
perform certain action, we can also provide
action for false situation.
if statement in Python is of many forms:
if without false statement
if with else
if with elif
Nested if
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Simple “if”
In the simplest form if statement in Python
checks the condition and execute the
statement if the condition is true and do
nothing if the condition is false.
Syntax: All statement
belonging to if
if condition: must have same
indentation level
Statement1
Statements ….
** if statement is compound statement having
header and a body containing intended
statement.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
bonus = 0
sale = int(input("Enter Monthly Sales :"))
if sale>50000:
bonus=sale * 10 /100
print("Bonus = " + str(bonus))
if with else
To Do…
1. WAP to enter any number and check it is even or
odd
2. WAP to enter any age and check it is teenager or
not
3. WAP to enter monthly sale of Salesman and give
him commission i.e. if the monthly sale is more
than 500000 then commision will be 10% of
monthly sale otherwise 5%
4. WAP to input any year and check it is Leap Year or
Not
5. WAP to Input any number and print Absolute
value of that number
6. WAP to input any number and check it is positive
or negative number
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
if with elif
if with elif is used where multiple chain of condition is to
be checked. Each elif must be followed by condition: and
then statement for it. After every elif we can give else
which will be executed if all the condition evaluates to
false
Syntax:
if condition:
Statements
elif condition:
Statements
elif condition:
Statements
else:
Statement
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
import math
print("For quadratic equation, ax**2 + bx + c = 0,
enter coefficients ")
a = int(input("enter a"))
b = int(input("enter b"))
c = int(input("enter c"))
if a==0:
print("Value of ",a," should not be zero")
print("Aborting!!!")
else:
delta = b*b - 4 *a * c
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
if delta >0:
root1=(-b+math.sqrt(delta))/(2*a)
root2=(-b-math.sqrt(delta))/(2*a)
print("Roots are Real and UnEqual")
print("Root1=",root1,"Root2=",root2)
elif delta==0:
root1=-b/(2*a)
print("Roots are Real and Equal")
print("Root1=",root1,"Root2=",root1)
else:
print("Roots are Complex and
Imaginary") VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
To Do…
1. WAP to enter marks of 5 subject and calculate total, percentage
and also division.
Percentage Division
>=60 First
>=45 Second
>=33 Third
otherwise Failed
2. WAP to enter any number and check it is positive, negative or zero
number
3. WAP to enter Total Bill amount and calculate discount as per given
table and also calculate Net payable amount (total bill – discount)
Total Bill Discount
>=20000 15% of Total Bill
>=15000 10% of Total Bill
>=10000 5% of Total Bill
otherwise 0 of Total Bill
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
To Do…
4. WAP to enter Bill amount and ask the user the payment
mode and give the discount based on payment mode. Also
display net payable amount
Mode Discount
Credit Card 10% of bill amount
Debit Card 5% of bill amount
Net Banking 2% of bill amount
otherwise 0
5. WAP to enter two number and ask the operator (+ - * / )
from the user and display the result by applying operator on
the two number
6. WAP to enter any character and print it is Upper Case,
Lower Case, Digit or symbol
7. WAP to enter 3 number and print the largest number
8. WAP to input day number and print corresponding day
name for e.g if input is 1 output should be SUNDAY and so on.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Nested if
In this type of “if” we put if within another if as a
statement of it. Mostly used in a situation where we
want different else for each condition. Syntax:
if condition1:
if condition2:
statements
else:
statements
elif condition3:
statements
else:
statements
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Storing Condition