Program 1: Input any number from user and calculate factorial of a number
# Program to calculate factorial of entered number
num = int(input("Enter any number :"))
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from n to 2
fact = fact * num
num-=1
print("Factorial of ", n , " is :",fact)
OUTPUT
Enter any number :6
Factorial of 6 is : 720
Page : 1
Program 2: Input any number from user and check it is Prime no. or not
#Program to input any number from user
#Check it is Prime number of not
import math
num = int(input("Enter any number :"))
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
isPrime=False
if isPrime:
print("## Number is Prime ##")
else:
print("## Number is not Prime ##")
OUTPUT
Enter any number :117
## Number is not Prime ##
>>>
Enter any number :119
## Number is not Prime ##
>>>
Enter any number :113
## Number is Prime ##
>>>
Enter any number :7
## Number is Prime ##
>>>
Enter any number :19
## Number is Prime ##
Page : 2
Program 3 : Write a program to find sum of elements of List recursively
#Program to find sum of elements of list recursively
def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)
mylist = [] # Empty List
#Loop to input in list
num = int(input("Enter how many number :"))
for i in range(num):
n = int(input("Enter Element "+str(i+1)+":"))
mylist.append(n) #Adding number to list
sum = findSum(mylist,len(mylist))
print("Sum of List items ",mylist, " is :",sum)
OUTPUT
Enter how many number :6
Enter Element 1:10
Enter Element 2:20
Enter Element 3:30
Enter Element 4:40
Enter Element 5:50
Enter Element 6:60
Sum of List items [10, 20, 30, 40, 50, 60] is : 210
Page : 3
Program 4: Write a program to calculate the nth term of Fibonacci series
#Program to find 'n'th term of fibonacci series
#Fibonacci series : 0,1,1,2,3,5,8,13,21,34,55,89,...
#nth term will be counted from 1 not 0
def nthfiboterm(n):
if n<=1:
return n
else:
return (nthfiboterm(n-1)+nthfiboterm(n-2))
num = int(input("Enter the 'n' term to find in fibonacci :"))
term =nthfiboterm(num)
print(num,"th term of fibonacci series is :",term)
OUTPUT
Enter the 'n' term to find in fibonacci :10
10 th term of fibonacci series is :
Page : 4
Program 5 : Program to search any word in given string/sentence
#Program to find the occurence of any word in a string
def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count
str1 = input("Enter any sentence :")
word = input("Enter word to search in sentence :")
count = countWord(str1,word)
if count==0:
print("## Sorry! ",word," not present ")
else:
print("## ",word," occurs ",count," times ## ")
OUTPUT
Enter any sentence :my computer your computer our computer everyones computer
Enter word to search in sentence :computer
## computer occurs 4 times ##
Enter any sentence :learning python is fun
Enter word to search in sentence :java
## Sorry! java not present
Page : 5
Program 6: Program to read and display file content line by line with each
word separated by „#‟
#Program to read content of file line by line
#and display each word separated by '#'
f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
NOTE : if the original content of file is:
India is my country
I love python
Python learning is fun
OUTPUT
India#is#my#country#
I#love#python#
Python#learning#is#fun#
Page : 6
Program 7: Program to read the content of file and display the total number
of consonants, uppercase, vowels and lower case characters‟
#Program to read content of file
#and display total number of vowels, consonants, lowercase and uppercase characters
f = open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
NOTE : if the original content of file is:
India is my country
I love python
Python learning is fun
123@
OUTPUT
Total Vowels in file : 16
Total Consonants in file n : 30
Total Capital letters in file :2
Total Small letters in file : 44
Total Other than letters :4
Page : 7
Program 8: Program to create binary file to store Rollno and Name, Search
any Rollno and display name if Rollno found otherwise “Rollno not found”
#Program to create a binary file to store Rollno and name
#Search for Rollno and display record if found
#otherwise "Roll no. not found"
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
Page : 8
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Add More ?(Y)y
Enter Roll Number :2
Enter Name :Jasbir
Add More ?(Y)y
Enter Roll Number :3
Enter Name :Vikral
Add More ?(Y)n
Enter Roll number to search :2
## Name is : Jasbir ##
Search more ?(Y) :y
Enter Roll number to search :1
## Name is : Amit ##
Search more ?(Y) :y
Enter Roll number to search :4
####Sorry! Roll number not found ####
Search more ?(Y) :n
Page : 9
Program 9: Program to create binary file to store Rollno,Name and Marks
and update marks of entered Rollno
#Program to create a binary file to store Rollno and name
#Search for Rollno and display record if found
#otherwise "Roll no. not found"
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m = int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
Page : 10
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Enter Marks :99
Add More ?(Y)y
Enter Roll Number :2
Enter Name :Vikrant
Enter Marks :88
Add More ?(Y)y
Enter Roll Number :3
Enter Name :Nitin
Enter Marks :66
Add More ?(Y)n
Enter Roll number to update :2
## Name is : Vikrant ##
## Current Marks is : 88 ##
Enter new marks :90
## Record Updated ##
Update more ?(Y) :y
Enter Roll number to update :2
## Name is : Vikrant ##
## Current Marks is : 90 ##
Enter new marks :95
## Record Updated ##
Update more ?(Y) :n
Page : 11
Program 10: Program to read the content of file line by line and write it to
another file except for the lines contains „a‟ letter in it.
#Program to read line from file and write it to another line
#Except for those line which contains letter 'a'
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()
NOTE: Content of file2.txt
a quick brown fox
one two three four
five six seven
India is my country
eight nine ten
bye!
OUTPUT
## File Copied Successfully! ##
NOTE: After copy content of file2copy.txt
one two three four
five six seven
eight nine ten
bye!
Page : 12