0% found this document useful (0 votes)
61 views

E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)

The document discusses functions in Python. It provides examples of defining functions to perform various tasks like finding the sum of odd numbers in a range, displaying the Fibonacci series, checking if a number is Armstrong, reversing a string, modifying a list based on certain conditions, returning a tuple of Unicode values of characters in a string, adding/removing keys/values from a dictionary based on conditions, performing binary search on a sorted list, calculating perimeter and area of a scalene triangle given sides, displaying adjacent points to a given point, converting between inches and centimeters, calculating area/surface area/volume for geometric shapes, finding the string with highest vowels, modifying numbers based on divisibility conditions, checking if a year is a leap

Uploaded by

Tanisha Jena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

E.G. - If Input Is (12, 54, 49, 86, 23, 36) Then Modified List Is (32, 18, 16, 106, 43, 12)

The document discusses functions in Python. It provides examples of defining functions to perform various tasks like finding the sum of odd numbers in a range, displaying the Fibonacci series, checking if a number is Armstrong, reversing a string, modifying a list based on certain conditions, returning a tuple of Unicode values of characters in a string, adding/removing keys/values from a dictionary based on conditions, performing binary search on a sorted list, calculating perimeter and area of a scalene triangle given sides, displaying adjacent points to a given point, converting between inches and centimeters, calculating area/surface area/volume for geometric shapes, finding the string with highest vowels, modifying numbers based on divisibility conditions, checking if a year is a leap

Uploaded by

Tanisha Jena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FUNCTION

A) Write the following programs with the help of user-defined functions only:

1. Find the sum of all the odd integers within a given range. Use def sum_odd(min,max)

2. Display the Fibonacci series up to a given number of terms. Use def fib_series(n)

3. Check whether a number is Amstrong or not. Return True/False from the function.

4. Find the reverse of a given string without using any built-in function. def rev(String)

5. Modify the given list of integers in the following manner:


If an element is within the range of 30 to 60 (both values inclusive) then replace the
element with its one third (use integer division), add 20 with rest of the elements.
e.g. - If input is [12, 54, 49, 86, 23, 36] then modified list is [32, 18, 16, 106, 43, 12]

6. Input a string and return a tuple of integers which are actually numeric Unicode values of
each characters.
e.g. - If input is ‘Welcome’ then output is (87, 101, 108, 99, 111, 109, 101)

7. Consider the dictionary dic_Food = { 'Breakfast' : 'Milk', 'Lunch' : 'Rice' , 'Dinner' : 'Bread' }
i. Display the keys only of dic_Food after adding two elements {'M_Snack' : 'Vada’} and
{'E_Snack' : 'Pizza'}. Use def key_add()
ii. Display the values only of dic_Food after deleting elements {'Lunch' : 'Rice'}.
Use def value_remove()

8. Write a function to input a list of real numbers in ascending order of values. def input_list()
Write another function to a search whether a given value is present in the list using binary
search technique. def binary_search(value)

9. Input 3 sides of a scalene triangle with one side having default value 5 unit. Define individual
functions for calculating perimeter and area of that triangle.

10. Input a point from user i.e. a tuple of 2 integers (X, Y) and display all the adjacent points:
(X+1, Y), (X-1, Y), (X, Y+1), (X, Y-1), (X+1, Y+1), (X-1, Y+1), (X+1, Y-1), (X-1, Y-1) inside a
single function.

B) Write functions for the following problems:

1.Write a function inch_to_cm() which accepts the value in inch and return the value in
centimeter, where 1 inch = 2.54 cm.

2. Input a radius r and height h from the user where both the values are positive real numbers
and define function for each of the following purpose:
i. Calculate area of the circle of radius r.
ii. Calculate total surface area of the cylinder of radius r and height h.
iii. Calculate volume of a sphere with radius r.
3. Define a function Max_Vowel() which accepts a tuple of strings as argument and return the
string with highest number of vowels in it. If more than one string have same highest count then
return the last occurrence string only.

4. Input 3 numbers as parameters and return twice of the original number if it is divisible by 4
or 9, otherwise no change.
e.g. If x=20, y=15 and z = 81 then Twice_4_9(x ,y, z) will return 40, 15, 162

5. Input a year in the function Check_Leap_Year() and return Boolean value depending on
whether that year is leap year.

6. Input length, breadth of a rectangular cuboid where default value of breadth is 2 in the
function Calc_area(length, breadth) which returns its surface area. Define height=3 as global
variable.

7. Input two real numbers in the function Display_random(). Generate 5 random real numbers
within this range and store in the list R. Display the median value of the list.

8. Input a tuple of strings and display 2 strings from the tuple randomly.

C) Re-write the following program code after rectifying errors and underline the corrections:

1. sum = 0
def series x, n
for i range[n]:
sum = sum + power(x,i)
return n : sum
sum(2,5) = a : b
print("Sum of term", a, " = ", b)

2. func series() = Tuple


while i in Tuple:
if i.length() > 2:
print i
series("I","am","intelligent","student","in","class")
series['You', 'are', 'good']
series("Horrible".split(r))

3. gb = 2
def func1(m=10):
gb = m
print(gb)
def func2(m=10, n):
global gb = m
print(gb+n)
func1(5)
func2(15)
func1(5, 15)
func2(5, 15)
print(gb)

4. sum(a = 10, b = a)
if a > b:
a-b+2
else return a+b-2
sum[10]
sum(20,30,40)

D) Find output of the following programs:

1. Find output of STATEMENT 1 and STATEMENT 2. Define STATEMENT 3 to call expr()


so that default value of b and c are used inside function.
def expr(a, b=5, c=1):
return 3 * a + b - c
print(expr(7, 4, 2)) # STATEMENT 1
print(expr(9, 3)) # STATEMENT 2
____________ # STATEMENT 3

2. If the user gives input as hallucination then find the output displayed by the following code:
def crop(text):
return text[:3:1] + text[-2:7:-1]
def hlen(text):
return len(text)//2
tx = input("Enter a text : ")
t1 = crop(tx)
print(t1)
t2 = tx[::-2]
print(t2)
p = hlen(tx)
print(tx[:p:3]*3)

3. Display the content of list L after executing the following code:


def rep_ds(X):
for i in range(3, len(X), 2):
X[i] = 10 - X[i]
L = [7, 19, 21, 45, 5, 63, 34]
rep_ds(L)
print(L)
If a L is replaced as L = (69, 20, 45, 10, 35) then what will be the result? Give reason supporting
your answer.

4. def toggle_tuple(L):
T, Temp = list(L), []
for i in range(0, len(T), 2):
Temp.extend([L[i+1], L[i]])
L=tuple(Temp)
print(L)
Tp = (9, 12, 47, 52, 73, 10)
toggle_tuple(Tp)
print(Tp)
Lst = list(Tp[::-1])
toggle_tuple(Lst)
print(Lst)

5. def bin_search(List, low, up, value):


while up >= low:
mid = (low + up ) // 2
if List[mid] == value:
print("Found at position : ", mid)
return
elif List[mid] > value:
print(' => ', mid, ':', List[mid], end = ' ')
up = mid - 1
else:
print(' => ', mid, ':', List[mid], end = ' ')
low = mid + 1
print("Not Found")

T = (5, 10, 15, 20, 25, 30, 35, 40)


bin_search(T,0,len(T)-1,37)

6. If value of R is 10 then find the output of the function func_sum(R):


def func_prod(x,n):
x=x+n
n=n*2
return x*n

def func_sum(R):
sum = 0
for i in range(1, R, 3):
sum = sum%5 + func_prod(i,2)
print(i, sum, sep = '#')

7. DIGIT = 10
def mod_num(N):
global DIGIT
m=4
DIGIT = N//m + 1
print("DIGIT", DIGIT)
DIGIT -= 2
def ch_num():
DIGIT = 7 #STATEMENT 1
print("TWICE DIGIT", DIGIT*2)
DIGIT = DIGIT - 3
ch_num()
mod_num(23)
DIGIT += 5
print("Final DIGIT", DIGIT)
Write output of the code above if #STATEMENT 1 is replaced with ‘global DIGIT’ inch_num().
8. def dic_ch(D):
if 'Name' in D:
D['Name'] = D['Name'] + ' ' + "KV DRDO"
D['Marks'] = (90, 79, 87)
if D['Fee'] >= 1000:
D['Fee'] += round(D['Fee']*0.1)
if 'City' in D:
del D['City']
return
Dict1 = { 'Roll' : 1, 'Name' : 'Sonam Dutta', 'Fee' : 500, 'City' : 'Bangalore'}
Dict2 = { 'Roll' : 2, 'Fee' : 1500, 'Marks' : 100}
dic_ch(Dict1)
dic_ch(Dict2)
print(Dict1)
print(Dict2)

E) Answer the following questions:

1. Write down the purpose of using functions in a program.


2. What is the difference between parameters and arguments in python? Explain with suitable
example.
3. Explain the concept of Calling function and called function using appropriate example.
4. What is the significance of __main__ ?
5. Briefly explain the working principal of positional argument and keyword argument with
suitable examples for each.
6. Write down the advantage of using default parameters in a function.
7. Is the following function definition correct? Give reason behind your answer.
def sum_odd(fst = 2, sec, trd = 2.65, frt):
return (fst+trd)
8. Define global variable and local variable. Give suitable example.
9. Explain the LEGB scope rule.
10. Identify the global variables and local variables from the following code:
max = 56
def find_high(x):
y=2
return 3*x+2
z = 10
p = max - find_high(z)
print("The outcome is = ", p)
11. Is the following function definition correct? Give reason behind your answer.
def area(a, b=a):
print("Area is = ", a*b)
x = area(10)
print(x)
12. What is the difference between passing a list and a tuple to a function as parameters?
______

You might also like