Python | CCIT
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
a=input("Enter a Number ") Enter a Number: 24
a=int(a) Number is EVEN
if a%2==0:
print("Number is Even")
else:
print("Number is Odd")
WAP to read 3 angles and check if triangle can be formed or not.
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")
else:
print("Triangle can not be formed")
Enter 3 Angles: 30 100 50
Triangle can be formed
4
Python | CCIT
WAP to read a no. and check if it is a 2 digit no or not.
a=input("Enter a Number: ")
a=int(a)
if a>=10 and a<=99:
print("2 digit Number")
else:
print("Not a 2 digit Number")
Enter a no.: 15
2 digit Number
WAP to read a no. and check if it is a 2 digit no or not. If it is a 2 digit
number then find sum of its digits.
a=input("Enter a Number: ")
a=int(a)
if a>=10 and a<=99:
print("2 digit Number")
x=a%10
y=a//10
s=x+y
print("Sum of digits is ",s)
else:
print("Not a 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
WAP to read a Number and check if it is positive or [Link] number is
Positive then check if it is even or odd.
a=int(input("Enter a Number:"))
if a>0:
print("Number is Positive")
if a%2==0:
print(a,"is Even")
else:
print(a,"is Odd")
else:
print("Number is Not Positive")
Enter a no.: 28
Number is Positive
28 is Even
6
Python | CCIT
WAP to read a Number and check if it is positive or not. If it is a positive
number then check if it is 2 digit number or not.
a=int(input("Enter a Number:"))
if a>0:
print("Number is Positive")
if a>=10 and a<=99:
print("it is a 2 digit Number")
else:
print("it is NOT a 2 digit Number")
else:
print("Number is Not Positive")
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 [Link] 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
WAP to read a number and check if it is a 2 digit no. or not. If it is a 2 digit
no. then check if both digits are same or not.
a=int(input("Enter a Number:"))
if a>=10 and a<=99:
print("It is 2 digit number")
f=a//10
l=a%10
if f==l:
print("Both digit are same")
else:
print("Both digit are not same")
else:
print("It is not 2 digit number")
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
WAP to read a number and check if it is Positive , Negative or Zero.
a=input("Enter a Number ")
a=int(a)
if a>0:
print("Number is Positive")
elif a<0:
print("Number is Negative")
else:
print("Number is Zero")
Enter a no.: 28
Number is Positive
9
Python | CCIT
WAP to read 3 different numbers and find greatest of them.
a,b,c=input("Enter 3 Nos").split()
a,b,c=int(a),int(b),int(c)
if a>b and a>c:
print(a," is Greatest")
elif b>c:
print(b," is Greatest")
else:
print(c," is Greatest")
Enter 3 nos 48 71 22
71 is Greatest
WAP to read salary and print grade according to given criteria.
a=input("Enter Salary") Sal Grade
a=int(a)
>=50000 A
if a>=50000:
print("A Grade") 25000-50000 B
elif a>=25000:
15000-25000 C
print("B Grade")
elif a>=15000: <15000 D
print("C Grade")
else:
print("D Grade")
Enter Salary 35000
B Grade
10
Python | CCIT
WAP to read percentage and print result according to given criteria.
a=input("Enter Percentage") Percentage Result
a=float(a)
if a>=75:
>=75 passed with DT
print("passed with DT") 60-75 passed with 1st class
elif a>=60:
40-60 passed
print("passed with 1st
class") <40 failed
elif a>=40:
print("passed")
else:
print("failed")
Enter Precentage 72
Passed with 1st class
WAP to read gender code and print appropriate gender according to
given criteria..
a=input("Enter Gender Code ") Code Gender
if a=="m" or a=="M":
M - Male
print("Male")
elif a=="f" or a=="F": F - Female
print("Female")
any other char – Invalid Input
else:
print("Invalid Input")
Enter Gender code M
Male
11
Python | CCIT
Single Line if Statement
It is used to conditionally execute simple expression.
If condition is true then statement1 will be evaluated else statement2 will be
evaluated.
Syntax:
statement1 if condition else statement2
WAP to read 2 different nos and find greatest of them.
a=int(input("Enter a Number:"))
b=int(input("Enter a Number:"))
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="))
x=a if a>b else b
y=c if c>d else d
z=x if x>y else y
print("Greatest Number is ",z)
12
Python | CCIT
a= 50
b= 30
c= 20
d= 70
Greatest Number is 70
WAP to read Basic Salary of person and calculate netSalary according to
following criteria. NetSal = Basic + HRA + TA - Itax.
basic=int(input("Enter Basic Salary "))
hra = 5000 if basic>=10000 else 3000
ta = 2000 if basic>=15000 else 1000
itax = 2500 if basic>=20000 else 0
netsal = basic+hra+ta-itax
print("Net Salary ",netsal)
Enter Basic Salary 17000
Net Salary is 24000
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
WAP to read 2 different nos and find greatest of them.
n=int(input("Enter a number"))
match n:
case 0:print("Zero")
case 1:print("One")
case 2:print("Two")
case 3:print("Three")
case 4:print("Four")
case 5:print("Five")
case 6:print("Six")
case 7:print("Seven")
case 8:print("Eight")
case 9:print("Nine")
case _:print("Invalid input")
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
Multiple case value
You check multiple value in case statement.
Syntax:
match variable:
case value:
----------
----------
case value | value:
----------
----------
:
:
case _:
----------
----------
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")
Enter shirt size: M
medium
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")
Enter color code: b
Blue
Conditional case value
You check condition in case statement.
Syntax:
match variable:
case value:
----------
----------
case value if condition:
----------
----------
:
:
case _:
----------
----------
18
Python | CCIT
WAP to read salary and print grade according to given criteria.
sal=int(input("Enter salary ")) Sal Grade
match sal:
>=50000 A
case sal if sal>=50000:
25000-50000 B
print("A Grade")
15000-25000 C
case sal if sal>=25000:
print("B Grade") <15000 D
case sal if sal>=15000:
print("C Grade")
case _:
print("D Grade")
Enter Salary: 35000
B Grade
WAP to read percentage and print result according to given criteria.
p=int(input("Enter Percentage ")) Percentage Result
match p:
case p if p>=75: >=75 passed with DT
print("Passed with DT") 60-75 passed with 1st class
case p if p>=60:
print("Passed with 1st class") 40-60 passed
case p if p>=40: <40 failed
print("Passed")
case _:
print("Failed")
Enter Percentage: 65
Passed with 1st class
19
Python | CCIT
Variable case value
You can use variable in case statement.
Syntax:
match variable:
case value:
----------
----------
case variable:
----------
----------
:
:
case _:
----------
----------
Example
time=input("Enter time:")
match [Link](":"):
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")
Enter time: 2:30
2 hours and 30 mins
20
Python | CCIT
21