Practical File (Python)
1. Count Vowels and Consonants
Write a Python program to input a string and count the number of
vowels and consonants separately.
PROGRAM:
text = input("Enter a string: ").lower()
vowels = "aeiou"
v=c=0
for ch in text:
if [Link]():
if ch in vowels:
v += 1
else:
c += 1
print("Vowels:", v)
print("Consonants:", c)
OUTPUT :
Enter a string: computer
Vowels: 3
Consonants: 5
2. Palindrome Sentence
Write a program to check whether a sentence is palindrome after
removing spaces and punctuation.
PROGRAM:
s = input("Enter a sentence: ").lower()
clean = ""
for ch in s:
if [Link]():
clean += ch
if clean == clean[::-1]:
print("Palindrome Sentence")
else:
print("Not a Palindrome")
OUTPUT :
Enter a sentence: Never odd or even
Palindrome Sentence
3. Fibonacci Series
Write a program to generate Fibonacci series up to N terms.
PROGRAM:
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a+b
OUTPUT :
Enter number of terms: 7
0112358
4. Word Frequency Counter
Write a program to count frequency of each word.
PROGRAM:
text = input("Enter a paragraph: ").lower().split()
freq = {}
for word in text:
freq[word] = [Link](word, 0) + 1
print(freq)
OUTPUT :
Enter a paragraph: cat dog cat lion dog
{'cat': 2, 'dog': 2, 'lion': 1}
5. Remove Duplicates from List
Write a program to remove duplicates without using set().
PROGRAM:
lst = list(map(int, input("Enter numbers: ").split()))
unique = []
for i in lst:
if i not in unique:
[Link](i)
print(unique)
OUTPUT :
Enter numbers: 1 2 2 3 4 4 5
[1, 2, 3, 4, 5]
6. Largest & Second Largest
Write a program to find largest & second largest.
PROGRAM:
lst = list(map(int, input("Enter numbers: ").split()))
lst = list(set(lst))
[Link](reverse=True)
print(lst[0], lst[1])
OUTPUT:
Enter numbers: 10 45 23 45 89
89 45
7. Strong Number
Check whether a number is Strong Number.
PROGRAM:
num = int(input("Enter number: "))
s=0
n = num
def fact(x):
f=1
for i in range(1, x+1):
f *= i
return f
while n>0:
s += fact(n%10)
n//=10
print("Strong" if s==num else "Not Strong")
OUTPUT:
Enter number: 145
Strong
8. Reverse Every Word
Reverse each word but keep order same.
PROGRAM:
s = input("Enter sentence: ")
words = [Link]()
rev = [w[::-1] for w in words]
print(" ".join(rev))
OUTPUT:
Enter sentence: hello world
olleh dlrow
9. Armstrong Number
WAP to Check Armstrong number.
PROGRAM:
n=int(input("Enter number: "))
s=sum(int(d)**3 for d in str(n))
print("Armstrong" if s==n else "Not Armstrong")
OUTPUT:
Enter number: 153
Armstrong
10. Prime Numbers in Range
WAP to Display prime numbers in a range.
PROGRAM:
a=int(input("Start: "))
b=int(input("End: "))
for n in range(a,b+1):
if n>1:
for i in range(2,n):
if n%i==0: break
else: print(n,end=" ")
OUTPUT:
Start: 10
End: 20
11 13 17 19
11. Sort Dictionary by Values
WAP to Sort dictionary by values.
PROGRAM:
d={"Sam":88,"John":77,"Riya":92}
sorted_d=dict(sorted([Link](), key=lambda x:x[1]))
print(sorted_d)
OUTPUT:
{'John': 77, 'Sam': 88, 'Riya': 92}
12. Binary Search
WAP to perform binary search.
PROGRAM:
lst=sorted(list(map(int,input().split())))
key=int(input("Enter key: "))
low,high=0,len(lst)-1
found=False
while low<=high:
mid=(low+high)//2
if lst[mid]==key:
found=True
break
elif lst[mid]<key: low=mid+1
else: high=mid-1
print("Found" if found else "Not Found")
OUTPUT:
Enter list: 1 3 5 7 9
Enter key: 7
Found
13. Matrix Transpose
Matrix transpose.
PROGRAM:
r=int(input("Rows: "))
c=int(input("Cols: "))
mat=[]
for i in range(r):
row=list(map(int,input().split()))
[Link](row)
transpose=[[mat[j][i] for j in range(r)] for i in range(c)]
for row in transpose:
print(row)
OUTPUT:
Rows: 2
Cols: 2
12
34
[1, 3]
[2, 4]
14. Count Letters, Digits, Special Characters
WAP to Count categories.
PROGRAM:
s=input("Enter text: ")
d=a=sp=0
for ch in s:
if [Link](): d+=1
elif [Link](): a+=1
else: sp+=1
print(d,a,sp)
OUTPUT:
Enter text: Hello123@
Digits: 3
Letters: 5
Special: 1
15. Merge Two Sorted Lists
WAP to Merge sorted lists.
PROGRAM:
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=j=0; c=[]
while i<len(a) and j<len(b):
if a[i]<b[j]: [Link](a[i]); i+=1
else: [Link](b[j]); j+=1
[Link](a[i:]); [Link](b[j:])
print(c)
OUTPUT:
List1: 1 3 5
List2: 2 4 6
[1,2,3,4,5,6]
16. Electricity Bill
WAP to generate Electricity bill calculation.
PROGRAM:
u=int(input("Units: "))
if u<=100: bill=u*5
elif u<=200: bill=100*5+(u-100)*7
else: bill=100*5+100*7+(u-200)*10
print(bill)
OUTPUT:
Units: 250
2050
17. Calculator Using Functions
WAP to create a Calculator in python.
PROGRAM:
def add(a,b): return a+b
def sub(a,b): return a-b
def mul(a,b): return a*b
def div(a,b): return a/b
a=float(input("A: "))
b=float(input("B: "))
op=input("Op: ")
if op=="+": print(add(a,b))
elif op=="-": print(sub(a,b))
elif op=="*": print(mul(a,b))
elif op=="/": print(div(a,b))
else: print("Invalid")
OUTPUT:
A: 10
B: 5
Op: *
50
18. Count Lines in File
WAP to Count lines in a file.
PROGRAM:
file=open("[Link]","r")
lines=[Link]()
print(len(lines))
[Link]()
OUTPUT:
Total Lines: 5
19. Common Elements
Common elements in lists.
PROGRAM:
a=list(map(int,input().split()))
b=list(map(int,input().split()))
common=[x for x in a if x in b]
print(common)
OUTPUT:
List1: 1 2 3 4
List2: 3 4 5 6
[3, 4]
20. Decimal to Binary
WAP to Convert decimal to binary.
PROGRAM:
n=int(input("Enter num: "))
binary=""
while n>0:
binary=str(n%2)+binary
n//=2
print(binary)
OUTPUT:
Enter num: 10
1010