Q1. Write a program to input a year in 4-digit form and check whether it is a leap year or not and display proper message.
Hint: A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
y=int(input('Enter Year: '))
if y%4==0:
if y%100==0:
if y%400==0:
print('The year is leap year')
else:
print('Not a leap year')
else:
print('The year is leap year')
else:
print('The year is not leap year')
Q2. Write a program to input a character and swap it's case(i.e. If it is lowercase letter, convert it to uppercase and vice versa).
ch=input("Enter a character")
if ch>="A" and ch<="Z":
print(chr(ord(ch)+32))
else:
print(chr(ord(ch)-32))
Q3. Write a program to input a number, check whether it is positive or negative and display the appropriate message.
n=eval(input('Enter no. here '))
if n>0:
print('Positive')
else:
print('Negative')
Q4. Write a program to input cost price and selling price of an item and calculate profit and loss on a transaction.
cp=eval(input('Enter Cost price here '))
sp=eval(input('Enter Selling price here '))
if sp>cp:
print('Profit=',sp-cp,'Rs.')
else:
print('Loss=',cp-sp,'Rs.')
Q5. Write a program to input a number and check whether it is even or odd and display the appropriate message.
n=eval(input('Enter no. here '))
if n%2==0:
print('The no. is even')
else:
print('The no. is odd')
Q6. Write a program to input two numbers and check whether both are divisible by 5.
n1=eval(input('Enter the first no. here '))
n2=eval(input('Enter the second no. here ' ))
if n1%5==0 and n2%5==0:
print('Both the numbers are divisible by 5')
else :
print('Both the numbers are not divisible by 5')
Q7. Write a program to input a number and check whether it is divisible by 8 or not. (If the last three digits of a whole number are divisible by 8, then the entire number is divisible by 8.)
a=eval(input('Enter no. here '))
b=a%1000
if b%8==0:
print('The no. is divisible by 8')
else:
print('The no. is not divisible by 8')
Q8. Write a program to read the age of a candidate and determine whether it is eligible for casting his/her own vote.
age=int(input('Enter your age '))
if age>=18:
print('You are eligible for voting')
else:
print('You are not eligible for voting')
Q9. Write a program to input a character and display whether it is a vowel or a consonant.
ch=input('Enter character here')
if ch in 'AEIOUaeiou':
print('Vowel')
else:
print('Consonant')
IF-ELIF STATEMENT
Q10. Write a program to input co-efficients of a, b and c of a quadratic equation ax2 + bx + c = 0, where a = 0. Discriminant, D = b2- 4ac.If discriminant is 0 then print there is exactly one real root, if discriminant is positive then print there are two distinct roots, if discriminant is negative then print there are no real roots.
a=eval(input("Enter the coefficient of x^2 "))
b=eval(input('Enter the coefficient of x '))
c=eval(input('Enter the constant term '))
D=b**2-4*a*c
if D>=0:
print('There are two distinct roots')
elif D==0:
print('There is exactly one real root')
else:
print('The equation have no real roots')
Q11. Write a program to input three sides of a triangle and display whether it is EQUILATERAL, SCALENE, RIGHT ANGLED or ISOSCELES.
a=eval(input('Enter side 1 on triangle '))
b=eval(input('Enter side 2 on triangle '))
c=eval(input('Enter side 3 on triangle '))
if a==b==c:
print('The triangle is an equilateral triangle')
elif a**2==b**2+c**2 or c**2==a**2+b**2 or b**2==a**2+c**2:
print('The triangle is a right angled triangle')
elif a==b!=c or c==b!=a or a==c!=b:
print('The triangle is an isoceles triangle')
else:
print('The triangle is an scalene triangle')
Q12. Write a program to accept a date(dd/mm/yyyy) and check for its validity.
i=int(input("Enter today's date here in the format dd mm yyyy "))
d=i//1000000
i=i%1000000
m=i//10000
i=i%10000
if d<=31 and m in(1,3,5,7,8,10,12):
print(" Valid Date")
elif d<=30 and m in(4,6,9,11):
print(" Valid Date")
elif d<=29 and m==2:
print("Valid Date")
else:
print('The date is not valid')
Q13. Write a program to input temperature of water and display its state (Liquid, solid or gas).
temp=eval(input('Enter temperature of water here '))
if temp>100:
print('Steam')
elif temp<0:
print('Ice')
else:
print('Liquid')
Q14. Write a program to input a character and check whether a character is an alphabet, digit or special character.
wo=input('Enter a character to check what it is ')
if wo>='A' and wo<='Z' or wo>='a' and wo<='z':
print('Alphabet')
elif wo in '1234567890':
print('Number')
else:
print('Special character')
Q15. Write a program to accept a coordinate point in a XY coordinate system and determine in which quadrant(Ist, IInd, IIrd or IVth) the coordinate point lies.
x=eval(input('Enter the x-coordinate '))
y=eval(input('Enter the y-coordinate '))
if x>0 and y>0:
print('I quadrant')
elif x<0 and y>0:
print('II quadrant')
elif x<0 and y<0:
print('III quadrant')
else:
print('IV quadrant')
Q16. Write a program to read any day number in integer and display day name in the word.
day=int(input('Enter the day no. to check what day to is. [1 is for Sunday] '))
if day==1:
print('Sunday')
elif day==2:
print('Monday')
elif day==3:
print('Tuesday')
elif day==4:
print('Wednesday')
elif day==5:
print('Thursday')
elif day==6:
print('Friday')
else:
print('Saturday')
Q17. Write a program to input salary and calculate commission for the salesman. The commission is calculated as follows:
Salary Comission
30001 onwards 15%
22001 - 30000 10%
12001 – 22000 7%
5001 – 12000 3%
0 - 5000 0%
print('Commission for the salesman')
a=int(input('Enter the amount in rupees= '))
if a>=30001:
print('Commission=Rs.',(15/100)*a)
elif a>=22001:
print('Commission=Rs.',(10/100)*a)
elif a>=12001:
print('Commission=Rs.',(7/100)*a)
elif a>=5001:
print('Commission=Rs.',(3/100)*a)
else:
print("Commission=Rs.",(0/100)*a)
Q18. Write a program to input the name, subject, salary and experience (in years) of a teacher. An allowance is given according to the following condition:
If salary >10,000 or experience > 10 years, allowance is 20% of salary.
If salary is in the range 5000-10000 and experience >5years, allowance is 15% of salary.
If salary >3000, allowance is 10%.
Otherwise if salary <3000, allowance is 6%.
Find the total salary (salary+allowance) and print it along with other details.
name=input('Enter your name ')
sub=input('Enter the subject you teach ')
sal=eval(input('Enter your salary '))
exp=int(input('Enter your experience in years '))
print('Your name- ',name)
print('Your subject- ',sub)
print('Your experience- ',exp)
print('Initial salary= ',sal)
if sal>10000 or exp>10:
allow=(20/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
elif 5000<sal<10000 and exp>5:
allow=(15/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
elif sal>3000:
allow=(10/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
else:
allow=(6/100)*sal
print('Allowence= ',allow,'Rs.')
print('Total salary= ',allow+sal,'Rs.')
Q19. Write a program to calculate and print the Electricity bill of a given customer. The customer id., name and unit consumed by the user should be taken from the keyboard and display the total amount to pay to the customer. The charge are as follow:
Unit
Charge/unit
upto 199
Free
200 and above but less than 400
@1.20
400 and above but less than 600
@1.60
600 and above
@2.00
If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill should be of Rs. 150/-
id=eval(input("Enter the customer I'd here "))
name=input('Enter your name ')
unit=eval(input('Enter the no. of units consumed '))
c1=0 c2=0
if unit<=200:
c1=0
elif unit<=400:
c1=200*0+(unit-200)*1.20
elif unit<=600:
c1=200*0+(200)*1.20+(unit-400)*1.60
elif unit>600:
c1=200*0+(200)*1.20+(200)*1.60+(unit-600)*2.00
if c1<150: c1=150 c2=0 elif c1>400: c2=15/100*c1
print('Your total bill= Rs.',c2+c1)
HomeFAQsPythonIF Statement and IF-ELIF Statement Python Question and Answer
0 Comments
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com