279 Python Practicals 1to4
279 Python Practicals 1to4
Solanki 20BECE30279
Practical Set - 1
b=int(input("B: "))
temp=a
a=b
b=temp
print("A:",a,"B:",b)
Output: A: 44
B: 28
A: 28 B: 44
Output: A: 10
B: 45
A: 45 B: 10
print("Floor Division:",x//y)
Practical Set –2
print("ax^2 + bx + c")
a=int(input("Enter a: "))
b=int(input("Enter b: "))
c=int(input("Enter c: "))
# calculate the Discriminant
else:
print("Real root is not possible")
Output: ax^2 + bx + c
Enter a: 2
Enter b: -11
Enter c: 5
Different Roots
20.0 2.0
if a>b:
if a>c:
print(a,"is Maximum .")
else:
print(c,"is Maximum .")
else:
if b>c:
print(b,"is Maximum .")
else:
6. WAP to calculate the salary of an employee based on following conditions (nested if-else):
1. if degree = B.E. and experience < 5 years, salary=30000
2. if degree = B.E. and experience >= 5 years, salary=40000
3. if degree = M.E. and experience < 5 years, salary=50000
4. if degree = M.E. and experience >= 5 years, salary= 60000
Code: degree=input("Enter Degree:(1 for B.E. and 2 for M.E.): ")
if degree = ='1':
degree="B.E."
else:
degree="M.E."
salary=40000
else:
if experience < 5:
salary=50000
else:
salary=60000
print("Salary: ",salary)
7. WAP to check whether entered input is character, digit or special symbol using ladder if-else.
print("a is digit")
elif a.isalpha():
print("a is character")
else :
Practical Set – 3
last_term=1
print(second_last,last_term,sep="\t",end="\t")
for i in range(1,n-1):
last_term+=second_last
second_last=last_term
print(last_term,end="\t")
else:
print("Reversed Number:",rev_num)
Output: Enter Number:8547
Reversed Number: 7458
7. WAP to print all even numbers between 1 to n except the numbers divisible by 6.
Code: n=int(input("Enter N:"))
for i in range(2,n+1,2):
if i%6==0:
pass
else:
print(i,end="\t")
rev_num=rev_num*10+digit
temp//=10
if num==rev_num:
Practical Set - 4
1. Write a python program which covers all the methods (functions) of list.
Code: import copy
list1=["hii","good morning",1,4,3,6]
print("Default list:",list1)
list1.append(6)
print("After Append: ",list1)
list2=list1.copy()
print("Shallow copy List:",list2)
print("Deep Copy:",copy.deepcopy(list1))
list2.clear()
print("Cleared List2:",list2)
print("3 in list1:",list1.count(3))
list1.extend([7,8,[10,11]])
print("Extended List:",list1)
print("Index Of 4 in list1:",list1.index(4))
list1.insert(4,'ldrp')
print("insert Element at index 4:",list1)
print("Poped element:",list1.pop(5))
list1.remove("hii")
print("Removed hii:",list1)list1.reverse()
print("reversed list:",list1)
list4=[5,76,3,45,32,5,7]
print("maximum in list4:",max(list4))
list4.sort()
print("Sorted List",list4)
Cleared List2: []
3 in list1: 1
Index Of 4 in list1: 3
Poped element: 3
maximum in list4: 76
3. Write a python program to check whether the given list is palindrome or not.
Code: list1=[]
4. Write a python program to store strings in list and then print them.
Code: list1=[]
n=int(input("Enter Total no. of Element:"))
for i in range(n):
j=input("Enter Element:")
list1.append(j)
print("List 1 : " ,list1)
5. Write a python program to print list of prime numbers up to N using loop and else clause.
Code: n=int(input("Enter N:"))
prime=[]
for i in range(1,n+1):
for j in range(2,int(i**0.5)+1):
if (i % j) == 0:
break
else:
prime.append(i)
print(prime)
Multiplication : 2000
Output: Enter N: 4
Enter number:14
Enter number:28
Enter number:100
Enter number:45
Largest number: 100
9. Write a Python program to count the number of strings where the string length is 2 or more
and the first and last character are same from a given list of strings.
Code: l1=[]
n=int(input("Enter Total no. of Element:"))
for i in range(n):
j=input("Enter Element:")
l1.append(j)
count=0
for i in l1:
if len(i)>2 and i[0]==i[-1]:
count+=1
else:
print("Total No. of strings with more than 2 len & same first and last char: ",count)
11. Write a Python program to find the list of words that are longer than n from a given string.
Code: s1=input("Enter String:")
n=int(input("Enter min length of word: "))
l1=s1.split()
l2=[]
for i in l1:
if len(i)>n:l2.append(i)
print("List of words:",l2)
12. Write a Python function that takes two lists and returns True if they have at least one common
member.
Code: def match(l1,l2):
for i in l1:
for j in l2:
if i= =j:return True
else:return False
l1=[4,5,6,7,2,3]
l2=[8,9,10,5,11,2]
print("True") if match(l1,l2) else print("False")
Output: True
13. Write a Python program to print the numbers of a specified list after removing even from it.
Code: l1=[1,2,3,4,5,6,7,8,9,10]
for i in l1:
if i%2==0:
l1.remove(i)
print(l1)
Output: [1, 3, 5, 7, 9]
Subject: Python Programming P a g e | 19
Nehal .V. Solanki 20BECE30279
for i in range(len(X)):
for j in range(len(Y)):
sum[i][j] = X[i][j]+Y[i][j]
printMat("X:",X)
printMat("Y:",Y)
printMat("Sum:",Y)
Output: X:
[2, 4, 5]
[5, 6, 7]
[1, 2, 3]
Y:
[7, 8, 9]
[10, 11, 12]
[3, 4, 5]
Sum:
[7, 8, 9]
[10, 11, 12]
[3, 4, 5]
[4, 5, 6]
[6, 7, 8]
[8, 9, 10]
Transposed Matrix:
[1, 4, 6, 8]
[2, 5, 7, 9]
[3, 6, 8, 10]
flattenList = []
def flatten(L):
for element in L:
if type(element) is list:
flatten(element)
else:
flattenList.append(element)
return flattenList
print("Nested List:",l1)
print("Flatten List:",flatten(l1))
Output: Nested List: [1, [12, 3], [4, 5, [6, [7, 8]]]]
Flatten List: [1, 12, 3, 4, 5, 6, 7, 8]
print("Splitted List:",l2)