Assignment on If Condition’s:
1) What is the output of the following if statement
a, b = 12, 5
if a + b:
print('True')
else:
print('False')
Ans: Output will be true
2) Given the nested if-else structure below, what will be the value of x after code execution
completes
x = 0
a = 0
b = -5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
Assignment on If Condition’s:
Ans:Output will be 2
3) Given the nested if-else below, what will be the value x when the code executed
successfully
x = 0
a = 5
b = 5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
Ans:Output is 3
4) 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
Assignment on If Condition’s:
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
Ans:
Marks = int(input())
if int(Marks) <25:
print ("The grade is F")
elif int(Marks) in range(25, 46):
print ("The grade is E")
elif int(Marks) in range(45, 51):
print ("The grade is D")
elif int(Marks) in range(50, 61):
print ("The grade is C")
elif int(Marks) in range(60, 81):
print ("The grade is B")
else:
print ("The grade is A")
5) A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
Ans:
Enter_Quantity=int(input())
cost=100
tc=Enter_Quantity*cost
t=tc*10/100
d=tc-t
if tc>1000:
print("Total cost along with discount",d)
else:
Assignment on If Condition’s:
print("Total cost",tc)
6) Write a program to print absolute value of a number entered by user. E.g.-
INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1
Ans:
Enternumber=int(input())
av=abs(Enternumber)
print("Absolute Value",av)
7) Write a Python program that accepts a string and calculate the number of digits and letters.
Sample Data : Python 3.2
Expected Output :
Letters 6
Digits 2
Ans: string = input("Please Enter your Own String : ")
alphabets = digits = 0
for i in range(len(string)):
if(string[i].isalpha()):
alphabets = alphabets + 1
else:
(string[i].isdigit())
digits = digits + 1
print("\nTotal Number of Alphabets in this String : ", alphabets)
print("Total Number of Digits in this String : ", digits)
8) Write a Python program to check whether an alphabet is a vowel or consonant.
Expected Output:
Input a letter of the alphabet: k
k is a consonant.
Ans:
Enteralphabet=str(input())
Assignment on If Condition’s:
vowels=('a','e','i','o','u')
if Enteralphabet in vowels:
print(Enteralphabet,"is vowel")
else:
print(Enteralphabet,"is consonant")
9) Write a Python program to check a triangle is equilateral, isosceles or scalene. Go to the
editor
Note :
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.
Expected Output:
Input lengths of the triangle sides:
x: 6
y: 8
z: 12
Scalene triangle
Ans:
print("Input lengths of the triangle sides: ")
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
if x == y == z:
print("Equilateral triangle")
elif x==y or y==z or z==x:
print("isosceles triangle")
else:
print("Scalene triangle")
Assignment on If Condition’s:
10) Write an If elif else condition for finding the largest number in 3 numbers.
Ans: num1 = 10
num2 = 14
num3 = 12
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)