Cs File
Cs File
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”)
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)
def Lsearch(L,val):
if val in L:
print(“found”)
else:
print(“Not found”)
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)
def revlist(L):
L.reverse()
return(L)
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)
def power(x,n):
y=x**n
return(y)
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)
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=" ")
def SI(P,R,T):
S=(P*R*T)/100
7
return(S)
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")
2-PALINDROME SERIES
s=input("Enter a string")
Palindrome(s)
OUTPUT
Enter a string madam
The string is palindrome
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]
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
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
OUTPUT
Enter a number 53
It is a prime 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.
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()
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()
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.
20