Pythonn 1 To 8 & 11
Pythonn 1 To 8 & 11
Pythonn 1 To 8 & 11
PRACTICAL 1
Aim: A)Write a program to check whether a passed letter is a vowel or
not.
Code:1
x=input("Enter a character")
print(x)
if(x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u' or x == 'A' or x == 'I' or x == 'O' or x=='U'):
print("It is a vowel")
else:
print("It is not a vowal")
Code:2
x=input("Enter a character")
if x in "aeiouAEIOU":
print("It is a vowel")
else:
print("It is not vowel")
Output:
Page | 1
Enrollment No: 202103103520069
Aim: B) Write a program that creates a function for nth Fibonacci number.
Code:
n = int(input("Enter the number for fibonacci:"))
def fibo(n):
a=0
b=1
print(a)
print(b)
for i in range(0,n-2):
c = a+b
a=b
b=c
print(c)
fibo(n)
Output:
Page | 2
Enrollment No: 202103103520069
PRACTICAL 2
Aim: A) Write a program to filter positive numbers from list.
Code:
l1 = []
l2 = []
for i in range(10):
if l1[i]>=0:
l2.append(l1[i])
Output:
Page | 3
Enrollment No: 202103103520069
Aim: B) Write a program to get the maximum and minimum value from a
dictionary.
Code:
d = {}
for i in range(n):
d.update({input("Enter key " + str(i+1) + ": "):int(input("Enter value " + str(i+1) + ": "))})
Output:
Page | 4
Enrollment No: 202103103520069
PRACTICAL 3
Aim: A) Write a program to print the Fibonacci sequence in comma separeted
from with a given n input by console.
Code:
n = int(input("Enter The Number To Print Fibonacci Sequence: "))
l = [0,1]
print(l)
Output:
Page | 5
Enrollment No: 202103103520069
Code:
m = int(input("Enter number of col: "))
i, j= 0, 0
print("Normal matrix:")
print(l1)
print("Tranposed matrix:")
print(l2)
Output:
Page | 6
Enrollment No: 202103103520069
Practical 4
Aim: A) Find the list of uncommon elements from 2 lists using set.
Code:
l1 = []
l2 = []
print("Enter size of two list: ")
n = int(input())
m = int(input())
print("Enter {0} numbers for list1: ".format(n))
for i in range(n):
l1.append(int(input()))
print("Enter {0} numbers for list2: ".format(m))
for i in range(m):
l2.append(int(input()))
print(list(set(l1) ^ set(l2)))
Output:
Page | 7
Enrollment No: 202103103520069
Aim:B) Find out the list of subjects of a particular semester from the input
provided as a list of dictionaries using lambda, map and filter together.
Code:
subjects = []
print("Input")
items = int(input("Enter the number of items:"))
for i in range(0, items):
sem = int(input("Enter semester:"))
subject = input("Enter subject:")
subjects.append({'sem': sem, 'sub': subject})
print("Output")
sem = int(input("Enter the semester:"))
getSem = lambda x: x['sem'] == sem
getSub = lambda x: x['sub']
selectedSem = filter(getSem, subjects)
selectedSubject = map(getSub, selectedSem)
print('Sem', sem, "subjects:", list(selectedSubject))
Output:
Page | 8
Enrollment No: 202103103520069
Code:
l=[]
print("Enter the size of the list: ")
n = int(input())
print("Enter the values:")
for i in range(n):
ele = int(input())
l.append(ele)
l1=[]
for i in l:
l1.append(l.count(i))
dic = dict(zip(l,l1))
print(dic)
Output:
Page | 9
Enrollment No: 202103103520069
Practical 5
Aim: Define a class named Rocket and its subclass MarsRover. The Rocket
class has an init function that takes two arguments called rocket_name and
target_distance and will initialize object of both classes. Subclass MarsRover
has its own attribute called "Maker". Both classes have a launch() function
which prints rocket name and target distance. Class MarsRover prints
attribute value of "Maker" inside launch()function.
Code:
class Rocket:
def __init__(self,rocket_name, target_distance):
self.rocket_name = rocket_name
self.target_distance = target_distance
def launch(self):
print("Rocket Name:",self.rocket_name)
print("target distance:",self.target_distance)
class MarsRover(Rocket):
def __init__(self,rocket_name,target_distance,Maker):
Rocket.__init__(self,rocket_name,target_distance)
self.Maker = Maker
def launch(self):
print("Rocket Name",self.rocket_name)
print("target distance",self.target_distance)
print("Maker:",self.Maker)
r = Rocket("5V2",100000)
m = MarsRover("67B2",189000,"ISRO")
r.launch()
m.launch()
Page | 10
Enrollment No: 202103103520069
Output:
Page | 11
Enrollment No: 202103103520069
PRACTICAL 6
Code of numpy:
import numpy
arr = numpy.array([1, 2, 3])
print(arr)
newArray= numpy.append (arr, [10, 11, 12])
print(newArray)
array4=numpy.array([7,8,9,10])
array5=numpy.array([6,7,8,9])
array6=numpy.array([7])
arr3=numpy.array([4,5,6,7,8,9])
pre=numpy.subtract(newArray,arr3)
print(pre)
divid=numpy.divide(newArray,arr3)
print(divid)
mult=numpy.multiply(newArray,arr3)
print(mult)
reci=numpy.reciprocal(newArray,arr3)
print(reci)
po=numpy.power(newArray,arr3)
print(po)
mo=numpy.mod(array4,array5)
print(mo)
si=numpy.sin(array4)
print(si)
co=numpy.cos(array4)
print(co)
ta=numpy.tan(array4)
print(ta)
ro=numpy.round(array4)
print(ro)
fl=numpy.float(array6)
print(fl)
ce=numpy.ceil(array6)
print(ce)
Page | 12
Enrollment No: 202103103520069
Output:
Code of scipy:
from scipy import linalg
import numpy as np
a = np.array([[5, 1, 3], [2, -2, 0], [5, 1, 6]])
b = np.array([8, 5, -3])
x = linalg.solve(a, b)
print(x)
A = np.array([[7,8],[9,6]])
x = linalg.det(A)
print(x)
l, v = linalg.eig(A)
print(l)
print(v)
Output:
Page | 13
Enrollment No: 202103103520069
PRACTICAL 7
Code:
import re
password=input("enter the password:")
if(len(password)<=12):
if(len(password)>=6):
if re.search("[A-Z]",password):
if re.search("[0-9]",password):
if re.search("[_@$]",password):
if re.search("[a-z]",password):
print("password:",password)
else:
print("min length of transaction is 6 ")
else:
print("at least 1 letter between [_@$]")
else:
print("at least 1 letter between [0-9]")
else:
print("at least 1 letter between [A-Z]")
else:
print("at least 1 letter between [a-z]")
else:
print("max length of transaction is 12")
Output:
Page | 14
Enrollment No: 202103103520069
PRACTICAL 8
Output:
Page | 15
Enrollment No: 202103103520069
Code:
def longest_word(file) :
with open(file,'r')as f:
n=f.read().split()
m=len(max(n, key=len))
return [n for n in n if len(n)==m]
print(longest_word('p8b.txt'))
Output:
Page | 16
Enrollment No: 202103103520069
Code:
f = open('p8.txt')
b=f.read()
bs=b.split()
print(bs)
d=dict()
for i in bs:
if i in d:
d[i]=d[i]+1
else:
d[i]=1
print(d[i])
Output:
Page | 17
Enrollment No: 202103103520069
PRACTICAL 11
Code:
stack=[]
stack.append('m')
stack.append('i')
stack.append('t')
stack.append('a')
stack.append('l')
stack.append('i')
print('\nstack:')
print(stack,'\n')
print(stack.pop())
print(stack,'\n')
print(stack.pop())
print(stack,'\n')
print(stack.pop())
print('\nAfter stack:')
print(stack,'\n')
queue=["M","i","t","a"]
queue.append("l")
queue.append("i")
print(queue,'\n')
print(queue.pop(0))
Page | 18
Enrollment No: 202103103520069
print(queue,'\n')
print(queue.pop(1))
print(queue,'\n')
Output:
Page | 19