You are on page 1of 7

Indian Institute of Information Technology Ranchi

PYTHON LAB ASSIGNMENT – 2


Submitted To BHARAT SINGH sir
Submitted By Kunal Chaurasia
BRANCH:- ECE
ROLL NO:- 2020UGEC037R
Question1:- Write a Program for checking whether the given number is
an even number or not. Using a for loop.
Code:- #
Question 1 my_list = [input("Enter your number to check EVEN or
ODD..!")] for num in my_list: if int(num)%2==0: print(num ,'Number is
EVEN') else: print(num ,'Number is ODD')
Output: -
Question2:- By considering the terms in the Fibonacci sequence whose
values do not exceed four million, WAP to find the sum of the even-
valued terms.
Code:- #
Questiion 2 m,n = 0, 1 total = 0 while True: m, n =n, m + n if n >= 4000000:
break if n % 2 == 0: total += n print(total)
Output:-
Question3:- Ask the user to enter their first name and then display the
length of their name.
Code:-
#Question 3 name = input("Enter Your Name::=>") print(len(name))
Output:-
Question4:- Ask the user to enter their first name and then ask them to
enter their surname. Join them together with a space between and
display the name and the length of whole name.
Code:-
#Question 4 Name = input("Enter your first name") last_name =
input("Enter Your last name") print(Name,last_name) print("The length of
yor name is ",len(Name+last_name))
Output:-
Question 5:- Ask the user to type in the first line of a nursery rhyme and
display the length of the string. Ask for a starting number and an ending
number and then display just that section of the text (remember Python
starts counting from 0 and not 1).
Code:-
#Question 5 Line = input("Enter the first line of a nursery rhyme ::=> ")
print("Thhe length of your first line of nursery rhyme is::=>",len(Line))
print('Enter here your choice starting and ending of rhyme::|| ')
num1=int(input("Enter the number where you want to start"))
num2=int(input("Enter the number till where you want too end"))
Line[num1:num2] # Remember string counting started. from 0
Output:-
Question 6:- Ask the user to type in any word and display it in upper
case.
Code:-
#Question 6 any_word = input("Enter any word to display in upper case")
print("Your input word in upper case is ",any_word.upper())
Output:-
Question 7 :- Ask the user to enter their first name. If the length of their
first name is under five characters, ask them to enter their surname and
join them together (without a space) and display the name in upper case.
If the length of the first name is five or more characters, display their first
name in lower case.
Code:-
#Question 7 first_name = input("Enter Your First name ::=> ") if
len(first_name) < 5: last_name = input("Enter Your last name ::=>")
print(first_name+last_name) print("The length of your name is
",len(first_name+last_name)) elif len(first_name)>=5:
print(first_name.upper())
Output:-
Question 8:- Write a program to compute the two roots of a quadratic
equation, consider the coefficient is enter by user.
Code:- #Question 8 # Let quadratic equation is ax^2+bx+c a =
int(input("Enter The value of a")) b = int(input("Enter The value of b")) c =
int(input("Enter the value of c")) root1 = ((-b)+(b**2-4*a*c)**(1/2))/(2*a)
root2 = ((-b)-(b**2-4*a*c)**(1/2))/(2*a) print("The roots of quadratic
eqution whose coefficients are a,b,c is ",root1,'and',root2)
Output:-
Question 9:- Write a program that prompts the user to input a number
and display if the number is even or odd.
Code:-
#Question 9 number = int(input("Enter Your Number::=>")) if
number%2==0: print("Your number=>",number,"is EVEN") else:
print("Entered number:=>",number,"is ODD")
Output:-
Question 10:- Write a program that prompts the user to input two
integers and outputs the largest.
Code:-
#Question 10 number1 = int(input("Enter 1st number::=>")) number2 =
int(input("Enter 2nd number::=>")) if number1 > number2:
print(number1,"is largest number ") else: print(number2,"is largest
number")
Output:-
Question 11:- Write a program that prompts the user to input three
integers and outputs the largest.
Code:-
#Question 11 Number1 = int(input('Enter 1st Number')) Number2=
int(input('Enter 2nd Number')) Number3 = int(input("Enter 3rd Number"))
if Number1>Number2 and Number1>Number3 : print(Number1,"is
largest number") elif Number2>Number1 and Number2>Number3:
print(Number2,'is largest number') else: print(Number3,'is largest
number')
Output:-
Question 12:- Write a program that prompts the user to input a
number.Program should display the corresponding days to the number.
For example, if user type 1 the output should be sunday. If user type 7 the
output should be saturday.
Code:-
#Question 12 day_number = int(input("Enter the Number to day find")) if
day_number==1: print('Sunday') elif day_number==2: print('Monday') elif
day_number==3: print('Tuesday') elif day_number==4:
print('Wednesday') elif day_number==5: print('Thursday') elif
day_number==6: print('Friday') elif day_number==7: print('Saturday')
else: print('SORRY..! No Week day for entered number')
Output:-
Question 13:- Write a program that prompts the user to input a
character and determine the character is vowel or consonant.
Code:-
#Question 13 my_list = ['a','e','i','o','u'] alphabet = input("Enter Your
alphabet") for letter in my_list: if alphabet == letter: print(letter+"","This
is vowel") else: pass else: print(alphabet ,"", "is consonant")
Output:-
Question 14:- Write a program to compute distance between two points
taking input from the user.
Code:-
#Question 14 num1 = int(input("Enter x-co-ordinate of 1st point::=>"))
num2 = int(input("Enter y-co-ordinate of 1st point::=>")) num3 =
int(input("Enter x-co-ordinate of 2nd point::=>")) num4 = int(input("Enter
y-co-ordinate of 2nd point::=>")) distance = ((num3-num1)**2 + (num4-
num2)**2)**(1/2) print(distance,"this is the distancce between two
lines")
Output:-

Question 15:- Write a program that prompts the user to input number of
calls and calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for up to 100 calls. Plus Rs. 0.60 per call for next 50
calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any
call beyond 200 calls.
Code:-
#Question 15 call_number = int(input("Enter the number th of call")) if 0
<= call_number <= 100: print("Your bill is Rs.200") elif 101 <= call_number
<= 150: result = 200 + (call_number - 100)*(0.60) print(result) elif 151 <=
call_number <=200: result = 200 +30+(call_number-150)*(0.50)
print(result) else: result = call_number * (0.40) print(result)
Output:-
Question 16:- Write a program that prompts the user to input the value
of a (the coefficient of x2), b (the coefficient of x), and c (the constant
term) and outputs the roots of the quadratic equation.
Code:-
#Question 16 # Let quadratic equation is ax^2+bx+c a = int(input("Enter
The value of a")) b = int(input("Enter The value of b")) c = int(input("Enter
the value of c")) root1 = ((-b)+(b**2-4*a*c)**(1/2))/(2*a) root2 = ((-b)-
(b**2-4*a*c)**(1/2))/(2*a) print("The roots of quadratic eqution whose
coefficients are a,b,c is ",root1,'and',root2)
Output:-
Question 17:- Write a program that prompts the user to input a year and
determine whether the year is a leap year or not. Leap Years are any year
that can be evenly divided by 4. A year that is evenly divisible by 100 is a
leap year only if it is also evenly divisible by 400. Example: 1992 Leap Year
2000 Leap Year 1900 NOT a Leap Year 1995 NOT a Leap Year
Code:-
#Question 17 Year = int(input("Enter Year to check if leap or not")) if
Year%100==0 and Year%400==0: if Year%4==0: print("This is leap year")
else: pass else: print(Year,"is not leap year")
Output:-
Question 18:- The marks obtained by a student in 3 different subjects are
input by the user. Your program should calculate the average of subjects
and display the grade. The student gets a grade as per the following rules:
Average Grade 90-100 A 80-89 B 70-79 C 60-69. D 0-59 F
Code:-
#Question 18 subject1 = int(input("Enter the marks in 1st subject"))
subject2 = int(input("Enter the marks in 2nd subject")) subject3 =
int(input("Enter the marks in 3rd subject")) average =
(subject1+subject2+subject3)/3 if 90 <= average <= 100: print("Student
gets A grade") elif 80<= average <90 : print("Student gets B grade") elif
70<= average < 80 : print("Student gets C grade") elif 60<= average < 70:
print("Student gets D grade") else: print("Student gets F grade")
Output:-

You might also like