Python Programming

 

PRACTICAL-1

Aim: (A) Write a python program to test whether a passed letter is vowel or not.

m=['a','e','i','o','u','A','E','I','O','U']
l=input("Enter the Alphabet= ")
if l in m:
    print("The alphabet is a Vowel.")
else :
    print("The alphabet is not a Vowel")

Output:











(B) Write a python program to find out the factorial of a number.

def fact(n) :
    if(n==0) :
        return 1
    else:
            return n * fact(n-1)
n=int(input("Enter the number= "))
ans=fact(n)
print("the factorial of the number is:" ,ans)


output:










PRACTICAL-2

Aim: (A)Write a program to filter positive numbers from a list.


a=list()
b=list()
n=int(input("Enter the no. of Elements= "))
for i in range(0,n):
    el=int(input("Enter the Elements= "))
    a.append(el)
print(a)
for i in a:
    if i>0:
        b.append(i)
print("positive numbers-",b)


output:












(B) Write a program to get the maximum and minimum value from

a dictionary.


d={'a':1,'b':4,'c':5}
l=d.keys()
print(l)
l1=list(d.values())
print(l1)
max=l1[0]
min=l1[0]
for i in l1:
    if i>max:
        max=i
    if i<min:
        min=i
print('Maximum no. is- ', max)
print('Minimum no. is- ', min)

output:












Practical-3
Aim:(A) Write a program to print the Fibonacci sequence in
comma separated form with a given n input by console.


n=int(input("Enter the number:"))
def f(n):
    if n==0:  
        return 0
    elif n==1:
        return 1
    else:
        return f(n-1)+f(n-2)

values=[str(f(x)) for x in range (0,n+1)]
l1=','.join(values)
print("The Fibonacci Series is:",l1)

Output:
Enter the number:7 The Fibonacci Series is: 0,1,1,2,3,5,8,13


(B) Write a program to transpose m x n matrix.
m=int(input("Enter the number of rows="))
n=int(input("Enter the number of columns="))
l=[[int(input("Enter elements of matrix:"))for i in range(n)]for i in range(m)]
l1=[[l[i][j] for i in range(m)]for j in range(n)]

print("The original matrix=",l)
print("The matrix after transpose is=",l1)


Output:
Enter the number of rows=4 Enter the number of columns=2 Enter elements of matrix:5 Enter elements of matrix:6 Enter elements of matrix:8 Enter elements of matrix:0 Enter elements of matrix:8 Enter elements of matrix:33 Enter elements of matrix:44 Enter elements of matrix:6 The original matrix= [[5, 6], [8, 0], [8, 33], [44, 6]] The matrix after transpose is= [[5, 8, 8, 44], [6, 0, 33, 6]]






Practical-4A

Aim: Find the list of uncommonelements from 2 lists using set.

a = list()
b = list()

n1 = int(input("Enter The Number Of Elements In First List : "))
for i in range(n1):
    num1 = int(input("Enter The Elements Of The List : "))
    a.append(num1)

n2 = int(input("Enter The Number Of Elements In Second List : "))
for i in range(n2):
    num2 = int(input("Enter The Elements Of The List : "))
    b.append(num2)

s1 = set(a)
s2 = set(b)

# USING SET METHODS
s3 = s1.difference(s2)
s4 = s2.difference(s1)
s5 = s3.union(s4)
c = list(s5)
print("The List Of The Unique Elements : ",c)

Output:


Practical-4(B)
Aim: 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.
i/p:[['sem':6, 'sub': 'python'),('sem':6, 'sub':'cns'),
('sem':5, 'sub':'java'},('sem':5,'sub':'daa'}] o/p:sem 6 subjects:['python', 'cns"]

“To find list of subjects we first create list and dictionary and then we use
the filter, map and lambda keywords ”

l=[]
n=int(input("Enter the number of semester you want=") )
for i in range(n):
    d={}
    d['sem']=int(input("enter the sem : "))
    d['sub']=input("Enter the subject : ")
    l.append(d)
se=int(input("Enter the sem which you want to subjects : "))
l1=list(filter(lambda x : x ['sem']==se,l))
ans=list(map(lambda x : x['sub'],l1))
print("The list of subject is : ",ans)

Output:


Practical-4(C)
Aim: Write a program to count frequency of elements and storing in dictionary
using zip().

“Zip function is used to count the frequency of the string,
zip() function creates an iterator that will aggregate elements from two or
more iterables.”


s=input("Enter a String=")
m=[]
m=s.split(" ")
n=(m.count(i) for i in m)
t=dict(zip(m,n))
print(t)


Output:


Practical-7
Aim: Using concept of regular expressions, write a program to check
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 1 letter between [A-Z]
iv. At least 1 letter special character
v. Min. length of transaction password:6
vi. Max. length of transaction password:12
Code:


import re
s=input("Enter the password- ")
if re.search("[a-z]",s):
    if re.search("[A-Z]",s):
        if re.search("[0-9]",s):
            if(len(s)>5 and len(s)<13):
                print("The password is valid")
            else:
                print("Invalid length")
                print("Invalid password")
        else:
            print("Digits should be present")
            print("Invalid password")
    else:
        print("Uppercase should be present")
        print("Invalid password")
else:
print("lowercase should be present")
print("Invalid password")

Output:




WORD FILE:-




Comments