1
Ex 1 (a) GCD Of Two Numbers
import math
a=int(input(“Enter the first number:” ))
b=int(input(“Enter the second number:”))
print("GCD of a and b is :")
print(math.gcd(a, b))
Output:
Enter the first number:36
Enter the second number:54
GCD of a and b is :
18
Ex 1 (b) Prime Numbers in the range
min = int(input(“Enter the starting range: ”))
max = int(input(“Enter the ending range:”))
print("Prime numbers between the numbers are")
for n in range(min,max):
if n > 1:
for i in range(2,n):
if (n % i) == 0:
break
if i==n-1:
print(n)
Output:
Enter the starting range: 1
Enter the ending range: 20
Prime numbers between the numbers are
3
5
7
11
13
17
19
Ex 2 (a) Leap Year or Not
year=int(input(“Enter year:”))
if ((year%400==0)or((year%100!=0)and(year%4==0))):
print(year," is a leap year")
else:
print(year," is a leap year")
Output:
Enter year:2024
2024 is a leap year
Enter year:2023
2
2023 is not a leap year
Ex 2 (b) Armstrong Number in a range
import math
print("Armstrong number between 100 to 2000:")
for num in range(100, 2000):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
Output:
Armstrong number between 100 to 2000:
153
370
371
407
1634
Ex 3 (a) String Operations
S="Welcome to Python"
print(S[0:8])
print(S[11:])
T=" Python "
print(T.strip())
print(T.lstrip())
print(T.rstrip())
Output:
Welcome
Python
Python
Python
Python
Ex 3 (b) Find the number of vowels, Characters & Spaces
import string
str=input(“enter the string:”)
vow=['a','e','i','o','u','A','E','I','O','U']
char=list(string.ascii_letters[:52])
3
char_count=0
vow_count=0
space_count=0
for i in str:
if i in vow:
vow_count=vow_count+1
if i in char:
char_count=char_count+1
elif i==' ':
space_count=space_count+1
print(vow_count)
print(char_count)
print(space_count)
Output:
enter the string: i am a superhero
7
13
3
Ex 4(a) Function to display numbers divided by 3 and not divided by 5 in a range
def display_numbers():
min=int(input(“Enter the min value:”))
max=int(input(“Enter the max value:”))
print(“Numbers divided by 3 and not divisible by 5 between min and max are:”)
for i in range(min,max):
if (i % 3==0) and (i%5!=0):
print(i)
display_numbers()
Output:
Enter the min value: 1
Enter the max value: 20
Numbers divided by 3 and not divisible by 5 between min and max are:
3
6
9
12
18
Ex 4(b) Fibonacci Series
def fib(n):
if n<=1:
return n
else:
return(fib(n-1)+fib(n-2))
nterms=int(input("Enter the number of terms you want in Fibonacci series:"))
4
for i in range(nterms):
print(fib(i))
Output:
Enter the number of terms you want in Fibonacci series: 5
0
1
1
2
3
Ex 5 String Suffix
string = input("Enter the word to add ing/ly:")
if len(string) < 3:
print(string)
elif string[-3:] == 'ing':
print(string + 'ly')
else:
print(string + 'ing')
Output:
Enter the word to add ing/ly: working
Workingly
Enter the word to add ing/ly: do
do
Enter the word to add ing/ly: work
Working
Ex 6 Minimum & Maximum in a List
print("enter list Element elements one by one")
L=[ ]
for i in range(n):
x=int(input(“Enter Element:”))
L.append(x)
print("minimum value:")
print(min(L))
print("maximum value:")
print(max(L))
Output:
enter size of list:5
enter list elements one by one
Enter Element:55
Enter Element:65
Enter Element:32
Enter Element:65
5
Enter Element:73
minimum value: 32
maximum value: 73
Ex 7 Reverse of a list
n = int(input("Enter the size of list:"))
print("Enter list elements one by one")
L=[ ]
for i in range(n):
x=int(input(“Enter Element:”))
L.append(x)
print("The list is")
print(L)
print("Reserve of list :")
L.reverse()
print(L)
Output:
Enter the size of list:5 Enter list elements one by one Enter Element:2
Enter Element:3 Enter Element:5 Enter Element:4 Enter Element:1 The list is
[2, 3, 5, 4, 1]
Reserve of list :
[1, 4, 5, 3, 2]
Ex 8 Print the Tuples in Half size
T=(1,2,3,4,5,6,7,8,9,10)
print("tuple is")
tuple_length=len(T)
print(T[ :tuple_length //2])
print(T[tuple_length //2: ])
Output:
tuple is
(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)
Ex 9 Longest Word in a List of Words
n=int(input(“Enter the numbers of words:”))
S=[ ]
for i in range(n):
x=input(“ Enter word:”)
S.append(x)
print("The longest Word is")
print(max(S,key=len))
Output:
Enter the numbers of words:5
Enter word:H
Enter word:HELLO
6
Enter word:HI
Enter word:HOW ARE YOU
Enter word:THANK YOU
The longest Word is HOW ARE YOU
Ex 10 Linear Search
n=int(input(“Enter the no. of elements:”))
S=[ ]
for i in range(n):
a=int(input(“Enter Element:”))
S.append(a)
x=int(input("enter element to search:"))
for i in range(n):
if x==S[i]:
print("element found at position"+str(i+1))
break
if i==n-1:
print("Not found")
Output:
Enter the no. of elements:5
Enter Element:3
Enter Element:2
Enter Element:5
Enter Element:3
Enter Element:1
enter element to search:3
element found at position1
Ex 11 Selection Sort
n=int(input(“Enter the number of Elements:”))
s=[ ]
for i in range(n):
x=int(input(“Enter Element:”))
s.append(x)
for step in range(n):
min_index = step
for i in range(step + 1,n):
if s[i] < s[min_index]:
min_index = i
(s[step], s[min_index]) = (s[min_index], s[step])
print('The array after sorting in Ascending Order by selection sort is:')
print(s)
Output:
7
Enter the number of Elements:5
Enter Element:2
Enter Element:3
Enter Element:4
Enter Element:1
Enter Element:6
The array after sorting in Ascending Order by selection sort is: [1, 2, 3, 4, 6]
Ex 12 Matrix Multiplication
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print(“Resultant Matrix is:”)
for r in result:
print(r)
Output:
Resultant Matrix is:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Ex 13 Tuple Operations
Tup1=(1,3,4,2,'hi',5,'hello')
Tup2=(1,3,4)
print("Concatenation")
Tup3=Tup1+Tup2
print(Tup3)
print("Repetition")
print(Tup2*3)
print("Length")
print(len(Tup3))
Output:
Concatenation
(1, 3, 4, 2, 'hi', 5, 'hello', 1, 3, 4)
8
Repetition
(1, 3, 4, 1, 3, 4, 1, 3, 4)
Length
10
Ex 14 Dictionary Functions
dic={101:'Ramu',102:'Babu',103:'kumar',104:'Somu'}
print("length:")
print(len(dic))
print(dic[101])
print(dic.keys())
print(dic.values())
print(dic.items())
print(dic.get(102))
dic2=dic.copy()
print(dic2)
dic.popitem()
print(dic)
dic.clear()
print(dic)
Output:
length:
4
Ramu
dict_keys([101, 102, 103, 104]) dict_values(['Ramu', 'Babu', 'kumar', 'Somu'])
dict_items([(101, 'Ramu'), (102, 'Babu'), (103, 'kumar'), (104, 'Somu')]) Babu
{101: 'Ramu', 102: 'Babu', 103: 'kumar', 104: 'Somu'}
{101: 'Ramu', 102: 'Babu', 103: 'kumar'}
{}
EX 15 Copy File Contents
src = r'C:\Users\DCSE4\Desktop\first.txt'
trg=r'C:\Users\DCSE4\Desktop\second.txt'
wordcount=0
with open(src,'r') as firstfile, open(trg,'a') as secondfile:
for line in firstfile:
secondfile.write(line)
wordcount=wordcount+len(line.split())
print(“Total no. of words copied from file1 are:”)
print(wordcount)
Output:
first.txt second.txt
9
hi hi
hello hello
how are you how are you
Total no. of words copied from file1 are: 5