Assignment - 5
Assignment - 5
Lab Assignment-1
Submitted by
Submitted to
Dr. Hemant Petwal
Solution:
Process of downloading python:
Step.1: Visit google.com and search download idle python for windows.
Step.6: On the righthand side under “Highlight Theme” Click on “IDLE dark”.
Solution:
Code: Output:
3. Declare a string variable called x and assign it the value “Hello”. Print out
the value of x.
Solution:
Code: Output:
x= ”Hello”
print(x)
4. Take different data types and print values using print function.
Solution:
Code: Output:
a=123
b=12.10 c="hello"
d=[1, 2, 3] print ("a is ", type
(a) ) print ("b is ", type (b) )
print ("c is “, type (c)) print ("d is ", type (d))
5. Take two variables, a and b.
Assign your first name and last name. Print your Name after adding your First name
and Last name together.
Solution:
Code: Output:
a=”Arpit”
b=”Gupta”
print(“Full name is “,a+b)
6. Declare three variables, consisting of your first name, your last name and
Nickname. Write a program that prints out your first name, then your
nickname in parenthesis and then your last name.
Example output: George (woody) Washington.
Solution:
7. Declare and assign values to suitable variables and print in the following
way:
NAME: NIKUNJ BANSAL
SAP ID: 500069944
DATE OF BIRTH: 13 Oct 1999
ADDRESS: UPES Bidholi Campus
Pincode: 248007
Programme: AI & ML
Semester: 2 Solution:
Code:
Output:
a="NIKUNJ BANSAL" b=500069944 c="13 ОСТ
1999" d="UPES Bidholi Campus" e=248007
f="AI & MI" g=2 print ("NAME: ", a)
print ("SAP ID: ", b)
print ("DATE OF BIRTH: ", c) print
("ADDRESS: ", d) print ("PINCODE: ", e)
print ("PROGRAMME: “, f)
print(“SEMESTER: “,g)
2. Write a Program where the radius is taken as input to compute the area of a circle.
Solution:
Code: a=int (input ('Enter the radius of circle: Output:
'))
area= 3.14*a*a print ('Area of circle is ', area)
6. Write a program to find area of triangle when length of sides are given.
Solution:
Code:
Output:
import math
print('Area of triangle') a1=int(input('Enter the side:
')) a2=int(input('Enter the side: '))
a3=int(input('Enter the side: ')) s=(a1+a2+a3)/2
ar=math.sqrt(s*(s-a1)*(s-a2)*(s-a3)) print('Area of triangle is
',ar)
Output:
print('CODE FOR SUM OF "n" NATURAL NUMBER') n=int(input('Enter
the number till want to add: ')) sum=0 for i in range(1,n+1):
sum=sum+i print('ADDITION OF n natural number: ',sum)
11. Write a program to covert a given series into perfect cube. Solution:
Code: num=eval(input("Enter a series of number:
Output:
")) l=[] for i in num:
l.append(i**3) print("Cubic series are ") print(l)
12. Write a program to print Fibonacci series.
Solution:
Code:
# Get the number of terms for the Fibonacci series num =
int(input("Enter the number of terms for the Fibonacci series:
"))
first= 0 second=
1
print("Fibonacci Series:")
print(first,end=", ") print(second,
end=", ") for _ in range(2, num):
next_term = first + second
print(next_term, end=", ") first,
second = second, next_term
Output:
13. Write a program to compute the length of the hypotenuse (c) of a right triangle
using Pythagoras theorem.
Solution:
Code:
#Calculate the length of the hypotenuse by Output:
Pythagorean theorem import math
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: ")) hypotenuse
= math.sqrt(a*a + b*b) print(f"The length of the
hypotenuse is:
{hypotenuse}")
14. Write a program to print sum of even and odd numbers among n natural numbers
Solution; Code:
# sums for even and odd numbers Output:
n = int(input("Enter the value of n: ")) even = 0 odd = 0
for i in range(1, n + 1): if i % 2 == 0: even += i
else:
odd += i print(f"Sum of even numbers
from 1 to {n}:
{even}")
print(f"Sum of odd numbers from 1 to {n}: {odd}")
Lab Assignment- 4
Python
1. Read an integer from keyboard using input and display it using print.
Input
10
Output
10
Solution:
CODE: Output:
n=int ( input ('Enter a number: '))
print(n)
2. Read a float type number from a keyboard using input and display it using print.
Truncate the number to two decimal places.
Input
10.5
Output
10.50
SOLUTION: OUTPUT:
CODE:
f=float (input('Enter a float number: '))
print('{:.2f}'.format(f))
3. Any variable in Python has to have a name to identify its presence in the program. The
name assigned to that variable will become a "identifier". The identifier's name can start
with the alphabets A to Z or a to z or an underscore (_). Also, numerals (0 to 9) can be
present in the variable name. And, the special symbols such as: !, #,@,%,$ cannot be used
in the identifiers. Write a program to assign different combinations of identifiers to the
variables and display the output of the values stored in identifiers and their type.
Sample Input :
integer = 10
decimal = 10.11
num_ber = -5
Sample Output :
10 <class 'int'>
10.11 <class 'float'>
-5 <class 'int'>
SOLUTION:
OUTPUT:
CODE:
integer=10
decimal=10.11
num_ber=-5
print(type(integer))
print(type(decimal))
print(type(num_ber))
4. Take input of a string “UPES University” and print 1st, fourth and last character of the strings.
Sample Input:
UPES University
Output:
U
S
Y
SOLUTION: OUTPUT:
CODE:
string=str(input('Enter a string: '))
print(string[0])
print(string[3])
print(string[-1])
5. Write a program to accept your personal details such as name and age and print it on the screen
using the formatter and the placeholder.
Sample Input:
Rahul
18
Sample Output:
Hi Rahul, you are 18 years old !!!
SOLUTION: OUTPUT:
CODE:
name=str(input('Enter your name: '))
age=int(input('Enter your age: '))
new=('Hi {},you are {} years old!!!'.format(name,age))
print(new)
6. Write a program to accept string as input and print it on the screen using formatter and
placeholder.
Sample Input 1:
Python
Useful
Sample Output:
I love Python programming, it is very Useful.
SOLUTION: OUTPUT:
CODE:
a=str(input('Enter string: '))
b=str(input('Enter second string: '))
c=('I love {} programming, it is very
Useful.'.format(a,b))
print(c)
7. Mohit is a very cunning child, when his brother was away from his laptop, he changed his
original program to the following:
x = input()
y = input()
if x>y:
output("x is greater than y")
else:
print("y is greater than x")
This program does not throw an error when it is run, rather it throws an error during runtime.
These
kinds of errors are known as runtime errors.
If we give x=1, y=2, the program runs fine, but when we give x=2 and y=1, the program will throw
an error.
SOLUTION:
OUTPUT:
CODE:
x=int(input('enter a number: '))
y=int(input('enter 2nd number: '))
if x>y:
print("x is greater than y")
else:
print("y is greater than x")
ANSWER:
1.In if statement there is syntax error instead of print ,it is output.
8. Below is Smart meter Prototype, write a program to implement this meter through python
programming.
Ex:
Input:
Login: Alok
Meter No: 1234
Output:
Dear Mr. Alok, Kindly proceed to generate your meter receipt.
Input:
Current Bill =User Input (Say 500)
Electricity bill you paid (a/b)= User Input
Alok, kindly find your receipt below.
SOLUTION:
CODE:
login=str(input('LOGIN: '))
meter=int(input('METER NO.: '))
OUTPUT:
Lab Assignment- 5
Python
1. Write a program to find left shift and right shift values of a given number.
SOLUTION:
CODE: OUTPUT:
num=int(input("Enter a number: "))
left=num<<1
right=num>>1
print("Right shift:",right)
print("Left shift: ",left)
SOLUTION:
CODE: OUTPUT:
list=[10,20,56,78,89]
n=int(input("Enter a number you want to find: "))
if n in list:
print("{} is in list".format(n))
else:
print("{} is not in list".format(n))
if n not in list:
print("{} is not in list".format(n))
else:
print("{} is in list".format(n))
SOLUTION:
CODE:
OUTPUT:
string=str(input("Enter a string: "))
print(string)
find=str(input("Enter a character that you want to find: "))
if find in string:
print("{} is in the given string".format(find))
else:
print("{} is not in the given string".format(find))
4. Check whether a given number is divisible by 3 and 5 both.
SOLUTION:
CODE:
OUTPUT:
num=int(input("Enter a number: "))
if (num%3==0 and num%5==0):
print("{} is divided by 3 and 5 both".format(num))
else:
print("{} is not divided by 3 and 5 both".format(num))
SOLUTION: OUTPUT:
CODE:
num=int(input("Enter a number: "))
if num%5==0 :
print("{} is multiple by 5".format(num))
else:
print("{} is not multiple by 5 ".format(num))
6. Find the greatest among two numbers. If numbers are equal than print “numbers are
equal”.
SOLUTION: OUTPUT:
CODE:
7. Find the greatest among three numbers assuming no two values are same.
SOLUTION:
CODE: OUTPUT:
8. Check whether the quadratic equation has real roots or imaginary roots. Display the
roots.
SOLUTION: OUTPUT:
CODE:
a=int(input("Enter a : "))
b=int(input("Enter b: "))
c=int(input("Enter c: "))
sol=(b**2-4*a*c)
d=sol**0.5
r1=(-b+d)/2*a
r2=(-b-d)/2*a
if sol>=0:
print("Quadratic equation having real roots ")
print("Root 1={}".format(r1))
print("Root 2={}".format(r2))
else:
print("Quadratic equation having imaginary roots ")
SOLUTION:
CODE:
SOLUTION:
CODE:
percent=((sub1+sub2+sub3+sub4+sub5)/500)*100
cgpa=percent/10
OUTPUT: