0% found this document useful (0 votes)
30 views10 pages

Xii Functions Answers

The document provides a series of Python function definitions aimed at performing various tasks, such as summing specific elements in a list, filtering positive numbers, and manipulating strings and dictionaries. Each function is accompanied by examples that illustrate its intended use and expected output. The document serves as a comprehensive guide for implementing basic to intermediate Python programming concepts related to functions.

Uploaded by

umamondal295
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)
30 views10 pages

Xii Functions Answers

The document provides a series of Python function definitions aimed at performing various tasks, such as summing specific elements in a list, filtering positive numbers, and manipulating strings and dictionaries. Each function is accompanied by examples that illustrate its intended use and expected output. The document serves as a comprehensive guide for implementing basic to intermediate Python programming concepts related to functions.

Uploaded by

umamondal295
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
You are on page 1/ 10

Computer Plus Python Functions 9434100649

CLASS: XII SUBJECT: COMPUTER SCIENCE (Functions)


Define a function ZeroEnding(SCORES) to add all those values in the list of SCORES, which are
1. ending with zero (0) and display the sum.
For example :
If the SCORES contain [200, 456, 300, 100, 234, 678]
The sum should be displayed as 600
Python funtion oddeve(L) to print positive numbers in a list L.
2. Example:
Input: [4, -1, 5, 9, -6, 2, -9, 8]
Output: [4, 5, 9, 2, 8]
Write a function ARRNG(string) which accepts a string arguments and returns a string containing all
3. letters arranged in alphabetical order , removing any duplicate occurrence of a letter. for example : if
an string agrument “corporate” is passed then function returns an output string as “aceoprt”
Write a function called rem_keys( D,keylist) that accepts two parameters: a dictionary called D and
4. a list called keylist. Function rem_keys(D,keylist) should remove all the keys contained in the
passed keylist from the dictionary D and return the dictionary.
Write a function count_city(CITY) in Python, that takes the dictionary, CITY as an argument and
5. displays the names (in uppercase) of the cities whose names are smaller than 6 characters.
For example, Consider the following dictionary
CITY = {1:"Ahmedabad", 2:"Pune", 3:"Baroda", 4:"Simla", 5:"Surat"}
The output should be: PUNE SIMLA SURAT
Write a function countAlpha(String) that takes a string as an argument and returns a dictionary
6. containing count of each letter in String.
For example, if the String is ‘Peter Parker’
The dictionary will have
{‘P’:2, ‘e’:3, ‘t’:1, ‘r’:3, ‘ ’:1, ‘a’:1, ‘k’:1}
Write a function countMy(SUBJECT) in Python, that takes the dictionary, SUBJECT as an
7. argument and displays the names (in uppercase) of the subjects whose names are longer than 5
characters. For example, Consider the following dictionary
SUBJECT={1:"Hindi",2:"Physics",3:"Chemistry",4:"cs",5:"Math"}
The output should be:
HINDI
PHYSICS
CHEMISTRY
Write a function dispTop(SCORES) in Python, that takes a dictionary SCORES as an argument and
8. returns the names in uppercase of those players who scored more than 50 as a list.
For example, Consider the following dictionary which is passed as an argument:
SCORES = {“ayan”:56, "Smile” :43, "Pritam”:18, “rehan”:90, “kush”:0}
Then the function should return an output list as : [AYAN , REHAN]
Write a function distinction(marks) in Python, that takes the dictionary marks as an argument and
9. displays the subjects (in upper case) for which marks is greater than or equal to 75.
For example, consider the following dictionary
marks = {‘eng’:78, ‘phy’:69, ‘chem’:74, ‘math’:75, ‘cs’:84}
The output should be:
ENG MATH CS
Computer Plus Python Functions 9434100649
Write a function EVEN_LIST(L), where L is the list of elements passed as argument to the function.
10. The function returns another list named ‘even list’ that stores even numbers in the list.
For example:
If L contains [1,2,3,4,5,6,7,8]
The even list will have - [2,4,6,8]
Write a function in PythonConvert() to replaces elements having even values with its half and
11. elements having odd values with twice its value in a list.
eg: if the list contains 3,4,5,16,9 then rearrange the list as 6,2,10,8,18
Write a function in Shift(Lst), Which accept a List ‘Lst’ as argument and swaps the elements of
12. every even location with its odd location and store in different list
eg. if the array initially contains
2, 4, 1, 6, 5, 7, 9, 2, 3, 10
then it should contain
4, 2, 6, 1, 7, 5, 2, 9, 10, 3
Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
13. function. The function returns another list named ‘indexList’ that stores the indices of all Non-Zero
Elements of L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]
Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
14. function. The function returns sum of odd nos in list .

Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the
15. function. The function returns another list named ‘indexList’ that stores the indices of all Non-Zero
Elements of L.
For example:
If L contains [12, 4, 0, 11, 0, 56]
The indexList will have - [0, 1, 3, 5]
Write a function itemshift(X), where X is the list of elements passed as an argument to the
16. function. The function shifts all the elements by one place to the left and then print it.
For example:
If X contains [1,2,3,4,5,6]
The function will print - [2,3,4,5,6,1]
Write a function letter_count(lst) that takes a list of string and returns a
17. dictionary where the keys are the letters from lst and the values are the number
of times that letter appears in the lst.
For example: if the passed list, lst is :
lst=list(“apple”)
Then it should return a dictionary as {‘a’:1,’p’:2,’l’:1,’e’:1}
Write a function listchange(Arr)in Python, which accepts a list Arr of numbers, the function will
18. replace the even number by value 10 and multiply odd number by 5. Sample Input Data of the list is:
a=[10,20,23,45]
listchange(a,4)
output : [10, 10, 115, 225]
Write a function listchange(Arr,n)in Python, which accepts a list Arr of numbers and n is an numeric
19. value depicting length of the list. Modify the list so that all even numbers doubled and odd number
multiply by 3
Sample Input Data of the list: Arr= [ 10,20,30,40,12,11], n=6
Output: Arr = [20,40,60,80,24,33]
Write a function max_length( ) ,that takes a list of string as argument and display the longest string
20. from the list.
Computer Plus Python Functions 9434100649
Write a function modilst(L) that accepts a list of numbers as argument and increases the value of the
21. elements by 10 if the elements are divisible by 5. Also write a proper call statement for the function.
For example:
If list L contains [3,5,10,12,15]
Then the modilist() should make the list L as [3,15,20,12,25]
Write a function R_Shift(Arr,n) in Python, which accepts a list Arr of numbers and n is a numeric
22. value by which all elements of the list are shifted to right and the last element removed should be
added in the beginning.
Sample Input Data of the list
Arr=[ 1,2,3,4,5,6], n=2
Output
Arr = [5, 6, 1, 2, 3, 4]
Write a function RShift(Arr) in Python, which accepts a list Arr of numbers and places all even
23. elements of the list shifted to left.
Sample Input Data of the list Arr= [10,21,30,45,12,11],
Output Arr = [10, 30, 12, 21, 45, 11]
Write a function seminars(events) in Python, that takes the dictionary
24. events as an argument and return a new dictionary containing the details of
only those events which are seminars. For example, consider the following
dictionary
events={‘Delhi’:‘seminar’, ‘Mumbai’:‘Party’,
‘Dehradun’:‘Marriage’, ‘Goa’:‘seminar’}
The new dictionary should contain:
{‘Delhi’:‘seminar’, ‘Goa’:‘seminar’}
Write a function shift2() that accept a list as parameter. The function should exchange first and last
25. two values of the list and return it. It should also check that the minimum length of the passed list is
more than 4. For example
L1=[4,20,9,78,45,34,76,56]
The returned list should be [76,56,9,78,45,34,4,20]
Write a function shiftn(L,n), where L is a list of integers and n is an integer. The function should
26. return a list after shifting n number of elements to the left.
Example: If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=2
then function should return [3, 14, 7, 9, 19, 6, 1, 10, 2, 15]
If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=4
then function should return [7, 9, 19, 6, 1, 10, 2, 15, 3, 14]
Write a function Show_sal(EMP) in python that takes the dictionary, EMP as an argument. Display
27. the salary if it is less than 25000
Consider the following dictionary
EMP={1:18000,2:25000,3:28000:4:15000}
The output should be:
18000
15000
EMP={1:18000,2:25000,3:35000,4:15000}
Write a function SQUARE_LIST(L), where L is the list of elements passed as argument to the
28. function. The function returns another list named ‘SList’ that stores the Squares of all Non-Zero
Elements of L.
For example:
If L contains [9,4,0,11,0,6,0]
The SList will have - [81,16,121,36]
Write a function that takes two numbers and return that has maximum one’s digit. (for eg if 491 and
29. 278 are passed it will return 278 as it has got maximum one’s digit)
Computer Plus Python Functions 9434100649
Write a function Word_Len(text), that takes a text as an argument and returns a tuple containing
30. length of each word of a text.
For example, if the text is "How are you Rohit", the tuple will have (3, 3, 3, 4)
Write a function, countArticles(String), that takes a string as an argument and returns a dictionary
31. containing the count of each article.
For example if the string is ‘The logical or is an operator not a literal or a punctuator. ’
The resultant dictionary will have
{‘The’:1, ‘an’:1, ‘a’:2}
Write a function, lenLines(STRING), that takes a string as an argument and returns a tuple
32. containing length of each word of a string.
For example, if the string is " let us learn Python", the
tuple will have ( 3, 2, 5, 6)
Write a function, VowelWords(Str), that takes a string as an argument and returns a tuple containing
33. each word which starts with an vowel from the given string
For example, if the string is "An apple a day keeps the doctor away”,
the tuple will have (“An”,”apple”,”a”, “away”)
Write a program to calculate and display the sum of all the odd numbers in the list.
34.

Write a Python function SwitchOver(Val) to swap the even and odd positions of the values in the list
35. Val.
Note : Assuming that the list has even number of values in it.
For example :
If the list Numbers contain
[25,17,19,13,12,15]
After swapping the list content should be displayed as
[17,25,13,19,15,12]
Write a python Function that accepts a string and calculates the number of uppercase letters and
36. lowercase letters.
Sample String : Python ProgrammiNg
Expected Output:
Original String : Python ProgrammiNg
No. of Upper case characters : 3
No. of Lower case characters :14
Write a Python Program containing a function FindWord(STRING, SEARCH), that accepts two
37. arguments : STRING and SEARCH, and prints the count of occurrence of SEARCH in STRING.
Write appropriate statements to call the function.
For example, if STRING = "Learning history helps to know about
history with interest in history" and SEARCH = 'history', the function should display
The word history occurs 3 times.

Write a user-defined function find-name (name), where name is an argument in Python to delete
38. phone number from a dictionary phone-book on the basis of the name, where name is the key.

Write afunction in Python Convert()to replaces elements having even values with its half and
39. elements having odd values with twice its value in a list.
eg: if the list contains 3,4,5,16,9 then
rearranged list as 6,2,10,8, 18
Write definition of a function How_Many(List, elm) to count and display number of times the value
40. of elem is present in the List. (Note: don’t use the count() function)
For example :
If the Data contains [205,240,304,205,402,205,104,102] and elem contains 205
The function should display 205 found 3 Times
Computer Plus Python Functions 9434100649
Write definition of a Method MSEARCH(STATES) to display all the state names from a list of
41. STATES, which are starting with alphabet M.
For example:
If the list STATES contains[“MP’,”UP”,”MH”,”DL”,”MZ”,”WB”]
The following should get displayed
MP
MH
MZ
Write definition of a method/function AddOddEven(VALUES) to display sum of odd and even
42. values separately from the list of VALUES.
For example : If the VALUES contain [15, 26, 37, 10, 22, 13]
The function should display
Even Sum: 58
Odd Sum: 65
Write the definition of a function Reverse (x) in Python, to display the elements in reverse order
43. such that each displayed element is the twice of the original element (element *2) of the List x in the
following manner: Example : If List x contains 7 integers is as follows:
Computer Plus Python Functions 9434100649

Functions
Q1:
def ZeroEnding(SCORES):
total = sum([x for x in SCORES if x % 10 == 0])
print(total)

Q2:
def oddeve(L):
print([x for x in L if x > 0])

Q3:
def ARRNG(string):
return ''.join(sorted(set(string)))

Q4:
def rem_keys(D, keylist):
for key in keylist:
D.pop(key, None)
return D

Q5:
def count_city(CITY):
for city in CITY.values():
if len(city) < 6:
print(city.upper(), end=' ')

Q6:
def countAlpha(String):
d = {}
for ch in String:
d[ch] = d.get(ch, 0) + 1
return d

Q7:
def countMy(SUBJECT):
for name in SUBJECT.values():
if len(name) > 5:
print(name.upper())

Q8:
def dispTop(SCORES):
return [name.upper() for name, score in SCORES.items() if score > 50]

Q9:
def distinction(marks):
for subject, mark in marks.items():
Computer Plus Python Functions 9434100649
if mark >= 75:
print(subject.upper(), end=' ')

Q10:
def EVEN_LIST(L):
return [x for x in L if x % 2 == 0]

Q11:
def PythonConvert(L):
return [x//2 if x % 2 == 0 else x*2 for x in L]

Q12:
def Shift(Lst):
return [Lst[i+1] if i % 2 == 0 else Lst[i-1] for i in range(len(Lst))]

Q13:
def INDEX_LIST(L):
return [i for i in range(len(L)) if L[i] != 0]

Q14:
def INDEX_LIST(L):
return sum([x for x in L if x % 2 == 1])

Q15:
def INDEX_LIST(L):
return [i for i in range(len(L)) if L[i] != 0]

Q16:
def itemshift(X):
X = X[1:] + X[:1]
print(X)

Q17:
def letter_count(lst):
d = {}
for ch in lst:
d[ch] = d.get(ch, 0) + 1
return d

Q18:
def listchange(Arr, n):
for i in range(n):
if Arr[i] % 2 == 0:
Arr[i] = 10
else:
Arr[i] *= 5
return Arr
Computer Plus Python Functions 9434100649
Q19:
def listchange(Arr, n):
for i in range(n):
if Arr[i] % 2 == 0:
Arr[i] *= 2
else:
Arr[i] *= 3
return Arr

Q20:
def max_length(L):
print(max(L, key=len))

Q21:
def modilst(L):
for i in range(len(L)):
if L[i] % 5 == 0:
L[i] += 10
return L

Q22:
def R_Shift(Arr, n):
for _ in range(n):
Arr.insert(0, Arr.pop())
return Arr

Q23:
def RShift(Arr):
even = [x for x in Arr if x % 2 == 0]
odd = [x for x in Arr if x % 2 != 0]
return even + odd

Q24:
def seminars(events):
return {k: v for k, v in events.items() if v == 'seminar'}

Q25:
def shift2(L):
if len(L) > 4:
L[:2], L[-2:] = L[-2:], L[:2]
return L

Q26:
def shiftn(L, n):
return L[n:] + L[:n]

Q27:
def Show_sal(EMP):
for sal in EMP.values():
Computer Plus Python Functions 9434100649
if sal < 25000:
print(sal)

Q28:
def SQUARE_LIST(L):
return [x**2 for x in L if x != 0]

Q29:
def max_ones_digit(a, b):
return a if a % 10 > b % 10 else b

Q30:
def Word_Len(text):
return tuple(len(word) for word in text.split())

Q31:
def countArticles(String):
words = String.split()
return {w: words.count(w) for w in ['The', 'an', 'a']}

Q32:
def lenLines(STRING):
return tuple(len(w) for w in STRING.split())

Q33:
def VowelWords(Str):
vowels = 'aeiouAEIOU'
return tuple(word for word in Str.split() if word[0] in vowels)

Q34:
def sum_odds(L):
print(sum(x for x in L if x % 2 != 0))

Q35:
def SwitchOver(Val):
for i in range(0, len(Val)-1, 2):
Val[i], Val[i+1] = Val[i+1], Val[i]
print(Val)

Q36:
def count_case(s):
upper = sum(1 for c in s if c.isupper())
lower = sum(1 for c in s if c.islower())
print(f"Upper case: {upper}, Lower case: {lower}")

Q37:
def FindWord(STRING, SEARCH):
print(f"The word {SEARCH} occurs {STRING.split().count(SEARCH)} times.")
Computer Plus Python Functions 9434100649
Q38:
def find_name(phonebook, name):
if name in phonebook:
del phonebook[name]

Q39:
def Convert(L):
return [x//2 if x % 2 == 0 else x*2 for x in L]

Q40:
def How_Many(List, elm):
count = 0
for x in List:
if x == elm:
count += 1
print(f"{elm} found {count} Times")

Q41:
def MSEARCH(STATES):
for state in STATES:
if state.startswith('M'):
print(state)

Q42:
def AddOddEven(VALUES):
even = sum(x for x in VALUES if x % 2 == 0)
odd = sum(x for x in VALUES if x % 2 != 0)
print(f"Even Sum: {even}\nOdd Sum: {odd}")

Q43:
def Reverse(x):
for val in reversed(x):
print(val * 2)

You might also like