All Important Program in Python For o Level Exam
All Important Program in Python For o Level Exam
O-Level (M3-R5)
All Python Important
Program for O-Level
Video Tutorial
O-Level M3-R5 Full Video
Playlist Available on
YouTube Channel UPCISS
1
Website For Free PDF Notes: www.upcissyoutube.com
Output:
length = len(str(num))
sum = 0
temp = num
while(temp != 0):
sum = sum + ((temp % 10) ** length)
temp = temp // 10
if sum == num:
print("Armstrong Number")
else:
print("Not Armstrong Number")
Output:
2
Website For Free PDF Notes: www.upcissyoutube.com
num = int(input("Enter number of digits you want in series (minimum 2): "))
first = 0
second = 1
first = second
second = next
Program Using Recursion:
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
num = int(input("Enter number of digits you want in series (minimum 2): "))
if num <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci series is:")
for i in range(num):
print(recur_fibo(i),end=',')
Output:
3
Website For Free PDF Notes: www.upcissyoutube.com
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
Enter a number: 67
67 Prime number
5. Write a program to Find Factorial of Number.
Program Using Loop:
num = int(input("Enter a number: "))
fac = 1
for i in range(1, num + 1):
fac = fac * i
print("Factorial of ", num, " is ", fac)
Output:
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
Enter a number: 6
Factorial of 6 is 720
6. Write a program to Check Leap Year.
Program:
Output:
4
Website For Free PDF Notes: www.upcissyoutube.com
Output:
num1 = 5
num2 = 6
total = 0
for i in range(num2):
total+=num1
print("Result",total)
print("Result",rep_add(5,6))
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
Result 30
5
Website For Free PDF Notes: www.upcissyoutube.com
def GCD(a,b):
if(b==0):
return a
else:
return GCD(b,a%b)
print (GCD(60,48))
Output:
Output:
6
Website For Free PDF Notes: www.upcissyoutube.com
DecimalToBinary(8)
Output:
binary1 = binary
decimal, i, = 0, 0
while binary != 0:
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
print(binary1,'=',decimal)
binaryToDecimal(10101)
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
10101 = 21
7
Website For Free PDF Notes: www.upcissyoutube.com
[6, 7, 5, 8, 4, 9]
[6, 5, 7, 8, 4, 9]
[6, 5, 7, 8, 4, 9]
[6, 5, 7, 4, 8, 9]
[5, 6, 7, 4, 8, 9]
[5, 6, 7, 4, 8, 9]
[5, 6, 4, 7, 8, 9]
[5, 6, 4, 7, 8, 9]
[5, 4, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
Sorted List: [4, 5, 6, 7, 8, 9]
8
Website For Free PDF Notes: www.upcissyoutube.com
9
Website For Free PDF Notes: www.upcissyoutube.com
10
Website For Free PDF Notes: www.upcissyoutube.com
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
Total Wages: 1780
18. Write a function that takes a string as parameter and
returns a string with every successive repetitive character
replaced by? e.g. school may become scho?l.
Program:
def fun1(st):
ch=[]
for i in st:
ch.append(i)
for i in range(len(ch)-1):
if ch[i] == ch[i+1]:
ch[i+1] = '?'
return ''.join(ch)
st = 'schools schools'
print(fun1(st))
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
scho?ls scho?ls
11
Website For Free PDF Notes: www.upcissyoutube.com
12
Website For Free PDF Notes: www.upcissyoutube.com
20. Write a Python function that takes two lists and returns
true if they have at least one common item.
Program:
def common_data(list1, list2):
result = False
for x in list1:
for y in list2:
if x == y:
result = True
return result
return result
a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]
print(common_data(a, b))
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9]
print(common_data(a, b))
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
True
False
13
Website For Free PDF Notes: www.upcissyoutube.com
14
Website For Free PDF Notes: www.upcissyoutube.com
22. Write a program that takes sentence as input from the user and
computes the frequency of each letter. Use a variable of dictionary type to
maintain and show the frequency of each letter.
Program:
test_str = "welcome to upciss"
all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print ("Count of all characters in GeeksforGeeks is :\n " + str(all_freq))
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ python kk.py
Count of all characters in GeeksforGeeks is :
{'w': 1, 'e': 2, 'l': 1, 'c': 2, 'o': 2, 'm': 1, ' ': 2, 't': 1, 'u': 1, 'p': 1, 'i': 1, 's': 2}
23. Write a function that takes two filenames f1 and f2 as input. The function
should read the contents of f1 line by line and write them onto f2.
Program:
def filefunc(f1,f2):
openf1=open(f1,'r')
openf2=open(f2,'w+')
print("File 1 Content:\n",openf1.read())
openf1.seek(0)
openf2.write(openf1.read())
openf2.seek(0)
print("File 2 Content:\n",openf2.read())
openf1.close()
openf1.close()
15
Website For Free PDF Notes: www.upcissyoutube.com
24. Write a function that reads the contents of the file f3.txt and counts the
number of alphabets, blank spaces, lowercase letters, and number of words
starting with a vowel.
Program:
def filefunc(f3):
file3=open(f3,'r')
snt=file3.read()
file3.close()
if len(snt) == 0:
print("String is Empty")
else:
alpha=space=lower=vowel=0
for i in snt:
if i.isalpha():
alpha+=1
elif i.isspace():
space+=1
if i.islower():
lower+=1
vow='aeiou'
li=snt.split(' ')
for word in li:
for x in vow:
if word.startswith(x):
vowel+=1
print(f"File Content:\n{snt}\n")
print(f"Number of Alphabets: {alpha}")
print(f"Number of Blank Space: {space}")
print(f"Number of Lowercase: {lower}")
print(f"Starting with a vowel: {vowel}")
filefunc("f3.txt")
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ py demo.py
File Content:
Hello Students!
Welcome T0 UPCISS
Thank for Like and Subscribe
Number of Alphabets: 51
Number of Blank Space: 9
Number of Lowercase: 38
Starting with a vowel: 1
16
Website For Free PDF Notes: www.upcissyoutube.com
25. Write a program to replace ‘a’ with ‘b’, ‘b’ with ‘c’……’z’ with ‘a’ and
similarly for ‘A’ with ‘B’,’B’ with ‘C’…..…. ‘Z’ with ‘A’ in a file. The other
characters should remain unchanged.
Program:
s1='UPci$$ c0mpuTer'
s2=[]
for i in s1:
if i.isalpha():
s2.append(chr(ord(i)+1))
else:
s2.append(i)
s2=''.join(s2)
print(s2)
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ py demo.py
VQdj$$ d0nqvUfs
26. Write a NumPy program to find the most frequent value in an array.
Program:
import numpy as np
x = np.array([5,9,6,9,6,2,6])
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print(np.bincount(x).argmax())
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ py demo.py
Original array:
[5 9 6 9 6 2 6]
Most frequent value in the above array:
6
27. Take two NumPy arrays having two dimensions. Concatenate the arrays
on axis 1.
Program:
import numpy as np
arr1 = np.arange(1,10).reshape(3,3)
arr2 = np.arange(10,19).reshape(3,3)
print(arr1)
print()
print(arr2)
print()
print(np.concatenate((arr1,arr2),axis=1))
17
Website For Free PDF Notes: www.upcissyoutube.com
Output:
UPCISS@DESKTOP-32007C6 MINGW64 ~/desktop
$ py demo.py
[[1 2 3]
[4 5 6]
[7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]
[[ 1 2 3 10 11 12]
[ 4 5 6 13 14 15]
[ 7 8 9 16 17 18]]
It takes a lot of hard work to make notes, so if you can pay some fee 50, 100,
200 rupees which you think is reasonable, if you are able to Thank you...
नोट् स बनाने में बहुत मेहनत लगी है , इसललए यलि आप कुछ शुल्क 50,100, 200 रूपए जो
आपको उलित लगता है pay कर सकते है , अगर आप सक्षम है तो, धन्यवाि ।
18