Control Structure –if
Control Structure
• Control Structure refers to the program segment which
controls the flow of program execution
Control Structure statements
Sequence Selection Iteration
Repeated execution of a
Execution of Execution of a set of
set of statement
statements is one after statement depending on
depending on a
another a condition.
condition
Sequence
• Refers to the execution when statements are executed one
after another
• Represents the normal flow of control in a program in which
all the statements are executed as they appear
• Represents the default flow of execution
Selection
• Refers to execution of a set of statement depending on a condition.
• If condition is true ,one set of statements is executed otherwise
another set of statement will be executed.
• Also known as conditional statement or decision construct as it
helps in making decision on a course of action
Iteration
• Refers to repeated execution of a set of statement depending on a
condition
• Till the time the test expression of the loop is true, a set of
statements are repeated
• As soon as the test expression becomes False, the repetition stops
• Iterative statements is also known as looping statement
Conditional Construct (Selection)
• Used in that situation when it is required that a set of statements of
a program to be executed on the basis of a test condition being
true or false
• This statement helps a programmer in the selection of one out of
alternative case of action
• In python , if…statement is used for selective execution of a
program segment
Flow chart ( if )
Syntax of an if…statement
if the condition given with if is true then the indented statements given
inside the if is executed
and does nothing when the condition given with if evaluates to false
Syntax:
if condition/expression:
Statement 1
Statement 2
Statements part of
Statement 3 if condition
Statement n
Program
Write a program to accept name and age from user. If the age is more than 18 and
display the name of the person with the message “is an adult”
name =input(“enter name-“)
age = int(input(“Enter your age-“))
if age>=18:
print (name,“is an Adult”)
Program
Write a program to accept percentage of final term of a student from user. If the
percentage is more than or equal to 33 ,display the message “You are promoted to
new class ”
perc=float(input("Enter percentage of final term"))
if perc>=33:
print("You are promoted to new class")
Multiple if in a program
• There can be more than one if statements in a program.
• These if statements are independent of each other
• This means any if statement whose condition is satisfied,
the statements within that if condition also called block,will
be executed
Multiple if…statement
if the condition given with if is true then the statements given inside the if is
executed
and does nothing when the condition given with if evaluates to false
Syntax:
if condition/expression:
Statement 1
Statement 2
Statements of first
Statement 3
if condition
Statement n
:
if condition/expression:
Statement 1
Statement 2 Statements of nth if
Statement 3 condition
Statement n
:
Program
Write a program to accept a number from user. Check if the number is positive,
negative or zero.
n =int(input(“enter number:“))
if n ==0:
print(“Number is zero”)
if n>0:
print(“Number is positive”)
if n<0:
print(“Number is negative”)
Variations of if
There are four variations of if statement in Python
• if statement
• if ….. else statement
• if ….. elif…else statement
• if with logical operator
• Nested if….. else statement
if…else statement
- if … else statement provides alternative execution
- This means there are two course of actions and the truth values of the condition
determines which one gets executed
- If the condition given with if is True, then statements part of it will get executed
otherwise statements part of else will be executed
Syntax:
if condition:
Indented-Statement / Block-For-True condition
else:
Indented-Statement / Block- For-False condition
Flowchart of if..else statement
Pseudocode
To input a number and Check whether the number is odd or even
START
READ number
remainder=number%2
IF remainder==0
WRITE "Even Number"
ELSE
WRITE "Odd Number"
END
Algorithm
To input a number and Check whether the number is odd or even
Step 1: Start
Step 2: Read N
Step 3: Check:
If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
Step 4: End
Flow chart ( if … else )
Program
Write a program to print whether a number entered by user is even or
odd.
num = int(input(“enter a number –“))
if num % 2 == 0:
print (“number is even”)
else:
print( “number is odd”)
Program
Write a program to print adult if age is greater than or equal to 18 otherwise
not an adult. Accept age from user
age=int(input("enter age"))
if age>=18:
print("adult")
else:
print("not an adult")
Program
Write a program to print the square if the number entered is even, otherwise
print its cube.
num=int(input("enter number"))
if num%2==0:
print("square is",num**2)
else:
print("cube is",num**3)
Program
Write a program to accept the 2 sides of a rectangle and display if its area is
greater than its perimeter.
l=int(input("enter length"))
b=int(input("enter breadth"))
area=l*b
peri=2*(l+b)
if area>peri:
print("area is bigger than perimeter")
else:
print("area is smaller than perimeter")
Program
Write a program to Input two numbers and a choice from a user. The program should
display the calculated result after performing the required mathematical operation
depending on the choice
ch= int(input(“1:Addition \n 2:Subtraction\n 3:Multiplication \n 4: Division\n Enter choice”))
a=int(input(“Enter first number”))
b=int(input(“Enter second number”))
if ch==1:
c=a+b
print(c)
if ch==2:
c=a-b
print(c)
if ch==3:
c=a*b
print(c)
if ch==4:
c=a//b
print(c)
Program
ch= int(input(“1:Addition \n 2:Subtraction\n 3:Multiplication \n 4: Division\n Enter choice”))
a=int(input(“Enter first number”))
b=int(input(“Enter second number”))
if ch==1:
c=a+b
print(c)
if ch==2:
c=a-b
print(c)
if ch==3:
c=a*b
print(c)
if ch==4:
c=a//b
print(c)
else:
print(“Wrong choice”)
In the above program, if the else statement is given at the end, this will work only if the
choice is NOT given as 4 .This means if the choice is either 1,2 or 3 and NOT 4, the
message Wrong choice would also be displayed which is logically incorrect.
if..elif… else statement
• if..elif..else statement is used in a situation where user needs to
decide one option among multiple options
• When any one of the given condition is true, then statement(s)
associated with that if get executed.
• if None of the if conditions are satisfied, then statements
associated with else are executed
if..elif… else statement
Syntax : if condition1:
Indented–statements-for-true value – for condition1
elif condition2:
Indented–statements-for-true value – for condition2
elif condition3:
Indented–statements-for-true value – for condition3 :
else:
Indented–statements-for-false-value – of all conditions
• The conditions in if..elif.. else are evaluated from top downwards
• If one of condition is true, statements given under it get executed and
control goes to the statement following if..elif….else
• else clause is optional. When it is given, is placed at the end
• Statements under else will be executed only when all the conditions given
with if/elif is evaluated to false.
Flowchart of if..elif Statement
if..elif… else statement
Example : Write a program to Input two numbers n1 and n2 .If the
first number n1 is bigger , display the message “ n2 is
less than n1”, if second number is bigger, display “n1 is
less than n2” otherwise display “both are equal”
Program : n1 = int(input(“Enter first number –“))
n2 = int(input(“Enter second number -))
if n1 < n2:
print( “n1 is less than n2”)
elif n2 < n1:
print ( “n2 is less than n1”)
else:
print (“both are equal”)
if..elif… else statement
Example : Write a program to Input two numbers and a character representing an operator. The
program should display the calculated result after performing the required simple
mathematical operation as addition , subtraction , multiplication or division.
Program : n1 = int(input(“Enter first number”))
n2 = int(input(“Enter second number”))
op = input(“+:Addition \n -:Subtraction\n *:Multiplication \n /: Division\n Enter choice”)
if op == ‘+’:
res = n1 + n2
print(“Calculated Result –”,res)
elif op == ‘-’:
res = n1 - n2
print(“Calculated Result –”,res)
elif op == ‘*’:
res = n1 * n2
print(“Calculated Result –”,res)
elif op == ‘/’:
res = n1 / n2
print(“Calculated Result –”,res)
else:
print(“wrong choice”)
if..elif… else statement
Example : Program :
WAP to accept basic salary and grade from grade=input(“Enter grade”)
user. Calculate and display net salary basic=float (input(“Enter basic salary”))
if grade = ‘A’ - Hra=12% of basic, if grade==‘A’:
da= 8% of basic hra=0.12 * basic
allow=500 da=0.08 *basic
pf = 10% of basic allow=500
if grade = ‘B’ - Hra=11% of basic pf=0.1*basic
da= 6% of basic if grade==‘B’:
allow=300 hra=0.11 * basic
pf = 10% of basic da=0.06 *basic
if grade = ‘C’ - Hra=9% of basic allow=300
da= 5% of basic pf=0.1*basic
allow=200 if grade==‘C’:
pf = 10% of basic hra=0.09 * basic
da=0.05 *basic
net salary = basic salary + hra +da +allow- allow=200
pf. pf=0.1*basic
Print all details netsal = basic + hra + da +allow– pf
print(“Name -”,name,“Grade-”,grade)
print(“hra-”,hra,”da-”,da,“net salary-”,netsal)
if..elif… else statement
Example : Write a program to calculate and print roots of a quadratic equation
ax2+bx+c=0.
Calculate d = b2 -4ac,
if d > 0, Calculate root1= -b+sqrt(d)/2*a and root2= -b-sqrt(d)/2*a,
if d = 0 then the root1= -b / 2*a
otherwise if d < 0 then it should display imaginary root.)
Program : a = int(input(“Enter value for a”))
b = int(input(“Enter value for b”))
c = int(input(“Enter value for c”))
d = b**2 – 4*a*c
if d>0:
print(“Two roots”)
r1 = (-b+d**0.5)/ 2*a
r2 = (-b-d**0.5)/ 2*a
print(“Roots are - ”,r1,r2)
elif d==0:
print(“One root”)
r1= -b/ 2 *a
print(“Root-”,r1)
else:
print(“Imaginary roots”)
if..elif… else statement
Example : Program:
Write a program to accept marks of 5 m1 = float(input("enter mark - "))
subjects. Calculate total, percentage m2 = float(input("enter mark - "))
and acquired division based on m3 = float(input("enter mark - "))
following criteria. m4 = float(input("enter mark - "))
If perc>=60 division = first m5 = float(input("enter mark - "))
If perc>=50 and less than 60 tot = m1+m2+m3+m4+m5
division = second perc = (tot / 500) * 100
If perc>=40 and less than 50 if perc >= 60:
division = third divn="first"
If perc is less than 40 elif perc >= 50 and perc<60:
division= failed divn=“Second"
If the student’s percentage is more elif perc >= 40 and perc<50:
divn="third"
than 80%,
else:
display “Eligible for scholar badge”
divn="failed"
otherwise display “Not eligible “
print(“Total marks - ",tot,“\nPercentage - ",perc)
print("result -",divn)
if perc>80:
print(“Eligible for scholar badge”)
else:
print(“Not eligible”)
if..elif… else statement
Example : Write program to input type of food(foodtype) from the user. Assign the sticker on
the food packet as shown below:
Food type Sticker
Non-veg Red
veg Green
Contains egg Yellow
any other No specific color
Display the foodtype along with sticker.
Program : foodtype=input("Enter the type of food")
if foodtype=="non_veg":
sticker="Red"
elif foodtype=="veg":
sticker="Green"
elif foodtype==“contains egg":
sticker="Yellow"
else:
sticker="No specific color"
print("Sticker on item : ",sticker)
if..elif… else statement
Write a Python program to input car no, Car cno = int(input(“car no”))
Type, total distance travelled from the user. ctype = input(“Enter car type”)
dist = float(input(“Enter distance”))
Calculate and display charges per kilometer charges = 0
as per the following criteria. Display error if ctype == ‘A’ :
message when type is not A/B/C. charges= 25
Type charges Per kilometer elif ctype == ‘B’ :
A 25 charges= 15
B 15 elif ctype == ‘C’ :
C 20 charges= 20
else:
Calculate
print(“You entered wrong choice”)
Total charges = charges per kilo meter *
distance totch = charges * dist
When the Total charges is more than 5000, print(“total charges - ”,totch)
the company gives an additional 5% discount if totch > 5000:
on total charges. discount=0.05 * totch
Calculate Netcharges =Total charges-discount print(“Discount –”,discount)
Display totalcharges , discount and print(“Net charges - ”,totch - discount )
Netcharges
if Statement with logical operators
When there is a need to combine more than one conditions/relational expressions,
Logical operators are used.
There are three logical operators - and, or, not
and
• The logical operator and is a Binary operator which combines two condition
• The evaluated result with and is True if both conditions are True.
• Hence, the statements under if statement will be executed only if all the specified
conditions are True.
or
• The logical operator or is also a Binary operator
• The evaluated result with or is True if either condition is True.
• The statements, under if statement will be executed if either of the specified conditions
are True.
not
• The logical operator not is a Unary operator which is used befor a condition.
• The result is True if negation of condition is True.
• The statements under if statement, will be executed, only if the opposite of the
condition is True.
if Statement with and operators
Example : Write a program to input salary of an employee. If the
salary is between 20000 and 50000 (both value
inclusive), then print “Your designation is Manager”
otherwise print” Manager designation not assigned “
Program : salary=float(input("Enter salary"))
if salary >=20000 and salary<=50000:
print("Your designation is Manager")
else:
print("Manager designation not assigned ")
if Statement with or operators
Example : Write a program to input percentage of a student along with his
total no. of prizes . If percentage is more than 75 and or
total prizes is more than equal to 20, print ”you are eligible”
otherwise print “you cannot apply”
Program : perc = input("Enter Percentage - ")
prizes = int(input(“Enter no prizes - "))
if perc >75 or prizes >=20:
print("you are eligible for this post")
else:
print(“you cannot apply")
if Statement with not operators
Example : Write a program to input marks of a student. If marks are not less
than 50 then print “You have cleared this test“ otherwise print” You
have not cleared this test"
Program : marks=float(input("Enter marks"))
if not marks<50:
print("You have cleared this test")
else:
print("You have not cleared this test")
if Statement with logical operators
Example : Write a program to accept 3 sides and check whether triangle can be
made with these or not.
Program : s1=int(input("Enter first side -"))
s2=int(input("Enter second side -"))
s3=int(input("Enter third side -"))
if (s1+s2>s3) and (s2+s3>s1) and (s3+s1>s2):
print("triangle can be made")
else:
print("triangle cannot be made")
Example : Write a program to input three numbers and display the largest.
Program: a = int(input("Enter first number"))
b = int(input("Enter second number"))
c = int(input("Enter third number"))
if a >= b and a >= c:
print(a,"is Largest")
elif b >= a and b >= c:
print(b,"is Largest ")
elif c >= a and c >= b:
print(c,"is Largest ")
if Statement with logical operators
Example : Write program to input a character from the user. Check if it is an
alphabet, digit or special character.
Program : ch=input("Enter a character")
if ch>='A' and ch <='Z' or ch>='a' and ch<='z':
print("It is an alphabet")
elif ch>='0' and ch<='9': # Condition can also be given as '0'<=ch<='9'
print("It is a digit")
else:
print("It is a special character")
Note : To check if ch is an alphabet or not condition can also be given as
if ‘A’<=ch<=‘Z’ or ‘a’<=ch<=‘z’:
Types Of Python Statement
Types of statements
Simple Empty Compound
Any single executable This refers to a group of
It is a statement which does
statement in Python is a statement executed as one
nothing
simple statement unit
Types Of Statement
Simple Statement
Any single executable statement in Python is a simple statement.
Example : n = input(“enter name “)
Empty Statement
• It is a statement which does nothing
• In Python, the pass statement is used as an empty statement which is
used when the program does nothing when a condition is evaluated to
true or False.
• The pass statement results null operation
Example: perc = float(input("enter Percentage - "))
if perc>=90:
print(“You are a scholar")
else:
pass
Types Of Statement
Compound Statement
• This refers to a group of statement executed as one unit
• That group of statement (referred as body) is indented inside a header.
• Python Compound statement follows the syntax:
Syntax - Compound statement header:
<Python satements>
• The compound statement header begins with a keyword and ends with a colon(:).
The : separates the header from the body.
• The body consists of one or more Python statement starting at the same column
position i.e all statements given as the body of a compound statement are typed
with same level of indentation.
• Example : if condition/expression: Header
Statement 1
Statement 2 Compound
: Statement of if condition
Statement n
Assessment
Rewrite the corrected code for the following. Also Underline the corrections.
age= int (input(“Enter age of the person”))
if age >= 18
print (“You are eligible for A category”)
elsif 15 <age<18:
print(“You are eligible for B category”)
else print (“You are eligible for C category”)
Answer :
age= int (input(“Enter age of the person”))
if age >= 18:
print (“You are eligible for A category”)
elif 15 <age<18:
print(“You are eligible for B category”)
else:
print (“You are eligible for C category”)
Assessment
Rewrite the corrected code for the following. Also Underline the corrections.
salary= Float (input(“Enter salary -”))
if salary >= 50000
print (“Eligible for Increment”)
elseif 25000 <salary<50000:
print(“You are eligible for Bonus”)
else
print (“Not eligible”)
Answer :
salary = float (input(“Enter salary -”))
if salary >= 50000:
print (“Eligible for Increment”)
elif 25000 <salary <50000:
print(“You are eligible for Bonus”)
else:
print (“Not eligible”)
Assessment
Give the output for the following code.
X = int(input(“Enter x –”))
if X>2 or X <7 and X >=5:
print(“Successful”)
else:
print(“unsuccessful”)
What will be the out when x =5, x=3 and x=4
Output: When x = 5 - Successful
When x= 3 - Successful
When x= 1 - unsuccessful
Assessment
Give the output for the following code.
a. num = int(input(“enter number –“))
if num%3 ==0 and num %7 ==0 or num>50:
print(num//2)
else:
print(num**2)
What will be the output if num=9 and num = 21
Output : If num=9 Output: 81
If num= 21 Output: 10
b. x,y= -5,0
if x< -9 or y % 2 == 0:
print(“First option",end=" ")
elif -7<x<3:
print(“Second option“,end=“ “)
print(“Additional option”)
Output: First option Additional option
Assessment
Give the output for the following code.
a=int(input(“Enter number”))
b=int(input(“Enter number”))
c=int(input(“Enter number”))
if((a>b and a<c) and (a != b and a != c)):
print(a, "is the number")
elif((b>a and b<c) and (b != a and b != c)):
print(b, " is the number")
elif((c>a and c<b) and (c != a and c != b)):
print(c, " is the number")
else:
print("All three numbers")
What will be the output when
i. a=4,b=3,c=5 ii. a=4,b=10,c=16 iii a=6,b=6,c=5
If input is:
i. a=4,b=3,c=5 Output: 4 is the number
ii. a=4,b=10,c=16 Output: 10 is the number
iii a=6,b=6,c=5 Output: All three numbers
Assessment
Give the output for the following code.
p,q=0,1
p=q and 0
q+=p
if p != 0:
print("Welcome to class XI", end=" ")
elif q==1:
print("Get Ready to learn", end=" ")
if q!=p:
print("Python", end=" ")
print("Well done!!!", end=" ")
Output: Get Ready to learn Python Well done!!!
Assessment
Write logical expression for the following
i. Either n1 is greater than n2 or n1 is less than n3
Answer : n1>n2 or n1<n3
ii. Language is Python or year between 2010 and 2019
Answer : language==“Python” or (year >=2010 and year<=2019)
iii. City is either “New Delhi or Bangalore but not Chennai
Answer : city==“New Delhi” or city ==“Bangalore”) and city!=“Chennai”
Assessment
When the condition given with if is false control goes to _________ or ________
part of if
Answer : elif or else
_____ statement is used in Python when null operation is required
Answer : pass
Give the output
x = int(input(“enter x”)
y = int(input(“enter y”)
if x+y:
print("Big")
else:
print("small")
i. x =5, y =10
ii. x =5 , y=-5
Answer : i. Big ii. small
Nested if…else statement
When, one if statement is enclosed within another if, it is
known as nested if
In a nested if statement, the execution is carried from top to
bottom.
The expression with each if is tested and if it is evaluated to
true, only then the expression given in enclosed if statement
gets evaluated and the remaining portion is bypassed
Any number of if statement can be nested inside one another
Nested if..else statement
Syntax : if condition1:
if condition2:
indented statement block1
else:
indented statement block 2
elif condition3:
indented statement block 3
else:
indented statement block 4
• When condition 1 is True, then only Condition 2 will be evaluated.
• When condition 1 and condition 2 both are evaluated to True, then
indented statement block1 will be executed.
• When condition1 = True and condition 2 = False, then indented-
statement block 2 will be executed
• When condition1 = False and condition 3 = True, then indented-
statement block 3 will be executed
• When condition1 = False and condition 3 = False, then indented-
statement block 4 will be executed
Nested if..else statement
Example : Write a program to accept year and check whether
it is a leap year or not
Program: yr = int(input(" enter year -"))
if yr %100 == 0:
if yr % 400 == 0:
print(yr," is a century year and leap year")
else:
print(yr, "is a century year but not a leap year")
elif yr % 4 == 0:
print("Non Century leap year")
else:
print("Non Century and not a leap year")
Nested if..else statement
Example: Write a program to accept a character and Check whether it is an
alphabet or digit or special character. If it is an alphabet check
whether it is a lower case or uppercase vowel or consonant . If it
is digit, check whether it is even or odd. Otherwise display it is a
special character.
Program: ch = input("Enter a character -")
if ch >= 'A' and ch<= ‘Z' or ch >= 'a' and ch<='z’:
if ch == 'a' or ch=='e' or ch == 'i' or ch == 'o' or ch == 'u’ :
print (" lower case vowel")
else:
print (" lower case Conosent")
if ch== 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U’:
print ("upper case vowel")
else:
print (“Uppercase consonant")
elif ch >= '0' and ch <= '9’:
if int(ch) % 2 ==0:
print (" even digit")
else:
print ("odd digit")
else:
print ("spl character")
Nested if..else statement
Example : Write a program to accept 3 numbers from user and
print the largest one
Program : n1 = int( input("enter number1 = "))
n2 = int( input("Enter number2 = "))
n3 = int(input("enter number3 ="))
if n1>n2:
if n1>n3:
print ("largest = ",n1)
else:
print ("largest = ",n3)
elif n2 > n3:
print ("largest = ",n2)
else:
print ("largest = ",n3)
Nested if..else statement
Example : Write a program to accept 3 numbers from users. Display the
numbers in descending order
Program: num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
num3 = int(input("Enter third number"))
if num1 > num2 and num1 > num3:
if num2 > num3:
max1, max2, max3 = num1, num2, num3
else:
max1, max2, max3 = num1,num3,num2
elif num2 > num1 and num2 > num3:
if num1>num3:
max1, max2, max3 = num2, num1, num3
else:
max1, max2, max3 = num2,num3,num1
elif num3 > num1 and num3 > num2:
if num1>num2:
max1,max2,max3=num3,num1,num2
else:
max1, max2, max3=num3, num2, num1
print(“arranged numbers-“,max1,max2,max3)
Nested if..else statement
Example: Program:
WAP to Input amount deposited and intrate = 0
type of deposit (Fixed and current ) amt = float(input("Enter Amount Deposited - "))
from the user. calculate interest on type = input("Enter type ")
deposited amount as per the if amt <=10000:
following criteria: if type==“Fixed":
Amount Type %interest intrate=2
<=10000 Fixed 2 elif type==“Current":
Current 5 intrate=5
>10000 & Fixed 4 elif amt > 10000 and amt <= 25000:
<=25000 Current 7 if type==“Fixed":
intrate = 4
>25000 Fixed 6 elif type == “Current":
Current 8.5
intrate = 7
elif amt > 25000 :
if type==“Fixed":
intrate = 6
elif type==“Current":
intrate=8.5
intamt = amt * intrate/100
print(“Calculated Interest – “,intamt)
Nested if..else statement
Program:
Example:
intrate = 0
WAP to Input amount deposited and
amt = float(input("Enter Amount Deposited - "))
type of deposit (Fixed and current )
from the user. calculate interest on type = input("Enter type ")
deposited amount as per the if amt <=10000:
following criteria: if type==“Fixed":
intrate=2
Amount Type %interest
elif type==“Current":
<=10000 Fixed 2
Current 5 intrate=5
elif amt > 10000 and amt <= 25000:
>10000 & Fixed 4
if type==“Fixed":
<=25000 Current 7
intrate = 4
>25000 Fixed 6 elif type == “Current":
Current 8.5 intrate = 7
elif amt > 25000 :
if type==“Fixed":
intrate = 6
elif type==“Current":
intrate=8.5
intamt = amt * intrate/100
print(“Calculated Interest – “,intamt)
Nested if..else statement
Example : Write a Python program to input two integers a and b. Write a menu driven
program to do the following operations and display the result on the basis of user
given choice.
When choice = 1 Simple calculation :
(i) a+b
(ii) a-b
(iii) a*b
(iv) a/b
When choice 2 Complex manipulation
(i) a^b
(ii) square root of a and b
(iii) Greatest among two numbers
Nested if..else statement
ch = int(input(“1. Simple Calculation \n2. Complex Calculation\n Enter
Choice”))
a = int(input(“Enter first number”))
b = int(input(“Enter second number”))
if ch == 1:
subch = int(input(“1: + \n2: - \n3: * \n4: / \nEnter Choice : ”))
if subch == 1:
print(“\n the sum of “, a, “and” , b , ”is”, a + b)
elif subch == 2:
print(“\n the diff of “,a, “and”, b, ”is” , a - b)
elif subch == 3:
print(“\n the Product of “, a, “and”, b ,” is”, a * b)
elif subch == 4:
print(“\n the diff of “,a, “and”, b, ”is” , a / b)
else :
print(“\n WRONG CHOICE!!!”)
Nested if..else statement
elif ch == 2:
subch =int(input(“1. power value \n2. Square root \n3. Greatest of 2 numbers” )
if subch == 1:
print(a, “to the power of ”, b , ”is”, a ** b)
elif subch == 2:
print(“the square root of “,a, “is”, a**0.5,” and “, b, “is” , b**0.5)
elif subch == 3:
if a > b:
print(a , “is greater than” ,b)
else:
print(b , “is greater than”, a)
else:
print(“WRONG CHOICE!!!”)
else:
print(“WRONG CHOICE!!!”)
Assessment
Give the output of the following
X = int(input("Enter value - "))
if X >=10:
if X < 15:
print ("First case", end= " ")
else:
print("Second Case",end=" ")
elif X <= 7:
if X != 0:
print("Third case",end= " ")
print ("Fourth case")
When X = 13 X = 100 X=5
Output : X = 13 First case Fourth case
X = 100 Second case Fourth case
X=5 Third case Fourth case
Assessment
Give the output of the following
i = int(input("Enter value of i - "))
j = int(input("Enter value of j - "))
k = int(input("Enter value of k - "))
if i < j:
if j < k:
i += j
else:
j -= k
else:
if j > k:
j *=i
else:
i += k
print(i+j+k)
Give output when a. i = 3, j = 5, k=7
b. i = -2, j = -5, k = 9
c. i = 8, j = 15, k = 12
Output : a. 20
b. 11
c. 23