The document is a worksheet for Class XII Computer Science students at Bharathi Vidyalaya Senior Secondary School, containing various programming tasks. Each task requires the student to write a specific Python function that performs operations on lists, dictionaries, or strings, such as summing values, filtering elements, or counting occurrences. The exercises aim to enhance students' understanding of functions and data manipulation in Python.
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 ratings0% found this document useful (0 votes)
138 views4 pages
Xii Functions
The document is a worksheet for Class XII Computer Science students at Bharathi Vidyalaya Senior Secondary School, containing various programming tasks. Each task requires the student to write a specific Python function that performs operations on lists, dictionaries, or strings, such as summing values, filtering elements, or counting occurrences. The exercises aim to enhance students' understanding of functions and data manipulation in Python.
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/ 4
BHARATHI VIDHYALAYA SENIOR SECONDARY SCHOOL
WORK SHEET – FUNCTIONS
CLASS: XII SUBJECT: COMPUTER SCIENCE 1. Define a function ZeroEnding(SCORES) to add all those values in the list of SCORES, which are 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 2. Python funtion oddeve(L) to print positive numbers in a list L. Example: Input: [4, -1, 5, 9, -6, 2, -9, 8] Output: [4, 5, 9, 2, 8] 3. Write a function ARRNG(string) which accepts a string arguments and returns a string containing all 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” 4. Write a function called rem_keys( D,keylist) that accepts two parameters: a dictionary called D and 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. 5. Write a function count_city(CITY) in Python, that takes the dictionary, CITY as an argument and 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 6. Write a function countAlpha(String) that takes a string as an argument and returns a dictionary 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} 7. Write a function countMy(SUBJECT) in Python, that takes the dictionary, SUBJECT as an 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 8. Write a function dispTop(SCORES) in Python, that takes a dictionary SCORES as an argument and 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] 9. Write a function distinction(marks) in Python, that takes the dictionary marks as an argument and 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 10. Write a function EVEN_LIST(L), where L is the list of elements passed as argument to the function. 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] 11. Write a function in PythonConvert() to replaces elements having even values with its half and 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 12. Write a function in Shift(Lst), Which accept a List ‘Lst’ as argument and swaps the elements of 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 13. Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the 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] 14. Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the function. The function returns sum of odd nos in list . 15. Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the 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] 16. Write a function itemshift(X), where X is the list of elements passed as an argument to the 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] 17. Write a function letter_count(lst) that takes a list of string and returns a 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} 18. Write a function listchange(Arr)in Python, which accepts a list Arr of numbers, the function will 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] 19. Write a function listchange(Arr,n)in Python, which accepts a list Arr of numbers and n is an numeric 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] 20. Write a function max_length( ) ,that takes a list of string as argument and display the longest string from the list. 21. Write a function modilst(L) that accepts a list of numbers as argument and increases the value of the 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] 22. Write a function R_Shift(Arr,n) in Python, which accepts a list Arr of numbers and n is a numeric 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] 23. Write a function RShift(Arr) in Python, which accepts a list Arr of numbers and places all even 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] 24. Write a function seminars(events) in Python, that takes the dictionary 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’} 25. Write a function shift2() that accept a list as parameter. The function should exchange first and last 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] 26. Write a function shiftn(L,n), where L is a list of integers and n is an integer. The function should 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] 27. Write a function Show_sal(EMP) in python that takes the dictionary, EMP as an argument. Display 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} 28. Write a function SQUARE_LIST(L), where L is the list of elements passed as argument to the 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] 29. Write a function that takes two numbers and return that has maximum one’s digit. (for eg if 491 and 278 are passed it will return 278 as it has got maximum one’s digit) 30. Write a function Word_Len(text), that takes a text as an argument and returns a tuple containing 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) 31. Write a function, countArticles(String), that takes a string as an argument and returns a dictionary 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} 32. Write a function, lenLines(STRING), that takes a string as an argument and returns a tuple 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) 33. Write a function, VowelWords(Str), that takes a string as an argument and returns a tuple containing 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”) 34. Write a program to calculate and display the sum of all the odd numbers in the list. 35. Write a Python function SwitchOver(Val) to swap the even and odd positions of the values in the list 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] 36. Write a python Function that accepts a string and calculates the number of uppercase letters and lowercase letters. Sample String : Python ProgrammiNg Expected Output: Original String : Python ProgrammiNg No. of Upper case characters : 3 No. of Lower case characters :14 37. Write a Python Program containing a function FindWord(STRING, SEARCH), that accepts two 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. 38. Write a user-defined function find-name (name), where name is an argument in Python to delete phone number from a dictionary phone-book on the basis of the name, where name is the key. 39. Write afunction in Python Convert()to replaces elements having even values with its half and 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 40. Write definition of a function How_Many(List, elm) to count and display number of times the value 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 41. Write definition of a Method MSEARCH(STATES) to display all the state names from a list of 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 42. Write definition of a method/function AddOddEven(VALUES) to display sum of odd and even 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 43. Write the definition of a function Reverse (x) in Python, to display the elements in reverse order 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: