0% found this document useful (0 votes)
126 views

Python Programs

The document contains 10 Python programs with their source code and sample outputs. The programs cover a range of tasks including calculating the average of a list, checking if a year is a leap year, finding the largest number in a list, counting vowels in a string, calculating a number factorial using recursion, and finding the area of a rectangle using classes. For each program, the source code and examples of running the program with different inputs and observing the corresponding outputs are provided.

Uploaded by

rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Python Programs

The document contains 10 Python programs with their source code and sample outputs. The programs cover a range of tasks including calculating the average of a list, checking if a year is a leap year, finding the largest number in a list, counting vowels in a string, calculating a number factorial using recursion, and finding the area of a rectangle using classes. For each program, the source code and examples of running the program with different inputs and observing the corresponding outputs are provided.

Uploaded by

rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy