2.conditional Statement
2.conditional Statement
1
Python | CCIT
Table of Contents
Conditional statements ........................................ 3
if…else Statements ............................................... 3
Boolean expressions ............................................ 4
Nested If Statement ............................................. 6
if…elif…else Statement ........................................ 9
Single Line if Statement ...................................... 12
match…case statement ...................................... 14
Multiple case value ............................................ 16
Conditional case value........................................ 18
Variable case value ............................................ 20
2
Python | CCIT
Conditional statements
It is used to these are statements within our program which are conditionally
executed.
To conditionally execute statements C++ provides us different control
structures and operators.
o if statement
o Single-line if statement
o match…case statement
if…else Statements
It is used to conditionally execute a block of statements
If condition is true then statements within if block are executed and then
program control is transferred to next statements.
If condition is false then statements within else block are executed and then
program control is transferred to next statements.
Note: else block is optional.
Syntax:
if condition:
Statements
----------
else:
Statements
----------
next statements
3
Python | CCIT
Boolean expressions
Condition can be specified by using a Boolean expression i.e. an expression
whose result is True or False.
Boolean expression can be created by using relational and logical operators. A
Boolean expression is just another name for a conditional test. A Boolean
value is either True or False, just like the value of a conditional expression after
it has been evaluated.
o Relational Operators: < , > , <= , >= , == , !=
o Logical Operators: and , or , not
WAP to read a number and check if it is an even or odd
4
Python | CCIT
Enter a no.: 15
2 digit Number
Enter a no.: 15
2 digit Number
Sum of digit is 6
5
Python | CCIT
Nested If Statement
If a if statement is used within if statement then such a control structure is
called as nested if.
Nested if is used to condition only if another condition is true.
elif and else blocks are optional.
Syntax:
if condition :
if condition :
Statements
----------
----------
next statements
Enter a no.: 28
Number is Positive
28 is Even
6
Python | CCIT
Enter a no.: 28
Number is Positive
It is 2 digit no.
WAP to read 3 angles and check if triangle can be formed or not.If triangle
can be formed then check if it is equilateral Triangle or isosceles triangle
or right angled triangle..
a,b,c=input("Enter 3 Angles :").split()
a,b,c=int(a),int(b),int(c)
if a+b+c==180:
print("Triangle can be formed")
if a==b==c:
print("Equilateral Triangle")
if a==b or b==c or c==a:
print("Isosceles Triangle")
if a==90 or b==90 or c==90:
print("Right angled Triangle")
else:
print("Triangle cannot be formed..")
7
Python | CCIT
Enter 3 Angles: 45 90 45
Triangle can be formed
Isosceles Triangle
Right angled Triangle
Enter a no.: 66
It is 2 digit no.
Both digit are same
8
Python | CCIT
if…elif…else Statement
The elif is short for else if. It allows us to check for multiple expressions. If the
condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, body of else is executed. 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.
Syntax:
if condition :
if condition :
Statements
----------
----------
next statements
Enter a no.: 28
Number is Positive
9
Python | CCIT
Enter 3 nos 48 71 22
71 is Greatest
10
Python | CCIT
Enter Precentage 72
Passed with 1st class
11
Python | CCIT
z = a if a>b else b
print("Greatest Number is",z)
Enter a Number: 22
Enter a Number: 28
Greatest Number is 28
WAP to read 2 different nos and find greatest of them.
a=int(input("a="))
b=int(input("b="))
c=int(input("c="))
d=int(input("d="))
a= 50
b= 30
c= 20
d= 70
Greatest Number is 70
netsal = basic+hra+ta-itax
print("Net Salary ",netsal)
13
Python | CCIT
match…case statement
It is used to check a variable/ expression for different values.
According to value of the variable different cases are executed i.e. program
control enters match block at different points.
If no matching case is found then code in the case _ block is executed.
case _ block is optional..
Syntax:
match variable:
case value:
----------
----------
case value:
----------
----------
case value:
----------
----------
:
:
case _:
----------
----------
14
Python | CCIT
Enter a Number: 2
Two
WAP to read month in digits and print it in words..
n=int(input("Enter a number"))
match n:
case 1:print("January")
case 2:print("February")
case 3:print("March")
case 4:print("April")
case 5:print("May")
case 6:print("June")
case 7:print("July")
case 8:print("August")
case 9:print("September")
case 10:print("October")
case 11:print("November")
case 12:print("December")
case _:print("Invalid input") 15
Python | CCIT
Enter a Number: 6
June
16
Python | CCIT
WAP to read shirts code i.e. a char value and print output according to
given criteria [s-small, m-medium, ,l-large any other char-invalid input].
n=input("Enter shirt size ")
match n:
case "S"|"s":
print("small")
case "M"|"m":
print("medium")
case "L"|"l":
print("large")
case _:
print(f"cannot find {n} size shirt")
WAP to read color code i.e. a char value and print output according to
given criteria [ r-red, g-green, b-blue, any other char-white].
n=input("Enter color code ")
match n:
case "r"|"R":print("Red")
case "g"|"G":print("Green")
case "b"|"B":print("Blue")
case _:print("White")
Enter color code: b
Blue
17
Python | CCIT
WAP to read color code i.e. a char value and print output according to
given criteria [ r-red, g-green, b-blue, any other char-white].
n=input("Enter color code ")
match n:
case "r"|"R":print("Red")
case "g"|"G":print("Green")
case "b"|"B":print("Blue")
case _:print("White")
match variable:
case value:
----------
----------
case value if condition:
----------
----------
:
:
case _:
----------
----------
18
Python | CCIT
Enter Percentage: 65
Passed with 1st class
19
Python | CCIT
match variable:
case value:
----------
----------
case variable:
----------
----------
:
:
case _:
----------
----------
Example
time=input("Enter time:")
match time.split(":"):
case [h,m]:
print(f"{h} hours and {m} mins")
case [h,m,s]:
print(f"{h} hours {m} mins and {s} sec")
case _:
print("Invalid format")
20
Python | CCIT
21