Python lab manual
Python lab manual
The installation process will take few minutes to complete and once
the
installation is successful, the following screen is displayed.
Step 3 − Verify Python is installed on Windows
To ensure if Python is successfully installed on your system. Follow
the given steps
• Open the command prompt.
• Type ‘python’ and press enter.
The version of the python which you have installed will be displayed
if the python is successfully installed on your windows.
Step 4): On the next screen, you can create a desktop shortcut if you
want and click on “Next”.
Step 5): Choose the start menu folder. Keep selected JetBrains and
click
on “Install”.
Step 6): Wait for the installation to finish.
Week -2
Step 5: stop
2 b . Write a python program to find average of three numbers.
avg = (num1+num2+num3)/3
Step 5 : stop
x,y = 4,3
a,b,c = 1,2,3
rest1 = x*x+y*y+2*x*y
print(”({}+{})^2={}”. format(x,y,rest1))
rest2 = a*a+b*b+c*c+2*a*b+2*b*c+2*c*a
print(”({}+{}+{})^2={}”. format(a,b,c,rest2))
Algorithm : step 1: read the value x,y and a,b,c
rest1 = x*x+y*y+2*x*y
rest2 = a*a+b*b+c*c+2*a*b+2*b*c+2*c*a
WEEK -3
if n>0:
print(”positive”)
elif n<0:
print(”negative”)
else:
print(”not in range”)
Steep 7 : stop
print(”distinction”)
print(”First class”)
print(”second class”)
print(” fail”)
else:
Step 9: stop
WEEK 4
n=1
while n<=5:
n = n+1
print(”hello”)
Step 3: initialise n
Step 4: print “hello”
Step 5: increment n by 1
n <-- n+1
Step 8: stop
sum=0
for i in range(1,11):
sum=sum+i
Step 7: stop
if val==”l”:
break
print(val)
print(”the end”)
if val==”l”:
continue
print(val)
print(”the end”)
WEEK 5
5a . Write a python program to perform fallowing
operation on set.
i) Union
(ii) Difference
(iii) Symmetric Difference
(iv) Intersection.
set1={10,20,30,40,50}
set2={30,40,50,60,70}
print(set1.intersection(set2))
print(set1.union(set2))
print(set1.difference(set2))
print(set1.symmetric_difference(set2))
Algorithm:
Step1: start
Step 2: create a list list1 and list2
Step3:list3 = list1 + list2
print(list3)
newlist = list1.copy()
print(newlist)
list1.sort()
print(list1)
list1.reverse()
print(list1)
print(max(list1))
print(min(list2))
Step5: display the result
Step6: stop
WEEK 7
7 a) Write a python program to perform following
operations
a) get the values of dictionary
b) get the keys of dictionary
c) add the element to dictionary
d) delete the element from dictionary
Algorithm:
Step1: start
Step 2: create dictionary car
car = {”brand”: “Ford”,”model”: “Mustang”,”year”:
1964}
Step3 : perform the operation
x = car.values()
print(x)
y=car.keys()
print(y)
car[”color”] = “red”
print(x)
car.popitem()
print(car)
Step4: stop
Algorithm:
Step1:start
Step2: import array module
Step3: create a array with integer data
array1 = array(’i’, [10,20,30,40,50])
Step4: print (array1[0])
print (array1[2])
Step5: inserting element to the array
array1.insert(1,60)
print(array1)
Step6: removeing element from array
array1.remove(40)
print(array1)
Step7: searching element from array
print(array1.index(50))
Step8: deleting 3rd element from array
array1.pop(2)
print(array1)
Step9:stop
b = “Hello, World!”
print(”characters from position 2 to position 5”)
print(b[2:5])
print(”to uppercase”)
print(b.upper())
print(”to lower case”)
print(b.lower())
print(”after replacing H with j”)
print(b.replace(”H”, “J”))
print(”split the string to sub string”)
print(b.split(”,”))
Algorithm:
Step1: start
Step2: create a string
b = “Hello, World!”
Ste3:print(b[2:5])
step4:print(b.upper())
Step5: print(b.lower())
Step6:print(b.replace(”H”, “J”))
step7: print(b.split(”,”))
Step8: stop
WEEK 9
x = bool(1)
print(x)
x = bin(36)
print(x)
x = pow(4, 3)
print(x)
x = abs(-7.25)
print(x)
Algorithm:
Step1: start
Step2: declare a variable x
Step3: x = bool(1) goto step4
x = bin(36) goto step4
x = pow(4, 3) goto step4
x = abs(-7.25) goto step4
Step4: print(x)
Step5: stop
Algorithm:
Step1:read the input num
Step2:call the function factorial(num)
Step3: def factorial(num)
fact=1
while(num!=0) if condition fails goto step4
find fact=fact*num
num=num-1
Step4: then print(fact)
9 C. Write a program to multiple of two values using
lambda function?
r=lambda x,y : x*y
print(r(12,3))
WEEK -10
10 a. Write a python program to perform following
using built-in modules
a) Find square root of number
b) Factorial of given number
c) Find the addition of two numbers
d) Check whether first number greater than second
or not
e) Find mod of two numbers
Algorithm:
Step1:start
Step2: import math module and operator module
Step3: declare a variables a,b
Step4: print(sqrt(16))
Step 5: print(factorial(5))
Step 6: print(mul(a, b))
Step 7: print(gt(a, b))
Step 8: print(mod(a, b))
Step 9: stop
WEEK - 11
import numpy as np
a=[1,3,4]
b=[4,5,6]
print(np.subtract(a,b))
print(np.divide(a,b))
print(np.multiply(a,b))
print(np.sqrt(a))
WEEK -12
12. a) Write a program to read first 5 characters from
a files?
f=open(“test.txt”,”r”)
print(f.read(5))
WEEK 13
13 a. Write a python code to integrate exception
handling
try:
f = open(”demo.txt”)
try:
f.write(”Hello everyone”)
except:
print(”Something went wrong when writing to the
file”)
finally:
f.close()
except:
print(”Something went wrong when opening the file”)