find the error(if any) in the following code and write the corrected code and underline it: Def sum(a,b): returna+b print "the sum=" sum(7,-1) sol def sum(a,b): #first letter of def keyword is written capital return a+b # there should be gap between return keyword and a+b print("the sum=",sum(7,-1)) #missing () in the print function and , is also missing after the text #-------------------------------- Consider the following function calls with respect to the function definition. Identify which of these will cause an error and why? def calculate(a,b=5,c=10): return a*b-c i) calculate(12,3)#no error ii) calculate(c=50,35)#we should have to specify the value to all the parameters .corrected calling is calculate(c=50,a=35,b=12) iii) calculate(20, b=7, a=15)#name c is missing . corrected code: calculate(c=20, b=7, a=15) iv) calculate(x=10,b=12)#name x is not mentioned in the function parameter . corrected code: calculate(c=10, b=12, a=15) #-------------------------------- find and write the output of the following python code: (a) def change(p,q=20): p=p+q #40+100=140 q=p-q #140-100=40 print(p,'#',q)#140 # 40 return(p) r=150 s=100 r=40 r=change(r,s) print(r,'#',s) s=change(s) (b) def callme(n1=1,n2=2): n1=n1*n2 n2+=2 print(n1,n2) callme() callme(2,1) callme(3) (c) def show(x,y=2): print(ord(x)+y) show('A') show('B',3) (d) def upgrade(a,b=2): a=a+b print(a+b) i,j=10,20 upgrade(i,5) upgrade(i) (e) def func(a,b=5,c=10): print(“a:”,a,” b:”,b, “ c:”,c) func(3,7) func(25,c=24) func(c=50, a=100) (f) x=1 def cg(): global x x=x+1 cg() print(x) (g) def wish(message, num=1): print(message * num) wish(‘Good’,2) wish(“Morning”) #-------------------------------- Q4. Identify the type one or more types of arguments in the following codes: a) def sum(a=4,b=6): return a+b print (sum( ))#default b) def sum(a=1,b): return a+b print(sum(b=20, a=5 ))#keyword c) def sum(*n): for i in n: total+=I return total print (sum(4,3,2,1,7,8,9))#positional d) def sum(a=1,b): return a+b print(sum(10,20))#positional #-------------------------------- What do you understand by local and global scope of variables?HOw can you access a global variable inside the function,if function has a variable with same name. ans- those variable which r defined inside the function or in the parameter-local variable those variable which r defined outside the function are called global variable x=1 def cg(): global x x=3 x=x+1 print(x) cg() print(x) #-------------------------------- consider the following function headers. identify the correct statement:- (i)def correct(a=1,b=2,c): (ii) def correct(a=1,b,c=3): (iii) def correct(a=1,b=2,c=3): #correct (iv) def correct(a=1,b,c): #-------------------------------- Create a function in Python to calculate and return Area of rectangle when user enters length and breadth. sol def arearect(l,b): return l*b a=int(input("enter length")) b=int(input("enter breadth")) arearect(a,b) #------------------------------- Give the basic structure of user defined function. sol def funtionname(parameters name): statement of the function function_calling(arguments) #-------------------------------- Write the features of a module. Ans: - 1) Modules can be reused in other programmes 2) It is independent group of code and data #-------------------------------- What is module in Python? Ans: - A module is a file with .py extension that contains variables, class definitions, statements and functions related to a particular task. #-------------------------------- Create a package Arithmetic Operations(named AO) contain sum, product and difference of two numbers and use it in your main programme. Ans: - #-------------------------------- What is Libraries in Python and How many types of main library use in Python? Ans: -A Library is a collection of modules that together caters to specific types of needs or applications. Like NumPy library of Python caters to scientific computing needs. #-------------------------------- How we can import library in Python program? Ans: - We can import a library in python program with the help of import command. Eg: - import random import mysql.connector as ms #-------------------------------- What is scope of a variable? Ans: - Part of program within which a name is legal and accessible is called scope of the variable. #-------------------------------- Explain two types of variable scope with example. Ans – Global Scope – A name declared in top level segment of a program is said to have global scope and it is accessible inside whole programme. Local Scope – A name declared in a function body is said to have local scope and it can be used only within this function. A=0 # global scope def fun1(): B=0 # local scope print(B) print(A) #-------------------------------- Write a small python function that receive two numbers and return their sum, product, difference and multiplication and modulo division. def ADD(X,Y): return (X+Y) def PRODUCT(X,Y): return(X*Y) def DIFFERENCE(X,Y): return(X-Y) #-------------------------------- Q what is a parameter A parameter is data that you ask to be imputed into a function. For example: def fun(param): In the code above,the function "fun" asks for a parameter called "param". When we call the function like so: fun(sample_param) the data that we inputed "sample_param" is an argument. #-------------------------------- Write a user defined function findname(name) where name is an argument in Python to delete phone number from a dictionary phonebook on the basis of the name, where name is the key. sol def findname(d): n=input("enter key which u want to delete") d.pop(n) print("dictionary after deletion",d) d={} n=int(input("enter how many elements u want to enter")) for i in range(n): na=input("enter name") p=int(input("enter phone no")) d[na]=p print(d) findname(d) #-------------------------------- 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 def MSEARCH(STATES): for i in STATES: if(i[0]=='M'): print(i) L=["MP","UP","WB","TN","MH","MZ","DL","BH","RJ","HR"] MSEARCH(L) #------------------------------- State the use of global keyword. Give an example to illustrate its use. Q7. Write the types of user defined functions Q8. Write a program using user defined function to : a) Check if a number entered by the user to check whether a number is in a given range or not. Range may be of programmer’s choice. b) Calculate and display average of all the elements in a user defined tuple containing numbers. c) Calculate sum of any 10 numbers using variable argument. #------------------------------------------------------------------------ Kritika was asked to accept a list of even numbers but she did not put the relevant condition while accepting the list. Write a user-defined function oddtoeven(L) that accepts the List L as an argument and converts all the odd numbers into even by multiplying them by 2. #-------------------------------- (a)Write a function that receives two numbers and generates a random number from that range and prints it. import random def fun(a,b): print(random.randint(a,b)) fun(3,6) (b) Write a function that takes a positive integer and returns the ones position digit of the integer. E.g. if the integer is 432, then the function should return 2. def fun(a): r=a%10 print(r) (c) Write a program having a function that takes a number as argument and calculates cube for it. The function does not return a value. If there is no return value passed to the function in function call, the function should calculate cube of 2. def fun(n=2): print(n**3) fun(3) fun() #-------------------------------- State the difference between: a) Formal parameters and actual parameters b) Local variables and global variables c) Immutable objects and mutable objects ans (c)Means a mutable object can be changed after it is created, and an immutable object can’t. Mutable objects: list, dict, set, byte array Immutable objects: int, float, complex, string, tuple, frozen set ,bytes