0% found this document useful (0 votes)
61 views19 pages

Pythonn 1 To 8 & 11

This document contains code snippets from 8 Python programming practical assignments. The code covers topics like checking if a character is a vowel, generating the Fibonacci sequence, filtering positive numbers from a list, finding maximum and minimum values from a dictionary, transposing a matrix, and validating passwords using regular expressions. The code uses concepts like functions, loops, conditional statements, lists, dictionaries, sets, and regular expressions.

Uploaded by

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

Pythonn 1 To 8 & 11

This document contains code snippets from 8 Python programming practical assignments. The code covers topics like checking if a character is a vowel, generating the Fibonacci sequence, filtering positive numbers from a list, finding maximum and minimum values from a dictionary, transposing a matrix, and validating passwords using regular expressions. The code uses concepts like functions, loops, conditional statements, lists, dictionaries, sets, and regular expressions.

Uploaded by

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

Enrollment No: 202103103520069

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):

l1.append(int(input("Enter element " + str(i+1) + ": ")))

if l1[i]>=0:

l2.append(l1[i])

print("\nPositive numbers in the given list:\n" + str(l2))

Output:

Page | 3
Enrollment No: 202103103520069

Aim: B) Write a program to get the maximum and minimum value from a
dictionary.

Code:
d = {}

n = int(input("Enter The Number Of Element You Want To Add In Dictionary: "))

for i in range(n):

d.update({input("Enter key " + str(i+1) + ": "):int(input("Enter value " + str(i+1) + ": "))})

print("Maximum value: " + str(max(d.values())))

print("Minimum value: " + str(min(d.values())))

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]

[l.append(l[i]+l[i+1]) for i in range(0,n-2)]

print(l)

Output:

Page | 5
Enrollment No: 202103103520069

Aim: B) Write a program to transpose m x n matrix.

Code:
m = int(input("Enter number of col: "))

n = int(input("Enter number of row: "))

i, j= 0, 0

l1 = [[(int(input())) for i in range(m)] for j in range(n)]

l2 = [[(l1[i][j]) for i in range(n)] for j in range(m)]

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

Aim:C) Write a program to count frequency of elements and storing


dictionary using zip().

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

Aim: Write a python program to implement basic mathematical operation on


using numpy and scipy packages.

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

Aim: Using concept of regular expressions, write a program to check the


validity of password input by users. Following are the criteria for checking the
password:
I. At least 1 letter between [a-z]
II. At least 1 number between [0-9]
III. At least one special character
IV. At least 1 letter between [A-Z]
V. Min. length of transaction password:6
VI. Max. length of transaction password:12

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

Aim: A) Write a program to read first and last m lines of a file.


Code:
with open('mitali2.txt','r') as f:
n=int(input("enter no of lines to read from starting:"))
for i in range(n):
print(f.readline())
f.seek(0)
m=int(input("enter no of lines to read from ending:"))
a=f.readlines()
a.reverse()
for i in range(m):
print(a[i])

Output:

Page | 15
Enrollment No: 202103103520069

Aim: B) Write a program to find the longest word from a file.

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

Aim: C) Write a program to count the frequency of words in a file.

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

Aim: Write a program to implement stack and queue in python.

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

You might also like