Rajan Mishra
Python Programs
1) Python Program to Calculate the Average of Numbers in a
Given List
Program/Source Code
n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
elem=int(input("Enter element: "))
a.append(elem)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
Output
Case 1:
Enter the number of elements to be inserted: 3
Enter element: 23
Enter element: 45
Enter element: 56
Average of elements in the list 41.33
Case 2:
Enter the number of elements to be inserted: 5
Enter element: 12
Enter element: 24
Enter element: 33
Enter element: 25
Enter element: 18
Average of elements in the list 22.4
Rajan Mishra
2) Python Program to Check Whether a Given Year is a Leap Year
Program/Source Code
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!)
else:
print("The year isn't a leap year!)
Output
Case 1:
Enter year to be checked:2016
The year is a leap year!
Case 2:
Enter year to be checked:2005
The year isn't a leap year!
3) Python Program to Find the Largest Number in a List
Program/Source Code
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Output
Case 1:
Enter number of elements:3
Enter element:23
Enter element:567
Enter element:3
Rajan Mishra
Largest element is: 567
Case 2:
Enter number of elements:4
Enter element:34
Enter element:56
Enter element:24
Enter element:54
Largest element is: 56
4) Python Program to Replace all Occurrences of ‘a’ with $ in
a String
Program/Source Code
string=raw_input("Enter string:")
string=string.replace('a','$')
string=string.replace('A','$')
print("Modified string:")
print(string)
Output
Case 1:
Enter string:Apple
Modified string:
$pple
Case 2:
Enter string:Asia
Modified string:
$si$
5)Python Program to Add a Key-Value Pair to the Dictionary
Program/Source Code
key=int(input("Enter the key (int) to be added:"))
value=int(input("Enter the value for the key to be added:"))
d={}
d.update({key:value})
print("Updated dictionary is:")
Rajan Mishra
print(d)
Output
Case 1:
Enter the key (int) to be added:12
Enter the value for the key to be added:34
Updated dictionary is:
{12: 34}
Case 2:
Enter the key (int) to be added:34
Enter the value for the key to be added:29
Updated dictionary is:
{34: 29}
6) Python Program to Count the Number of Vowels Present
in a String using Sets
Program/Source Code
s=raw_input("Enter string:")
count = 0
vowels = set("aeiou")
for letter in s:
if letter in vowels:
count += 1
print("Count of the vowels is:")
print(count)
Output
Case 1:
Enter string:Hello world
Count of the vowels is:
3
Case 2:
Enter string:Python Program
Count of the vowels is:
3
7) Python Program to Find the Factorial of a Number Using
Recursion
Rajan Mishra
Program/Source Code
def factorial(n):
if(n <= 1):
return 1
else:
return(n*factorial(n-1))
n = int(input("Enter number:"))
print("Factorial:")
print(factorial(n))
Output
Case 1:
Enter number:5
Factorial:
120
Case 2:
Enter number:9
Factorial:
362880
8) Python Program to Find the Sum of Digits in a Number
without Recursion
Program/Source Code
l=[]
b=int(input("Enter a number: "))
while(b>0):
dig=b%10
l.append(dig)
b=b//10
print("Sum is:")
print(sum(l))
Output
Case 1:
Rajan Mishra
Enter a number: 123
Sum is:
6
Case 2:
Enter a number: 567
Sum is:
18
9) Python Program to Count the Number of Words in a Text
File
Program/Source Code
fname = input("Enter file name: ")
num_words = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
print("Number of words:")
print(num_words)
Output
Case 1:
Contents of file:
Hello world
Output:
Enter file name: data1.txt
Number of words:
2
Case 2:
Contents of file:
This programming language is
Python
Output:
Enter file name: data2.txt
Number of words:
5
Rajan Mishra
10) Python Program to Find the Area of a Rectangle Using
Classes
Program/Source Code
class rectangle():
def __init__(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
return self.breadth*self.length
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())
print()
Output
Case 1:
Enter length of rectangle: 4
Enter breadth of rectangle: 5
Area of rectangle: 20
Case 2:
Enter length of rectangle: 15
Enter breadth of rectangle: 13
Area of rectangle: 195