Akshat Gulati
XII-C
FUNCTIONS
1. Program to calculate the sum of even numbers entered by the user
def evensum(numbers):
total=0
for num in numbers:
if num%2==0:
total+=num
return total
n=int(input("How many numbers?: "))
user_numbers=[]
for i in range(n):
num=int(input("Enter Number: "))
user_numbers.append(num)
result=evensum(user_numbers)
print("Sum of even numbers: ",result)
#OUTPUT
How many numbers?: 5
Enter Number: 2
Enter Number: 3
Enter Number: 5
Enter Number: 4
Enter Number: 8
Sum of even numbers: 14
1|Pag e
Akshat Gulati
XII-C
2. Write a Python program that stores book names in a dictionary with serial numbers.
Display only those book names that do not start with a vowel.
vowels = 'aeiouAEIOU'
def dispbook(BOOKS):
for i in BOOKS.values():
if i[0].lower() not in vowels:
print(i)
books = {}
z = int(input("Enter no. of books: "))
for j in range(z):
main_ = int(input("Enter Sno.: "))
b_name = input("Enter book name: ")
books[main_] = b_name
print(books)
dispbook(books)
#OUTPUT
Enter no. of books: 3
Enter Sno.: 1
Enter book name: Harry Potter
Enter Sno.: 2
Enter book name: Chotta Bheem
Enter Sno.: 3
Enter book name: The Art Of War
{1: 'Harry Potter', 2: 'Chotta Bheem', 3: 'The Art Of War'}
Harry Potter
Chotta Bheem
The Art Of War
2|Pag e
Akshat Gulati
XII-C
3. Write a function that takes a sentence and a word as input and returns how many times
the word occurs in the sentence.
def findword(str_, search_):
c=0
str1 = str_.split()
for i in str1:
if i == search_:
c += 1
return c
str_ = input("Enter a statement: ")
search_ = input("Enter word to search: ")
print(findword(str_, search_))
#OUTPUT
Enter a statement: this is a test this is only a test
Enter word to search: test
3|Pag e
Akshat Gulati
XII-C
4. Write a Python program to check whether a given word or sentence contains at least one
vowel. Return True if it does, otherwise return False.
def checkvowel(text):
vowels = "AEIOUaeiou"
for ch in text:
if ch in vowels:
return True # vowel found
return False # no vowels
x = input("Enter word or sentence: ")
print(checkvowel(x))
#OUTPUT
Enter word or sentence: sky
False
Enter word or sentence: hello
True
4|Pag e
Akshat Gulati
XII-C
5. Create a Python function that accepts a sentence and replaces all vowels in it with an
asterisk *
str_ = input("Enter A sentence: ")
def swap(str_):
swap_ = "*"
for i in str_:
if i in "AEIOUaeiou":
str_ = str_.replace(i, swap_)
return str_
print(swap(str_))
#OUTPUT
Enter A sentence: Apple
*ppl*
5|Pag e
Akshat Gulati
XII-C
6. Write a Python program that stores place names in a dictionary with serial numbers.
Display the names that are longer than 5 characters in uppercase.
PLACES = {}
n = int(input("Enter no. of places: "))
for i in range(n):
key = int(input("Enter Sno.: "))
name = input("Enter name: ")
PLACES[key] = name
print(PLACES)
def countNow(PLACES):
for i in PLACES.values():
if len(i) > 5:
y = i.upper()
print(y)
countNow(PLACES)
#OUTPUT
Enter no. of places: 3
Enter Sno.: 1
Enter name: Paris
Enter Sno.: 2
Enter name: NewYork
Enter Sno.: 3
Enter name: Amsterdam
{1: 'Paris', 2: 'NewYork', 3: 'Amsterdam'}
NEWYORK
AMSTERDAM
6|Pag e
Akshat Gulati
XII-C
7. Write a Python program that takes a sentence as input and displays a tuple containing the
length of each word in the sentence.
str_ = input("Enter string: ")
def lenwords(st):
sp = st.split()
tup = []
for i in sp:
x = len(i)
tup.append(x)
tup = tuple(tup)
print(tup)
lenwords(str_)
#OUTPUT
Enter string: I love programming
(1, 4, 11)
7|Pag e