0% found this document useful (0 votes)
14 views17 pages

Lab Manual

The document outlines a series of Python programming experiments, each with specific aims and source code examples. It covers various topics including basic output, data types, arithmetic operations, conversions, area and volume calculations, conditional statements, recursion, and list and dictionary manipulations. Each experiment includes the source code, expected output, and explanations of the tasks performed.

Uploaded by

darlingnani885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Lab Manual

The document outlines a series of Python programming experiments, each with specific aims and source code examples. It covers various topics including basic output, data types, arithmetic operations, conversions, area and volume calculations, conditional statements, recursion, and list and dictionary manipulations. Each experiment includes the source code, expected output, and explanations of the tasks performed.

Uploaded by

darlingnani885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

EXPERIMENT:1

AIM: Write and execute simple python Program


Source code:

#A simple Python program , displays HELLO WORLD


print("Hello World!")
output:
Hello World!

EXPERIMENT:2

AIM: Write /execute simple ‘Python’ program: Develop minimum 2 programs using different
data types (numbers, string, tuple, list, and dictionary).

Source code:

2 a) # Develop minimum 2 programs using different data types (numbers,


string, tuple, list, and dictionary)

#initialize int,float, string and boolean


a=10
b=3.4567
c="hello"
d=True
print("Integer value a=",a)
print("Float value=",b)
print("String value",c)
print("Boolean value",d)
output:
Integer value a= 10
Float value= 3.4567
String value hello
Boolean value True

b) # Using list, tuple dictionary

#declaring list
li=[10,20,30,40,50,"SBTET",3.5]
#declaring Tuple
t=("monday","tue","wed","thu","fri","sat","sun",7)
#declaring Dictionary
d={
"pin":"20202-cm-001",
"name":"Devi srinivas",
"marks":89
}
# print list
print("list ddata",li)
#print Tuple
print("Tuple data",t)
#print dictionary
print("Dictionary data",d)
output:
list ddata [10, 20, 30, 40, 50, 'SBTET', 3.5]
Tuple data ('monday', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun', 7)
Dictionary data{'pin': '20202-cm-001', 'name': 'Devi srinivas', 'marks':
89}

EXPERIMENT:3

AIM: Write /execute simple ‘Python’ program: Develop a program using Arithmetic
Operators, exhibiting data type conversion.

SOURCE CODE:
# Develop minimum 2 programs using Arithmetic Operators, exhibiting data
type conversion.
# Read 2 numbers from keyboard
a=int(input("Enter First Number"))
b=float(input("Enter Second Number"))
#print numbers
print("a=",a)
print("b=",b)
#addition
c=a+b
print("Sum is",c)
#Difference
c=a-b
print("Difference is",c)
#Multiplication
c=a*b
print("Multiplication:",c)
#Division
c=a/b
print("Division:",c)
OUTPUT:
Enter First Number12
Enter Second Number3
a= 12
b= 3.0
Sum is 15.0
Difference is 9.0
Multiplication: 36.0
Division: 4.0

EXPERIMENT:4

AIM:
(i)Write simple programs to convert U.S. dollars to Indian rupees.

(ii) Write simple programs to convert bits to Megabytes, Gigabytes and Terabytes.
Source code(i):
dollars=int(input("Enter U.S Dollars"))
rupees=dollars*86.53
print("In Rupees:",rupees)
output:
Enter U.S Dollars5
In Rupees: 432.65

Source code(ii):
#simple program to convert bits to Megabytes, Gigabytes and Terabytes.
bits=int(input("Enter Number of Bits"))
mb=bits/(8*1024*1024)
print(mb,"MB")
gb=mb/1024
print(gb,"GB")
tb=gb/1024
print(tb,"TB")
output:
Enter Number of Bits1234567890
147.17196106910706 MB
0.14372261823154986 GB
0.0001403541193667479 TB

EXPERIMENT:5

AIM:
( i)Write simple programs to calculate the area and perimeter of the square
(ii)the volume & perimeter of the cone.

Source code(i):
#simple programs to calculate the area and perimeter of the square
side=int(input("Enter side of a square"))
area=side ** 2
pm=4*side
print("Area of Square is",area)
print("Perimeter of square is",pm)

output:
Enter side of a square23
Area of Square is 529
Perimeter of square is 92

Source code(ii):
#simple programs to calculate the the volume & perimeter of the cone
r=int(input("Enter radius"))
h=int(input("Enter access height"))
vol=3.14*r*r*h/3
print("Volume of cone is",vol)
p=2*3.14*r
print("perimeter of cone is",p)

output:
Enter radius7
Enter access height9
Volume of cone is 461.5800000000001
perimeter of cone is 43.96

EXPERIMENT:6

AIM: Write program to:


(i)Determine whether a given number is odd or even.

(ii)Find the greatest of the three numbers using conditional operators.


Source code(i):
#program to: (i) determine whether a given number is odd or even.
n=int(input("Enter a Number"))
if n%2==0:
print(n,"is EVEN NUMBER")
else:
print(n,"is ODD NUMBER")

output:
Enter a Number67
67 is ODD NUMBER

Source code(ii):
#program to: Find the greatest of the three numbers using conditional
operators.
a=int(input("Enter first number"))
b=int(input("Enter second number"))
c=int(input("Enter third number"))
max= a if a>c else c if a>b else b if b>c else c
print("Max:",max)

output:
Enter first number3
Enter second number67
Enter third number8
Max: 67

EXPERIMENT:7

AIM: Write a program to:


i)Find factorial of a given number.

ii) Generate multiplication table up to 10 for numbers 1 to 5.

Source code(i):
# a program to Find factorial of a given number
n=int(input("Enter a Number"))
fact=1
for i in range(1,n+1):
fact *= i
print("Factorial of Given Number is",fact)
output:
Enter a Number8
Factorial of Given Number is 40320

Source code(ii):
#Generate multiplication table up to 10 for numbers 1 to 5
for i in range(1,6):
for j in range(1,11):
print(i,"*",j,"=",i*j)
output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

EXPERIMENT:8

AIM: Write a program to:


i) Find factorial of a given number.
ii) Generate multiplication table up to 10 for numbers 1 to 5 using functions.
Source code(i):
# a program to Find factorial of a given number using functions
def factorial(n):
f=1
for i in range(1,n+1):
f *= i
return f
n=int(input("Enter a Number"))
print("Factorial of ",n,"is:",factorial(n))
output:
Enter a Number6
Factorial of 6 is: 720

Source code(ii):
#Generate multiplication table up to 10 for numbers 1 to 5 using functions
def multab():
for i in range(1,6):
for j in range(1,11):
print(i,"*",j,"=",i*j)
multab()
output:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

EXPERIMENT:9
AIM: Write a program to:
i) Find factorial of a given number using recursion.
ii) Generate Fibonacci sequence up to 100 using recursion.
Source code(i):
# a program to Find factorial of a given number using recursion
def factorial(n):
if n == 1:
return 1
else:
return n*factorial(n-1)

n=int(input("Enter a Number"))
print("Factorial of ",n,"is:",factorial(n))
output:
Enter a Number7
Factorial of 7 is: 5040

Source code(ii):
# Generate Fibonacci sequence up to 100 using recursion.
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1)+fib(n-2)
print("FIBONACCI SERIES UPTO 100")
i=0
while fib(i) <= 100:
print(fib(i),end=” “)
i += 1
output:
FIBONACCI SERIES UPTO 100
0 1 1 2 3 5 8 13 21 34 55 89
EXPERIMENT:10

AIM: Write a program to: Create a list, add element to list, delete element from the lists.

Source code:
# program to: Create a list, add element to list, delete element from the
lists
list1=["red","green","biue","yellow","pink"]
print("initially List elements:",list1)
while True:
print("1.Add element")
print("2.Remove element")
print("3.Display")
print("4.Exit")
ch=int(input("enter ur Choice"))
if ch == 1:
item=input("enter item to ADD ")
pos=int(input("Enter index to insert"))
list1.insert(pos,item)
elif ch == 2:
pos=int(input("Enter index to delete"))
list1.pop(pos)
elif ch == 3:
print("List :",list1)
else:
break
output:
initially List elements: ['red', 'green', 'biue', 'yellow', 'pink']
1.Add element
2.Remove element
3.Display
4.Exit
enter ur Choice1
enter item to ADD black
Enter index to insert2
1.Add element
2.Remove element
3.Display
4.Exit
enter ur Choice3
List : ['red', 'green', 'black', 'biue', 'yellow', 'pink']
1.Add element
2.Remove element
3.Display
4.Exit
enter ur Choice2
Enter index to delete0
1.Add element
2.Remove element
3.Display
4.Exit
enter ur Choice3
List : ['green', 'black', 'biue', 'yellow', 'pink']
1.Add element
2.Remove element
3.Display
4.Exit
enter ur Choice4

EXPERIMENT:11

AIM: Write a program to: Sort the list, reverse the list and counting elements in a list.

Source code:
# program to: Sort the list, reverse the list and counting elements in a
list
list1=["orange","banana","grapes","fruits"]
print("initially list :",list1)
print("Number of elements in list is:",len(list1))
print("Reverse of the list:")
list1.reverse()
print(list1)
print("Ascending order:")
list1.sort()
print(list1)
print("Descending order:")
list1.sort(reverse=True)
print(list1)
output:
initially list : ['orange', 'banana', 'grapes', 'fruits']
Number of elements in list is: 4
Reverse of the list:
['fruits', 'grapes', 'banana', 'orange']
Ascending order:
['banana', 'fruits', 'grapes', 'orange']
Descending order:
['orange', 'grapes', 'fruits', 'banana']

EXPERIMENT:12

AIM: Write a program to: Create dictionary, add element to dictionary, delete element from
the dictionary.

Source code:
# program to: Create dictionary, add element to dictionary, delete element
from the dictionary
mydict={
"pin":"20202-cm-001",
"name":"xyz",
"marks": 78
}
print("initially My Dictionary:",mydict)
#Adding elements to dictionary

mydict["branch"]="dcme"
mydict["college"]="GPT ADDANKI"
print("After adding My Dictionary:",mydict)
#Delete elements

mydict.pop("branch")
print("After deleting branch My Dictionary:",mydict)
output:
initially My Dictionary: {'pin': '20202-cm-001', 'name': 'xyz', 'marks':
78}
After adding My Dictionary: {'pin': '20202-cm-001', 'name': 'xyz',
'marks': 78, 'branch': 'dcme', 'college': 'GPT ADDANKI'}
After deleting branch My Dictionary: {'pin': '20202-cm-001', 'name':
'xyz', 'marks': 78, 'college': 'GPT ADDANKI'}

EXPERIMENT:13

AIM: Write a program to: To calculate average, mean, median, and standard deviation of
numbers in a list.

Source code:
program to: To calculate average, mean, median, and standard deviation of
numbers in a list
import numpy
list1=[]
for i in range(1,11):
n=int(input("enter element"))
list1.append(n)
print("LIST elements :",list1)
print("Mean :",numpy.mean(list1))
print("Median:",numpy.median(list1))
print("Standard Deviation:",numpy.std(list1))
output:
enter element10
enter element9
enter element9
enter element7
enter element4
enter element7
enter element9
enter element4
enter element8
enter element9
LIST elements : [10, 9, 9, 7, 4, 7, 9, 4, 8, 9]
Mean : 7.6
Median: 8.5
Standard Deviation: 2.0099751242241783

EXPERIMENT:14

AIM: Write a program to: To print Factors of a given Number.

Source code:
# program to: To print Factors of a given Number
n=int(input("Enter a Number"))
print("Factors of ",n,":")
for i in range(1,n+1):
if n % i == 0:
print(i)
output:
Enter a Number24
Factors of 24 :
1
2
3
4
6
8
12
24

EXPERIMENT:15

AIM: File Input/output: Write a program to:


i) To create simple file and write “Hello World” in it.
ii) To open a file in write mode and append Hello world at the end of a file.

Source code(i):
#File Input/output: Write a program to: i) To create simple file and write
“Hello World” in it
f = open("mydata.txt", "w")
f.write("Hello World")
f.close()
f=open("mydata.txt","r")
print(f.read())
f.close()
output:
Hello World

Source code(ii):
#To open a file in write mode and append Hello world at the end of a file.
f = open("mydata.txt", "a")
f.write("Hello World")
f.close()
f=open("mydata.txt","r")
print(f.read())
f.close()
output:
Hello WorldHello World

EXPERIMENT:16

AIM: Write a program to :


i)To open a file in read mode and write its contents to another file but replace every occurrence
of character ‘h’

ii)To open a file in read mode and print the number of occurrences of a character ‘a’.

Source code(i):
# program to :i) To open a file in read mode and write its contents to
another file but replace every occurrence of character ‘h’
f=open("mydata1.txt","w")
data=input("enter data to write into a file")
f.write(data)
f.close()
f=open("mydata1.txt","r")
d=f.read()
print("Data in First file:",d)
d=d.replace("h","@")
f.close()
f=open("mydata2.txt","w")
f.write(d)
print("Data in second file after copying:",d)
f.close()
output:
enter data to write into a filehealth is wealth
Data in First file: health is wealth
Data in second file after copying: @ealt@ is wealt@

Source code(ii):
# To open a file in read mode and print the number of occurrences of a
character ‘a’.
f=open("mydata1.txt","r")
d=f.read();
print("The number of occurances of a is:",d.count("a"))
f.close()
output:
The number of occurances of a is: 2

EXPERIMENT:17

AIM: Write a Program to: Add two complex number using classes and objects.

Source code:
# a Program to: Add two complex number using classes and objects
class complex1:
def __init__(self, real, imag):
self.real=real
self.imag=imag
def add(self,c1,c2):
self.real = c1.real + c2.real
self.imag = c1.imag + c2.imag
def display(self):
print(self.real,"+",self.imag,"i")
c1=complex1(2,3)
c2=complex1(5,6)
c3=complex1(0,0)
print("First Number:")
c1.display()
print("second Number:")
c2.display()
print("Result after Addition:")
c3.add(c1,c2)
c3.display()
output:
First Number:
2 + 3 i
second Number:
5 + 6 i
Result after Addition:
7 + 9 i

EXPERIMENT:18

AIM: Write a Program to: Subtract two complex number using classes and objects.

Source code:
# a Program to: subtract two complex number using classes and objects
class complex1:
def __init__(self, real, imag):
self.real=real
self.imag=imag
def sub(self,c1,c2):
self.real = c1.real - c2.real
self.imag = c1.imag - c2.imag
def display(self):
print(self.real,"+",self.imag,"i")
c1=complex1(2,3)
c2=complex1(5,6)
c3=complex1(0,0)
print("First Number:")
c1.display()
print("second Number:")
c2.display()
print("Result after subtraction:")
c3.sub(c1,c2)
c3.display()
output:
First Number:
2 + 3 i
second Number:
5 + 6 i
Result after subtraction:
-3 + -3 i

EXPERIMENT:19

AIM: Write a Program to create a package and access a package.

Steps:
1. Create a folder with the name mypack
2. Create an empty file __init__.py in the folder mypack
3. Create a python file pack.py in the folder with the below code.
Source code(pack.py):
# pack.py
def add(a,b):
print("ADD:",(a+b))
def sub(a,b):
print("SUB:",a-b)
def mul(a,b):
print("MUL:",a*b)
def div(a,b):
print("DIV:",a/b)
4. Create a python file to import the mypack(it is stored out side of mypack)
Source code(test.py):
From mypack import *
a=int(input("Enter a"))
b=int(input("Enter b"))
pack.add(a,b)
pack.sub(a,b)
pack.mul(a,b)
pack.div(a,b)
output:
Enter a10
Enter b5
ADD: 15
SUB: 5
MUL: 50
DIV: 2.0

You might also like