Python Lab
Python Lab
Source Code:
print('Hello, world!')
1b. Write and execute simple python Program to add two numbers?
Source Code:
a=int(input("enter a number:"))
b=int(input("enter a number:"))
c=a+b
Source Code:
temp = x
x=y
y = temp
Source code:
s = (a + b + c) / 2
Source code:
#LIST
list1=[1,"hi","python",2]
print(type(list1))
print(list1)
print(list1[3:])
print(list1[0:2])
print(list1*3)
#Dictionary
print(d)
print(d.keys())
print(d.values())
3 a. Write /execute simple ‘Python’ program to demonstrate type conversion using int and
float data type ?
Source Code:
a = "10010"
b = int(a,2)
print (b)
d = float(a)
print (d)
Source code:
a=7
b=2
# addition
# subtraction
# multiplication
# division
print ('Division: ', a / b)
# floor division
# modulo
# a to the power b
Source code:
rs = dollar*81.98500
4b. Write simple programs to convert bits to Megabytes, Gigabytes and Terabytes.
Source code:
B=float(input("enter a bit:"))
KB=B/1024
MB=B/(1024*1024)
GB=B/(1024*1024*1024)
TB=B/(1024*1024*1024*1024)
print(B,"KILOBYTE IS:",KB)
print(B,"MEGABYTE IS:",MB)
print(B,"GIGABYTE IS:",GB)
print(B,"TERABYTE IS:",TB)
5. Write simple programs to calculate the area and perimeter of the square, and the
volume & Perimeter of the cone.
Source code:
height=38
radius=35
pie=3.14285714286
volume=pie*(radius*radius)*height/3
Source code:
if num % 2 == 0:
else:
Source code:
largest = num1
largest = num2
else :
largest = num3
Source code:
fact=1
if n < 0:
elif n == 0:
else:
fact = fact * i
Source code:
for i in range(1,11):
for j in range(1,11):
Source code:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
if num < 0:
elif num == 0:
else:
Source code:
def fibonnaci(n):
if n <= 1:
return n
else:
return(fibonnaci(n-1) + fibonnaci(n-2))
for i in range(nterms):
print(fibonnaci(i))
Source code:
# Creating a List
List = []
print(List)
print(List)
# using index
print(List[0])
print(List[2])
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print(List)
print(List)
Source code:
# Creating a List
List = []
print(List)
# Addition of Elements
# in the List
List.append(1)
List.append(2)
List.append(4)
print(List)
List.append((5, 6))
print(List)
List.append(List2)
print(List)
Source code:
#remove method
l = [1, 4, 6, 2, 6, 1]
print(l)
l.remove(6)
print(l)
#del method
l = [1, 4, 6, 2, 6, 1]
print(l)
del l[3]
print(l)
#pop method
l = [1, 4, 6, 2, 6, 1]
print(l)
print(l.pop(4))
print(l)
10. Write a python program to Sort the list, reverse the list and counting elements in a list.
Source code:
prime_numbers.reverse()
prime_numbers.sort()
print(prime_numbers)
#count
print(prime_numbers.count(7))
Source code:
#Creating a Dictionary
print(Dictionary)
print(Dictionary)
Source code:
Dictionary = {}
print(Dictionary)
Dictionary[0] = 'ptrhon'
Dictionary[2] = 'java'
Dictionary.update({ 3 : 'Dictionary'})
Dictionary['list_values'] = 3, 4, 6
print(Dictionary)
Dictionary[2] = 'WT'
print(Dictionary)
print(Dictionary)
Source code:
del my_dict[31]
print(my_dict)
#using pop
print(my_dict.pop(31))
print(my_dict)
# clear method
numbers = {1: "one", 2: "two"}
numbers.clear()
print(numbers)
12. Write a program to: To calculate average, mean, median of numbers in a list.
Source code:
numb=[1,2,3,6,8]
no=len(numb)
sum1=sum(numb)
mean=sum1/no
numb=[2,6,5,4,8]
no=len(numb)
numb.sort()
median=sorted(numb)[len(numb)//2]
Source code:
13 b) Write a program to check whether the given number is prime or not in python.
SOURCE CODE:
num=int(input("Enter a number:"))
i=1
count=1
for i in range(1,num):
if(num%i==0):
count=count+1
if(count==2):
print("Prime number")
else:
print("not a prime number")
SOURCE CODE:
14.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:
demofile.txt
HELLO WORLD
f = open("demofile.txt", "r")
print(f.read())
f = open("demofile.txt", "a")
f.write("Now the file has more content!")
f.close()
f = open("demofile.txt", "r") #open and read the file after the appending:
print(f.read())
f = open("demofile.txt", "w")
f.close()
f = open("demofile.txt", "r")
print(f.read())
15. write a python Program to multiply two matrices using nested loops.
Source code:
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Creating Packages
We have included a __init__.py, file inside a directory to tell Python that the current directory
is a package. Whenever you want to create a package, then you have to include __init__.py file
in the directory. You can write code inside or leave it as blank as your wish. It doesn't bothers
Python.
Follow the below steps to create a package in Python
Create a directory and include a __init__.py file in it to tell Python that the current
directory is a package.
Include other sub-packages or files you want.
__init__.py
student.py
faculty.py
Go to any directory in your laptop or desktop and create the above folder structure. After
creating the above folder structure include the following code in respective files.
Example
# student.py
class Student:
def get_student_details(self):
return f"Name: {self.name}\nGender: {self.gender}\nYear: {self.year}"
# faculty.py
class Faculty:
def get_faculty_details(self):
return f"Name: {self.name}\nSubject: {self.subject}"
We have the above in the student.py and faculty.py files. Let's create another file to access
those classed inside it. Now, inside the package directory create a file named testing.py and
include the following code.
Example
# testing.py
# importing the Student and Faculty classes from respective files
from student import Student
from faculty import Faculty
Name: Emma
Subject: Programming