1.Find all the prime numbers from list.
list1=[5,7,9,15,17]
primelist=[]
for i in list1:
flag=0
for j in range(2,i):
if(i%j==0):
flag=1
if(flag==1):
continue
else:
primelist.append(i)
print(primelist)
2.Find the square of each number in list.
list1=[10,20,30]
for i in list1:
print("square of",i,"=",i*i)
3.find the Factorial of each number in list.
import math
list1=[5,6,7,8]
for i in list1:
print(math.factorial(i))
4.check the given number is palindrome or not.
no=int(input("enter any number="))
s1=str(no)
s2=s1[::-1]
if(s1==s2):
print("Number is palindrome")
else:
print("number is not palindrome")
5.check each number in list is palindrome or not.
list1=[151,555,423]
for i in list1:
s1=str(i)
Made by Akash Gaikwad
if(s1==s1[::-1]):
print(i,"is palindrome")
else:
continue
6.Check whether the given number is Armstrong or not.
number=int(input("Enter any number="))
temp=number
rem=1
sum=0
while(number>0):
rem=number%10
sum=sum+rem**3
number=number//10
if(temp==sum):
print(temp,"is armstrong number")
else:
print(temp,"is not armstrong number")
7.find all the armstrong numbers from list.
list1=[153,555,443]
armlist=[]
for i in list1:
rem=0
temp=i
sum=0
while(i>0):
rem=i%10
sum=sum+rem**3
i=i//10
if(temp==sum):
armlist.append(temp)
print(armlist)
8.Count the frequency of each character in file.
f1=open('akash.txt')
data=f1.read()
for i in data:
print("Occurance of ",i,"=",data.count(i))
Made by Akash Gaikwad
8.find the length of given file.
f1=open('count_fre.py','r')
data=f1.read()
print("Length of file=",len(data))
9.print the last line of file.
f1=open('count_fre.py','r')
data=f1.readlines()
print("Last line of file=",data[-1])
10.count the total lines from file.
f1=open('count_fre.py','r')
data=f1.readlines()
print("Last line of file=",len(data))
11.count the total number of uppercase,lowercase,digits & special
symbols from any file.
f1=open('count_fre.py','r')
data=f1.read()
up=0
low=0
dg=0
sp=0
for i in data:
if(i.isupper()):
up+=1
elif(i.islower()):
low+=1
elif(i.isdigit()):
dg+=1
else:
sp+=1
print("Total uppercase letters=",up)
print("Total lowercase letters=",low)
print("Total digits=",dg)
print("Total special letters=",sp)
Made by Akash Gaikwad
12.write a program which copy the contents of one file from into
another file.
f1=open('count_fre.py','r')
data=f1.read()
f2=open("akash.txt","w")
f2.write(data)
13.write a program which copy the alternate characters from one file
into another file.
f1=open('count_fre.py','r')
data=f1.read()
f2=open("akash.txt","w")
for i in range(0,len(data),2):
f2.write(data[i])
14.Accept any file and replace all uppercase with lowercase and
viseversa,and replace all digits with 0 ,and replace special symbols
with $.
filename=input("Enter any filename=")
f1=open(filename,'r')
f2=open('asch.txt','r+')
data=f1.read()
for i in data:
if(i.isupper()):
f2.write(i.lower())
elif(i.islower()):
f2.write(i.upper())
elif(i.isdigit()):
f2.write('0')
elif(i.isspace()):
continue
else:
f2.write('$')
data2=f2.read()
print(data2)
Made by Akash Gaikwad
15.write a program which open file in write mode and append some
contents in file.
f1=open("akash.txt","w")
list1=["hello","i","am","akash"]
f1.writelines(list1)
f1.close()
f2=open("akash.txt","a")
f2.write("\nfrom ahmednagar")
f2.close()
f3=open("akash.txt","r")
print(f3.read())
f3.close()
16.write a program which throws user defined excpetion when age is
less than 18.
class invalidage(Exception):
def __init__(self,age):
print("age is less than 18 user not aligible for voting")
try:
age=int(input("Enter age="))
if(age<18):
raise invalidage(age)
else:
print("you are aligible for voting")
except invalidage as x:
None
Made by Akash Gaikwad