Python Lab File Shivang
Python Lab File Shivang
Lab File
For
BACHELOR OF
UTTAR PRADESH
Aim: - Write a python program to print all prime numbers among the interval given by user.
Code:-
n = int(input("enter the
number:")) p = int(input("enter
the number:")) for j in
range(n, p):
if j >= 2:
for i in range(2,
j+1): if i < j:
if j % i ==
0: break
else:
print(i
) break
Output: -
2|Pa ge
Experiment no. 2
Output: -
3|Pa ge
Experiment no. 3
Aim: - WAP to Find the maximum digit and second maximum digit in the integer.
Code: -
n = int(input("enter a number:"))
s = str(n) # in order to convert it to
string l = list(s) # in order to convert
it to list a = int(l[0])
b = int(l[1])
c = int(l[2])
if a > b:
if a > c:
max = a
if c >
b:
max2 =
c else:
max2 = b
else:
max = c
max2 =
a
else:
if b > c:
max = b
if c >
a:
max2 =
c else:
max2 = a
else:
max = c
max2 =
4|Pa ge
Output: -
5|Pa ge
Experiment no. 4
Code: -
Output: -
6|Pa ge
Experiment no. 5
Aim: - write a program to swap two values using function with argument using python.
Code: -
def swap(a,b):
a=(a^b)
b=(a^b)
a=(a^b)
print('The value of x after swapping is ', a)
print('The value of y after swapping is ', b)
x=int(input('Enter x'))
y=int(input('Enter y'))
swap(x,y)
Output: -
7|Pa ge
Experiment no. 6
Aim: - WAP Count no of Vowels in a string
Code: -
str=input('Enter a word')
count=0
for i in str.lower():
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u'):
count=count+1
print(count)
Output: -
8|Pa ge
Experiment no. 7
Aim: - WAP to demonstrate slicing in strings.
Code: -
country='Argentina'
print(country[2:7])
print(country[2:])
print(country[:7])
print(country[::-1])
Output: -
9|Pa ge
Experiment no. 8
Aim: - WAP to print Local time.
Code: -
import datetime
current_time = datetime.datetime.now()
print(current_time.now())
Output: -
10 | P a g e
Experiment no. 9
Aim: - WAP to Read a List of Words and Return the Length of the Longest One[ use List Datatype]
Code: -
def longest_word(a):
longest_length=len(a[0])
longest_string=a[0]
for i in a:
if(len(i)>longest_length):
longest_length=len(i)
longest_string=i
a=["Monday","Tuesday","Saturday","Argentina","Maharashtra"]
longest_word(a)
Output: -
11 | P a g e
Experiment no. 10
Aim: -WAP to Find second max and second minimum value in an array of Ten integers.
Code: -
def second_max_min(a):
length=len(a)
a.sort()
a=[7,3,1,8,5,98,46,34,109,67]
second_max_min(a)
Output: -
12 | P a g e
Experiment no. 11
Code: -
l = eval(input("enter the list in squre
brackets:")) l.sort()
print("Largest no. in the list is:", l[-1])
Output: -
13 | P a g e
Experiment no. 12
Aim: -WAP to Unpack a given tuple.
Code: -
a=("Shivang Singh",20,"lUCKNOW",168)
(name,age,city,height)=a
print(name)
print(age)
print(city)
print(height)
Output: -
14 | P a g e
Experiment no. 13
Aim: -WAP to Check if a Given Key Exists in a Dictionary or Not.
Code: -
d = {'Rohan': 4, 'Ayush': 7, 'Aditya': 9, 'Akhilendra': 12,
'Jatin': 45, 'Umad': 2, 'Shivang':
56} print("Dictionary is:", d)
n = input("enter the key that you want to
search:-") if n in d:
print("Key", n, "is in the
dictionary.") else:
print("Key", n, "is not in the dictionary.")
Output: -
15 | P a g e
16 | P a g e
Experiment no. 14
Aim: -WAP to Read the Contents of a File in Reverse Order
Code: -
myfile = open("text/normal.txt",
'r') str = myfile.read()
l = len(str)
print("reverse order of text
file:") for i in range(-1, -l-1,
-1):
Output: -
1. normal.txt
17 | P a g e
18 | P a g e
Experiment no. 15
Aim: -WAP that Reads a Text File and Counts the Number of Times a Certain Letter Appears in
the Text File.
Code: -
myfile = open("text/poem.txt",
'r') str = myfile.read()
k = len(str)
l = input("enter the letter that you want to
search:") c = 0
for i in range(0,
k): if l ==
str[i]:
c =
c+1 else:
continue
print(c)
Output: -
1.poem.txt
19 | P a g e
Experiment no. 16
Code: -
import csv
filename = "university_records.csv"
Output: -
20 | P a g e
Experiment no. 17
Code: -
import csv
filename = "university_records.csv"
Output: -
21 | P a g e
Experiment no. 18
Code: -
def power(n):
return lambda a : a ** n
base = power(2)
base = power(5)
print("Now power is set to 5")
Output: -
22 | P a g e
Experiment no. 19
Code: -
index = 0
num_array[index] = -1
print("num_array after update 1: {}".format(num_array))
Output: -
23 | P a g e
Experiment no. 20
Aim: - Write a program to demonstrate creation of Set in Python for updating element in array.
Code: -
set1 = set()
print("Initial blank Set: ")
print(set1)
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
Output: -
24 | P a g e
Experiment no. 21
Aim: - Write a program to demonstrate working of ways to concatenate tuples using + operator.
Code: -
test_tup1 = (1, 3, 5)
test_tup2 = (4, 6)
Output: -
25 | P a g e
Experiment no. 22
Aim: - Write a program to demonstrate working of ways to concatenate tuples using + operator.
Code: -
test_tup1 = (1, 3, 5)
test_tup2 = (4, 6)
Output: -
26 | P a g e
Experiment no. 23
Aim: - Write a program to import time module and compare time of loop and list.
Code: -
import time
def for_loop(n):
result = []
for i in range(n):
result.append(i**2)
return result
def list_comprehension(n):
return [i**2 for i in range(n)]
begin = time.time()
for_loop(10**6)
end = time.time()
begin = time.time()
list_comprehension(10**6)
end = time.time()
Output: -
27 | P a g e
Experiment no. 23
Code: -
import time
def for_loop(n):
result = []
for i in range(n):
result.append(i**2)
return result
def list_comprehension(n):
return [i**2 for i in range(n)]
begin = time.time()
for_loop(10**6)
end = time.time()
begin = time.time()
list_comprehension(10**6)
end = time.time()
Output: -
28 | P a g e
Experiment no. 24
plt.plot(xpoints, ypoints)
plt.show()
Output: -
29 | P a g e
30 | P a g e