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

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:
a="NIKUNJ BANSAL" Output:
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}")

You might also like