0% found this document useful (0 votes)
76 views3 pages

Python Functions for Data Management

The document contains a series of Python function definitions for various tasks, including removing elements from a list, adding contacts to a phone book, identifying perfect squares, updating student marks, and counting even numbers. Each function is provided with a brief description and example code. It serves as a programming exercise for students learning Python functions.

Uploaded by

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

Python Functions for Data Management

The document contains a series of Python function definitions for various tasks, including removing elements from a list, adding contacts to a phone book, identifying perfect squares, updating student marks, and counting even numbers. Each function is provided with a brief description and example code. It serves as a programming exercise for students learning Python functions.

Uploaded by

sushant.sm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VELAMMAL VIDYALAYA – SHOLINGANALLUR.

XII – COMPUTER SCIENCE (083)


FUNCTION
1. Write a function remove_element() in 2. Write a Python function add_contact() that
Python that accepts a list L and a number n. accepts a dictionary phone_book, a name, and a
If the number n exists in the list, it should phone number. The function should add the
be removed. If it does not exist, print a name and phone number to the dictionary. If the
message saying "Element not found". name already exists, print "Contact already
Answer: exists" instead of updating it.
def remove_element(L, n): Answer:
if n in L: def add_contact(phone_book, name, number):
[Link](n) if name in phone_book:
print(L) print("Contact already exists")
else: else:
print("Element not found") phone_book[name] = number
print("Contact added successfully")
3. Write a function Insert_pn() in Python [Link] a Python function topper() that accepts
that accepts a list named Perfect and a a dictionary resultsheet that contains student
number n. If the number is a perfect square, details { rollno: [ name,mark]}. The function
add the number to the end of the given list should return the name and rollno of the
else display a message stating “Number is student who scored highest mark. Display as
not Perfect number”. “Topper is xyz with rollno abc”.
Answer: Answer:
import math rs={1:['ajay',100],2:['siva',80],3:['saran',70]}
def Insert_pn(Perfect, n): def topper():
if n >= 0 and int([Link](n)) ** 2 == n: mark=0
[Link](n) no=0
else: name=""
print("Number is not Perfect number") for i,j in [Link]():
if j[1]>mark:
mark=j[1]
no=i
name=j[0]
return [no,name]
x,y=topper()
print("Topper is",y,"with no is",x)

1
[Link] a Python function update_marks() that [Link] a function count_even() in Python
accepts a dictionary Marks with student names that accepts a list of integers as input and
as keys and their marks as values, along with a returns the count of even numbers
name and new_mark. The function should present in the list.
update the student’s mark if the name exists, Answer:
otherwise print "Student not found". def count_even(numbers):
Answer: count = 0
def update_marks(Marks, name, new_mark): for num in numbers:
if name in Marks: if num % 2 == 0:
Marks[name] = new_mark count += 1
print("Marks updated") return count
else:
print("Student not found")
[Link] a function countNow(PLACES) in Python, [Link] a function, lenWords(STRING),
that takes the dictionary, PLACES as an argument that takes a string as an argument and
and displays the names (in uppercase) of the returns a tuple containing length of each
places whose names are longer than 5 word of a string.
characters. For example, Consider the following For example, if the string is
dictionary "Come let us have some fun",
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New the tuple will have (4, 3, 2, 4, 4, 3)
York",5:"Doha"} Answer:
The output should be: def lenWords(STRING):
LONDON NEW YORK t = ()
Answer: for i in [Link]():
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New x = len(i)
York",5:"Doha"} t = t + (x , )
def countNow(PLACES): return t
for i in [Link]():
if len(i) > 5:
print([Link]())
countNow(PLACES)

2
9. Write a function INDEX_LIST(L), where L is 10. Write a function modilst(L) that accepts a
the list of elements passed as argument to list of numbers as argument and increases
the function. The function returns another the value of the elements by 10 if the
list named indexList that stores the indices of elements are divisible by 5. Also write a
all Non-Zero Elements of L. proper call statement for the function.
For example: For example:
If L contains [12,4,0,11,0,56] If list L contains [3,5,10,12,15]
The indexList will have - [0,1,3,5] Then the modilist() should make the list L as
Answer: [3,15,20,12,25]
def INDEX_LIST(L): Answer:
indexList=[ ] def modilst(L):
for i in range(len(L)): for i in range(len(L)):
if L[i]!=0: if L[i] % 5 == 0:
[Link](i) L[i]+=10
return indexList L = [12,10,15,20,25]
modilst(L)
print(L)
11. Write a python function count_element() [Link] a function Fun1() which takes a list
to accept a set of negative as well as positive of integers as parameter and returns sum of
numbers in a list. Display the sum of negative those numbers which are divisible by 3 in the
even numbers and positive odd numbers list. For example if the list
available in the list. (Both sum separately). L1=[2,3,4,6,7,9,11,12,14], then the function
Answer: should return 3+6+9+12=30
def count_element(numbers): Answer:
sum_neg_even = 0 def Fun1(L1):
sum_pos_odd = 0 s=0
for n in numbers: for x in L1:
if n < 0 and n % 2 == 0: if x%3==0:
sum_neg_even += n s=s+x
elif n > 0 and n % 2 != 0: return s
sum_pos_odd += n L1=[2,3,4,6,7,8,9,11,12,14]
print(sum_neg_even) print(Fun1(L1))
print(sum_pos_odd)

You might also like