Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Python Programming

Lab Assignment-1

Bachelor of Computer Application

Submitted by

Name: Arpit Gupta Roll No:05

Submitted to
Dr. Hemant Petwal

University of Petroleum & Energy Studies


Bidholi, Via Prem Nagar, Dehradun, Uttarakhand
Jan-May – 2024
Lab Assignment 1
Question 1: Write down the detailed process for downloading, installation, and
configuration python in window/linux.

Solution:
Process of downloading python:

Step.1: Visit google.com and search download idle python for windows.

Step.2: Visit the official Python website at python.org.

Step.3: Click on download tab.

Step.4: Now it will start downloading.


Process of installation:
Step.1: Click on the open file.

Step.2: Now click on “Install Now”.

Step.3: Now it will start installing.

Step.4: Once download click on close.


Configuration of python in window:

Step.1: Open idle.

Step.2: Go to “options” in the top tab.

Step.3: Select “Configure IDLE”.

Step.4: Select “Highlights” from top horizontal tab.


Step.5: Select “Background” radio button just above the image of IDLE on the lefthand
side.

Step.6: On the righthand side under “Highlight Theme” Click on “IDLE dark”.

Step.7: Click Apply and then Click OK done!


Experiment 2: Starting with python

1. Install Python and understand difference between scripting and interactive


modes in IDLE.
Solution:
Scripting mode in idle:
• In this file can be saved and executed using command prompt.
• We can view and edit the file at any time when we want.
• It is very suitable for writing long pieces of code.
• In this file are saved by “.py” extension.
Interactive mode in idle:

• In this file can’t be saved.


• We can view the code at once and we can’t store that.
• It is very convenient for writing very short line of codes.
• It is also very suitable for beginners because it helps them evaluate their
code line by line and understand the code well.

2. Write Python programs to print strings in the given manner:


a. Hello Everyone !!!
b. Hello World
c. Hello World
d. Rohit’s date of birth is 12\05\1999

Solution:

Code: Output:

a="Hello Everyone!!! "


b="Hello World"
c="Hello World" d="Rohit's date of birth
is 12/05/1999" print (a) print (b)
print(c)
print (d)

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:

Code: a=”Arpit” Output:


b=”Kumar”
c=”Gupta”

print(a+ ”(“ + b +”)” +c)

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)

EXPERIMENT 3: Use of input statements, operators


1. Declare these variables (x, y and z) as integers. Assign a value of 9 to x. Assign a
value of 7 to y, perform addition, multiplication, division, and subtraction on these
two variables and print out the result.
Solution:
Code:
Output:
x=9 y=7
z=x+y
print ('addition is ', z)
z=x-y

print ('Subtraction is ', z) z=x*y


print ('Multiplication is ', z)
z=x//y print
('Division is ', z)

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)

3. Write a Python program to solve (x1+y1)*(x2+y2). Solution:


Code: Output: x1=int(input('Enter value of x1: '))
y1=int(input('Enter value of y1: '))
x2=int(input('Enter value of x2: '))
y2=int(input('Enter value of y2: '))
z1=x1+y1 z2=x2+y2 sol=z1*z2
print(' final answer is ',sol)

4. Test data: x = 4, y = 3. Write a Program to perform any operation to get expected


output: 49 Solution:

Code: Output: x=4


y=3 z=15 output=x+(y*z) print('expected output: ',output)
5. Write a program to find simple interests.
Solution:
Code: Output: x=int(input('Enter the principal amount: '))
y=int(input('Enter the rate: ')) z=int(input('Enter
the time(in year): '))
si=(x*y*z)//100 print('Simple amount is ',si)

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)

7. Write a program to convert minutes into seconds and hours Solution:


Code:
Output:
min= int(input("Enter minutes: "))
sec= min*60 hour= min/60
print("The hours: ",hour) print("The seconds:
",sec)

8. Write a program to swap two numbers without taking additional


variable. Solution:
Code: Output: print('CODE FOR SWAPPPING')
x=int(input('Enter the first number: '))
y=int(input('Enter the second number: '))
print('BEFORE SWAPPING')
print('a= ',x) print('b= ',y)
x,y=y,x
print('AFTER SWAPPING')
print('a= ',x)
print('b= ',y)

9. Write a program to find sum of first n natural numbers.


Solution:
Code:

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)

10. Write a program to check whether a number is perfect square or not?


Solution:
Code:
#to check perfect square Output:
n=int(input("Enter the number:")) flag=0
for i in range(1,n): if i*i==n:
flag=1 break if flag==1:
print("It is perfect square") else:
print("It is not perfect square")

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.

Correct this code so that it is error free.


INPUT
2
1
OUTPUT
x is greater than y

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.: '))

print('Dear Mr.{}, Kindly proceed to generate your meter reciept'.format(login))

c=int(input('Current Bill:Rs '))


a=int(input('Electricity bill you paid(a):Rs '))
b=int(input('Electricity bill you paid(b):Rs '))
unit=int(input("Enter unit: "))
w=int(input('Enter weather of Dehradun: '))
tb=56000
print('{},Kindly find your receipt below.'.format(login))
dash='_'
space=' '
print(dash*110)
print("Electricity bill(a)you paid {:<20} Current Bill=Rs{:<15} Electricity bill (b)you paid
".format(space,c))
print(space*40,"Total bill paid=Rs 56000")
print(dash*110)
if(a<=c) and (b>=c):
print('{}{:>90}'.format(a,b))
print("{:>55},56000".format(c))
print("Bill to be Paid(Balance)(D):{:^50.2f}{:>15.2f}".format((c-a),(c-b)))
q=tb+(c-a)
p=tb+(c-b)
print("Total payment received till date: {:>25.2f} {:>35.2f} ".format(q,p))
print("Unit Consumed {:^80}".format(unit))
print("System accepted units:{:^65b}".format(unit))
print("City Weather:{:<30}Dehradun weather is {}\u00b0 C".format(space,w))
print(dash*110)

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)

2. Using membership operator find whether a given number is in sequence


(10,20,56,78,89)

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))

3. Using membership operator find whether a given character is in a string.

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))

5. Check whether a given number is multiple of 5 or not.

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:

a=int(input("Enter 1st num: "))


b=int(input("Enter 2nd num: "))
if a>b:
print("{} is greater".format(a))
elif a==b:
print("{} and {} are equal".format(a,b))
else :
print("{} is greater".format(b))

7. Find the greatest among three numbers assuming no two values are same.

SOLUTION:
CODE: OUTPUT:

a=int(input("Enter 1st num : "))


b=int(input("Enter 2nd num: "))
c=int(input("Enter 3rd num: "))
if a>b:
if a>c:
print(" {} is greater".format(a))
else:
print("{} is greater".format(c))
elif b>c:
print("{} is greater".format(b))
else:
print("{} is greater".format(c))

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 ")

9. Write a program to find whether a given year is a leap year or not.

SOLUTION:
CODE:

year=int(input("Enter a year to check leap year or not year: "))


if (year % 400 == 0) and (year % 100 == 0):
print("{} is leap year".format(year)) OUTPUT:
elif (year % 4 == 0) and (year % 100 != 0):
print("{} is leap year".format(year))
else:
print("{} is not leap year".format(year))
10. Print the grade sheet of a student for the given range of cgpa. Scan marks of five subjects
and calculate the percentage.
CGPA=percentage/10 Sample Grade sheet
CGPA range: Name: Rohit Kumar
0 to 3.4 -> F Roll Number: R17234512
3.5 to 5.0->C+ SAPID: 50005673
5.1 to 6->B Sem: 1
6.1 to 7-> B+ Course: B.Tech. CSE AI & ML
7.1 to 8-> A Subject name: Marks
8.1 to 9->A+ PDS: 70
9.1 to 10-> O (Outstanding) Python: 80
Chemistry: 90
English: 60
Physics: 50
Percentage: 70%
CGPA:7.0
Grade: A

SOLUTION:
CODE:

name=str(input("Enter your name: "))


rn=str(input("Enter your Roll number: "))
sap=int(input("Enter your SAP ID: "))
sem=int(input("Enter your semester: "))
course=str(input("Enter your course: "))
sub1=int(input("Enter marks of PDS: "))
sub2=int(input("Enter marks of Python: "))
sub3=int(input("Enter marks of Chemistry: "))
sub4=int(input("Enter marks of English: "))
sub5=int(input("Enter marks of Physics: "))

percent=((sub1+sub2+sub3+sub4+sub5)/500)*100
cgpa=percent/10

print(' SAMPLE GRADE SHEET ')


print('Name:{}'.format(name))
print('Roll Number:{}'.format(rn))
print('SAP ID:{}'.format(sap))
print('Semester:{}'.format(sem))
print('Course:{}'.format(course))
print('Subject name :Mark}')
print('PDS:{}'.format(sub1))
print('Python:{}'.format(sub2))
print('Chemistry:{}'.format(sub3))
print('English:{}'.format(sub4))
print('Physics:{}'.format(sub5))
print('Percentage:{}'.format(percent))
print("CGPA:{}".format(cgpa))
if cgpa>=0 and cgpa<=3.4:
print('Grade:F')
elif cgpa>=3.5 and cgpa<=5.0:
print('Grade:C+')
elif cgpa>=5.1 and cgpa<=6.0:
print('Grade:B')
elif cgpa>=6.1 and cgpa<=7.0:
print('Grade:B+')
elif cgpa>=7.1 and cgpa<=8.0:
print('Grade:A')
elif cgpa>=8.1 and cgpa<=9.0:
print('Grade:A+')

OUTPUT:

You might also like