Python Functions Worksheet for Grade XII
Python Functions Worksheet for Grade XII
To find the indices of all non-zero elements in a list, a function named `INDEX_LIST` can be created. This function will iterate over the list, check each element to see if it is non-zero, and if so, add its index to a new list called `indexList`. For example, given a list L = [12, 4, 0, 11, 0, 56], the function would return [0, 1, 3, 5], as these are the indices of the non-zero elements .
The program should define user-specific functions for each shape, each accepting relevant parameters required for the calculation, such as side lengths or radii. For example, functions could calculate area and perimeter for squares and rectangles, perimeter and area for circles, and surface area for cylinders. After defining these functions, the program would import them as a module and use them to perform calculations on the given parameters, returning the results accordingly .
A function designed to check if two strings are of the same length should first receive the strings as arguments. It should then use the `len()` function to obtain the length of each string, and compare these lengths. If they are equal, the function returns `True`; otherwise, it returns `False` .
The function `lenWords` should take a string as its argument, split the string into a list of words using the `split()` method, and then calculate the length of each word using the `len()` function. The function should compile these lengths into a tuple and return it. For instance, for the input "Come let us have some fun", the function would return (4, 3, 2, 4, 4, 3).
The function to calculate compound interest should use the formula CI = P*(1 + r/n)^(nt), where P is the Principal Amount, r is the annual interest rate, n is the number of times the interest is compounded per year, and t is the time the money is invested or borrowed for, in years. These parameters should be passed to the function to compute and return the compound interest .
A function named `countNow` should accept the dictionary of place names as an argument. It will iterate over the dictionary values and check the length of each place name. If a name is longer than 5 characters, the function will convert it to uppercase and add it to the output. For example, given PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}, the output would be: LONDON NEW YORK .