Python FUNCTIONS
1- Write a function to check whether the number is an
Armstrong number.
def Armstrong(x):
s=0
a=x
while x>0:
k=x%10
s=(s+k)**3
x=x//10
if s==a:
print(“The number is Armstrong”)
else:
print(“The number is not Armstrong”)
2- Write a function to check whether the string is
palindrome or not.
def Palindrome(s):
y=""
for i in range(len(s)-1,-1,-1):
y=y+s[i]
if (y==s):
print(“The string is palindrome”)
else:
print(“The string is not a palindrome”)
3
3- Write a function to count the number of vowels and
consonants in a string.
def string(s):
v=c=0
for i in s:
if i.isalpha():
if i in "AaEeIiOoUu":
v=v+1
else:
c=c+1
print(“number of vowels”,v)
print(“number of consonants”,c)
4- Write a function to pass a list and a number, search
for the number in a list.
def Lsearch(L,val):
if val in L:
print(“found”)
else:
print(“Not found”)
5- Write a function to pass the string and return a
reversed string.
def Sreverse(str):
y=""
for i in range(len(str)-1,-1,-1):
y=y+str[i]
return(y)
4
6-Write a function to print the sum of digits in a
passed number.
def sumdigit(n):
s=0
while n>0:
k=n%10
s=s+k
n=n//10
return(s)
7-Write a function to pass a list and return a reversed
list.
def revlist(L):
L.reverse()
return(L)
8-Write a function to pass a list. If the elements are
even multiply by 3 otherwise multiply by 5.
def xyz(L):
for i in range(0,len(L)):
if L[i]%2==0:
L[i]*=3
else:
L[i]*=5
print(L)
5
9-Write a function to pass two parameters x and n as
integers. Calculate the following sum of series and
return the value:-
1+ x/2! + x2/4! + x3/6! +………xn/2n!
def sumofseries(x,n):
s=0
for i in range(0,n+1):
F=1
for j in range (1,2*i+1):
F=F*j
s=s+(x**i)/F
print("Sum of series=",s)
10- Write a function power(x,n) where x and n are
integers. Calculate xn and return the result.
def power(x,n):
y=x**n
return(y)
11- Write a function which consists of list of integers as
parameter. Interchange even element location with
the odd location element.
def location(L):
for i in range(0,len(L)-1):
if i%2==0:
L[i],L[i+1]=L[i+1],L[i]
print(L)
6
12- Write a function to search an element in a list. If
found replace it by 0 and move all the 0 to the
beginning of the list.
def func(L,n):
k=0
for i in range(len(L)):
if L[i]==n:
L[i]=0
for j in range(i,k,-1):
L[j],L[j-1]=L[j-1],L[j]
k=k+1
print(L)
13- Write a function to display the fibonacci series.
def fibonacci(n):
n1,n2=0,1
print(n1,n2,end=" ")
for i in range(2,n):
n3=n1+n2
n1=n2
n2=n3
print(n3,end=" ")
14- Write a function to accept ‘p’ as principle amount,
‘r’ as rate and ‘t’ as time, calculate and return simple
interest, ‘SI’.
def SI(P,R,T):
S=(P*R*T)/100
7
return(S)
15- Write a function to accept a 2d list. Display the
sum of even elements of the 2d list.
def xyz():
l=[]
for i in range(r):
row=[]
for j in range(c):
x=int(input("Enter a no."))
row.append(x)
l.append(row)
s=0
for i in range(r):
for j in range(c):
if l[i][j]%2==0:
s+=l[i][j]
print(“Sum of even elements”,s)
16- Write a function to accept a 2d array. Find the
sum of those elements which ends with 3.
def abc():
l=[]
for i in range(r):
row=[]
for j in range(c):
x=int(input("Enter a no."))
row.append(x)
l.append(row)
s=0
for i in range(r):
8
for j in range(c):
if l[i][j]%10==3:
s+=l[i][j]
print(“Required sum”,s)
17- Write a function to check whether the number is
prime or not.
def prime(n):
k=0
for i in range(2,n):
if n%i==0:
k=1
if k==0:
print("It is a prime number")
else:
print("It is not a prime number")
18- Write a function to print the maximum number.
def MAX(A,B,C):
if A>B and A>C:
print("Maximum number - ",A)
elif B>A and B>C:
print("Maximum number - ",B)
elif C>A and C>B:
print("Maximum number - ",C)
19- Write a function to print the multiplication table
of a number.
def MTABLE(N):
for i in range(1,11):
print(N,"x",i,"=",N*i)
20- Write a function to print the sum of natural
numbers till N.
9
def NSUM(N):
S=0
for i in range(1,N+1):
S=S+i
print("SUM OF NATURAL NUMBER :",S)
calling statements with their
outputs
1-ARMSTRONG NUMBER
x=int(input("Enter a number"))
Armstrong(x)
OUTPUT
Enter a number 153
The number is Armstrong
2-PALINDROME SERIES
s=input("Enter a string")
Palindrome(s)
OUTPUT
Enter a string madam
The string is palindrome
3-VOWELS AND CONSONANTS IN A STRING
x=input("Enter a string")
string(x)
OUTPUT
Enter a string Python is a computer language
number of vowels 11
number of consonants 14
4- SEARCH FOR A NUMBER IN A LIST
10
L=eval(input("Enter a list"))
val=int(input("value to be search"))
Lsearch(L,val)
OUTPUT
Enter a list [2,4,7,9,5]
value to be search 2
found
5-REVERSED STRING
x=input("Enter a string")
s=Sreverse(x)
print(“Reversed string”,s)
OUTPUT
Enter a string python
Reversed string nohtyp
6- SUM OF DIGITS IN A NUMBER
n=int(input("Enter a number"))
y=sumdigit(n)
print("Sum of digit",y)
OUTPUT
Enter a number 207
Sum of digits 9
7- REVERSED LIST
L=eval(input("Enter a list"))
print(revlist(L))
OUTPUT
Enter a list [1,2,3,4,5]
[5, 4, 3, 2, 1]
8- IF ELEMENTS OF THE LIST ARE EVEN MULTIPLY BY 3
OTHERWISE MULTIPLY BY 5
x=eval(input("Enter a list"))
xyz(x)
11
OUTPUT
Enter a list [2,3,4,5,6,7]
[6,15,12,25,18,35]
9-SUM OF SERIES:-
1+ x/2! + x2/4! + x3/6! +………xn/2n!
x=int(input("Enter a number="))
n=int(input("Enter a number="))
sumofseries(x,n)
OUTPUT
Enter a number= 5
Enter a number= 5
Sum of series= 4.731639936067019
10-POWER FUNCTION (xn)
x=int(input("Enter a number"))
a=power(x,n=2)
print(a)
OUTPUT
Enter a number 5
25
11- INTERCHANGE EVEN LOCATION ELEMENT TO ODD
LOCATION ELEMENT OF A LIST
x=eval(input("Enter a list"))
location(x)
OUTPUT
Enter a list [2,3,4,5,6,7]
[3, 2, 5, 4, 7, 6]
12
12- REPLACE THE SEARCHED ELEMENT BY 0 AND MOVE ALL
THE 0 AT THE BEGINNING OF THE LIST
L=eval(input("Enter a list"))
n=int(input("Enter no. to be searched"))
func(L,n)
OUTPUT
Enter a list [1,2,3,5,2,2,6,2,7,2]
Enter no. to be searched 2
[0, 0, 0, 0, 0, 1, 3, 5, 6, 7]
13- FIBONACCI SERIES
n=int(input("Enter the no. of times"))
fibonacci(n)
OUTPUT
Enter the no. of times 10
0 1 1 2 3 5 8 13 21 34
14- SIMPLE INTEREST
P=int(input("Enter principle amount"))
R=int(input("Enter Rate"))
T=int(input("Enter time period"))
print("Simple interest",SI(P,R,T))
OUTPUT
Enter principle amount 2000
Enter Rate 5
Enter time period 4
Simple interest 400.0
15- SUM OF EVEN ELEMENT OF 2D LIST
r=int(input("Enter no. of rows"))
c=int(input("Enter no. of columns"))
xyz()
OUTPUT
Enter no. of rows 3
Enter no. of columns 3
13
Enter a no. 1
Enter a no. 2
Enter a no. 3
Enter a no. 4
Enter a no. 5
Enter a no. 6
Enter a no. 7
Enter a no. 8
Enter a no. 9
Sum of even elements 20
16- SUM OF THOSE ELEMENT IN 2D LIST WHICH ENDS WITH
3
r=int(input("Enter no. of rows"))
c=int(input("Enter no. of columns"))
abc()
OUTPUT
Enter no. of rows 3
Enter no. of columns 3
Enter a no. 1
Enter a no. 2
Enter a no. 3
Enter a no. 33
Enter a no. 5
Enter a no. 6
Enter a no. 13
Enter a no. 2
Enter a no. 5
Required sum 49
17- PRIME NUMBER
n=int(input("Enter a number "))
prime(n)
14
OUTPUT
Enter a number 53
It is a prime number
18- MAXIMUM NUMBER
A=int(input("Enter first number "))
B=int(input("Enter second number "))
C=int(input("Enter third number "))
MAX(A,B,C)
OUTPUT
Enter first number 10
Enter second number 50
Enter third number 30
Maximum number - 50
19- MULTIPLICATION TABLE
N=int(input("Enter the number "))
MTABLE(N)
OUTPUT
Enter the number 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
20- SUM OF NATURAL NUMBERS TILL N
N=int(input("Enter the number "))
15
NSUM(N)
OUTPUT
Enter the number 5
SUM OF NATURAL NUMBER : 15
file handling
text files fuctions
NOTES.TXT FILE CONTENTS
Data Files-
Data files are the files that store data pertaining to a specific application for later
use. The data files can be stored in two ways-
1- Text files - A text file stores information in the form of a stream of ASCII or
Unicode characters. In this each line is terminated of text with a special
character known as EOL (End of line).
2- Binary files – A binary file stores the information in the form of a stream of
bytes. A binary files contains information in the same format in which
information is held in memory. There is no delimiter for a line.
21-Write a function to read the file notes.txt using
readlines and display its content on the screen.
def abc():
xf=open("notes.txt","r")
y=xf.readlines()
for i in y:
print(i)
xf.close()
16
22-Write a function to read a file notes.txt, count the
number of words in the file.
def xyz():
xf=open("notes.txt","r")
y=' '
c=0
while y:
y=xf.readline()
l=y.split()
c=c+len(l)
print("No. of words=",c)
xf.close()
23-Write a function to read the file Notes.txt , count
number of lines in the file.
def countlines():
xf=open("Notes.txt","r")
y=len(xf.readlines())
print("Total number of lines = ",y)
xf.close()
24-Write a function to read the file notes.txt , count
number of words starting with vowels and consonants.
def count():
xf=open("notes.txt","r")
y=' '
l=[]
c=v=0
while y:
y=xf.readline()
l=y.split()
for i in l:
if i[0].isalpha():
17
if i[0] in "AaEeIiOoUu":
v=v+1
else:
c=c+1
print("No. of words starting with vowels =",v)
print("No. of words starting with consonants =",c)
xf.close()
25-Write a function to read a file notes.txt and count
the number of words which have 3 or 5 characters.
def characters():
xf=open("notes.txt","r")
y=' '
l=[]
c=0
while y:
y=xf.readline()
l=y.split()
for i in l:
if len(i)==3 or len(i)==5:
c=c+1
print("Required characters =",c)
xf.close()
calling statements with their
outputs
18
21- READ A FILE USING READLINES AND DISPLAY ITS
CONTENTS
abc()
OUTPUT
Data Files-
Data files are the files that store data pertaining to a specific application for later
use. The data files can be stored in two ways-
1- Text files - A text file stores information in the form of a stream of ASCII or
Unicode characters. In this each line is terminated of text with a special
character known as EOL (End of line).
2- Binary files – A binary file stores the information in the form of a stream of
bytes. A binary files contains information in the same format in which
information is held in memory. There is no delimiter for a line.
22- COUNT THE NUMBER OF WORDS IN THE FILE
xyz()
OUTPUT
No. of words= 106
23- COUNT THE NUMBER OF LINES IN THE FILE
countlines()
OUTPUT
Total number of lines = 4
24- COUNT THE NUMBER OF WORDS STARTING WITH
VOWELS AND CONSONENTS
count()
OUTPUT
No. of words starting with vowels = 37
No. of words starting with consonants = 65
24- COUNT THE NUMBER OF WORDS WHICH HAVE 3 OR 5
CHARACTERS
characters()
19
OUTPUT
Required characters = 27
20