Unit-2
(Working with Functions)
Programming Based Questions:-
Q1. Write definition of a method COUNTNOW(REGIONS) to find and display names of those REGIONS, in which there
are less than or equal to 5 characters.
For example:
If the list REGIONS contains
[“GOA”,”NEW DELHI”,”DAMAN”,”CHENNAI”,”BANGALORE”]
The following should get displayed
GOA
DAMAN
Q2. Write the definition of a method/function SearchOut(Teachers, TName) to search for TName from a list
Teachers, and display the position of its presence.
For example:
If the Teachers contain[“Ankit” , “Siddharth”, “Rahul”, “Sangeeta” , “rahul”]
And TName contains “Rahul”
The functions should display
Rahul at 2
rahul at 4
Q3. Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays
the names(in uppercase) of the places whose names are longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:”Delhi”,2:”London”,3:”Paris”,4:”New York”,5:”Doha”}
The output should be:
LONDON
NEW YORK
Q4. 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”,”WB”,”TN”,”MH”,”MZ”,”DL”,”BH”,”RJ”,”HR”]
The following should get displayed
MP
MH
MZ
Q5. Write definition of a method/function EndingA(Names) to search and display those strings from the list of
Names, which are ending with ‘A’.
For example,
If the Names contain[“JAYA”,”KAREEM”,”TARUNA”,”LOVISH”]
The method / function should display
JAYA
TARUNA
Q6:- Write a function EOReplace() in Python, which accepts a list L of numbers Thereafter, it increments all even
numbers by 1 and decrements all odd numbers by 1.
Example:
If Sample Input data of the list is:
L=[10,20,30,40,35,55]
Output will be:
L=[11,21,31,41,34,54]
Q7. Write a user defined function in Python named showGrades(S) which takes the dictionary S as an argument.
The dictionary, S contains Name:[Eng,Math,Science] as key:value pairs.
The function displays the corresponding grade obtained by the students according to the following grading rules:
Average of Eng, Math, Science Grade
>=90 A
<90 but >=60 B
<60 C
For example: Consider the following dictionary
S={“AMIT” :[92,86,64], “NAGMA”:[65,42,43],”DAVID”:[92,90,88]}
AMIT – B
NAGMA – C
DAVID – A
Q8. Write a function, lenWords(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 “Come let us have some fun”,
The tuple will have(4,3,2,4,4,3)
Q9. Write a function search_replace() in Python which accepts a list L of numbers and a number to be searched. If
the number exists, it is replaced by 0 and if the number does not exist, an appropriate message is displayed.
Example:
L=[10,20,30,10,40]
Number to be searched = 10
List after replacement:
L=[0,20,30,0,40]
Q10. Write the definition of a function Sum3(L) in Python, which accepts a list L of integers and displays the sum of
all such integers from the list L which end with the digit 3. For example, if the list L is passed [123,10,13,15,23] Then
the function should display the sum of 123,13,23 i.e. 159 as follows: Sum of integers ending with digit 3=159
Or
Q11. 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]
Q12. Kritika was asked to accept a list of even numbers but she did not put the relevant condition while accepting
the list of numbers. You are required to write a user defined function oddtoeven(L) that accepts the List L as an
argument and convert all the odd numbers into even by multiplying them by 2.
Q13. Write a method in python to display the elements of list thrice if it is a number and display the element
terminated with ‘#’ if it is not a number.
For example, if the content of list is as follows:
ThisList=[‘41’,’DROND’,’GIRIRAJ’,’13’,’ZARA’]
414141
DROND#
GIRIRAJ#
131313
ZARA#
Q14. Write definition of a method OddSum(NUMBERS) to add those values from the list of NUMBERS, which are at
odd positions.
Q15. Write a user defined function in Python named Puzzle(W,N) which takes the argument W as an English word
and N as an integer and returns the string where every Nth alphabet of the word W is replaced with an
underscore(“_”).
For example : if W contains the word “TELEVISION” and N is 3, then the function should return the string
“TE_EV_SI_N”.
Likewise for the word “TELEVISION” if N is 4, then the function should return “TEL_VIS_ON”.
Q16. Write definition of a method Ending with 5(SCORES) to add all those values in the list of scores, which are
ending with 5 and display the sum.
For example,
If the SCORES contain [205,506,365,100,230,335]
The sum should be displayed as 905
Q17. Write definition of a method/function TenTimesEven(VALUES) to add and display the sum of ten times of the
even values present in the list of VALUES.
For example,
If the Nums contain [5,2,3,6,3,4]
The method/function should display Even Sum:120
(i.e. 2 x 10 + 6 x 10 + 4 x 10)
Q18. Write definition of a method/function FindOut(Names, HisName) to search for HisNAme string from a list
Names, and display the position of its presence.
For example :
If the Names contain[“Arun”,”Raj”,”Tarun”,”Kanika”] and HisName contains “Tarun”
The function should display
Tarun at 2
Q19. Write a method/function Swapper(Numbers) to swap the first half of the content of a list Numbers with second
half of the content of list Numbers and display the swapped values.
Note: Assuming that the list has even number of values in it
For example:
If the list Numbers contain
[35,67,89,23,12,45]
After swapping the list content should be displayed as
[23,12,45,35,67,89]
Q20. Write a function LShift(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 left.
Sample Input Data of the list Arr=[10,20,30,40,12,11],n=2
Output Arr=[30,40,12,11,10,20]