def table(a): b = 1 while b <= 5: print ("%d * %d = %d" %(a,b,b*a)) b += 1 table (5) Output: 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
def average (a,b,c): d = (a + b + c ) / 3 return d result = average (10,20,30) print (result) Output: 20.0
In the program mentioned above, variables a,b,c and d are local variables of the function average. Scope of those variables are with in the function average , so if we try to use them outside the function it will throw an error.
def average (a,b,c): d = (a + b + c ) / 3 return d average (10,20,30) print (d) Output: print (d) NameError: name 'd' is not defined
Variables defined outside the function are treated as global variables, and will be available in all functions without global command.
a = 10 def average (b,c): print ("a is",a) d = ( a + b + c ) / 3 return d print (average (20,30)) Output: 20.0
Note:If we assign value to a variable using “=” inside a function , that variable will become a local variable (even if the same name is used in the global scope).
a = 10 def average (b,c): a = 40 d = ( a + b + c ) / 3 print ("average is", d) print ("local variable ‘a’ is", a) average (20,30) print ("global variable ‘a’ is still", a) Output: average is 30.0 local variable 'a' is 40 global variable 'a' is still 10
a = 10 def average (b,c): global a print ("variable 'a' inside function is ", a) d = ( a + b + c ) / 3 print ("average is",d) a = 40 #this will change the value of 'a' locally and globaly average (20,30) print ("global variable 'a' is now", Output variable 'a' inside function is 10 average is 20.0 global variable 'a' is now 40
If a mutable argument is used, modification done inside the function will be visible outside also.
In the function defined below, 'x' is pointing to the object 'a' . If we modify 'x' , that change will reflect in 'a' also
def add_to_list (x,y): x.append(y) a =  add_to_list (a,10) add_to_list (a,20) add_to_list (a,30) print (a) Output: [10, 20, 30]
Note: if we use "=" to assign some value in 'x', 'x' will become a local variable and it wont be pointing to 'a'
def average (a,b,c=30): print ((a+b+c)/3) average (10,20) average (10,20,60) Output: 20.0 30.0
def repeat(string, count): a = 1 while a < count: print (string) a +=1 repeat (count = 3, string = "python") Output: python python python
def sum (a,b,*c): result = a + b for tmp in c: result += tmp print (result) sum (10,20) sum (10,20,30) sum (10,20,30,40) Output: 30 60 100
Lambda functions are similar to a normal functions , but they doesn’t have a name. So they are also called as anonymous functions.
Lambda functions has arguments similar to normal functions and they can evaluate an expression with these arguments and return the result. They are created using “lambda” keyword.
Note: Only one expression is allowed with lambda functions.
>>> cube = lambda x : x ** 3 >>> cube (5) 125 >>> even = lambda x : True if x % 2 == 0 else False >>> even(10) True >>> sum = lambda x,y : x + y >>> sum (10,5) 15
Lambda functions will be useful whenever a function object is required. For example , builtin functions like filter(), map() and reduce() an make use of Lambda function as they require a function object as an argument
>>> a = [1,2,3,4,5] >>> cube = lambda x : x ** 3 >>> print ( list (map (cube ,a) ) ) [1, 8, 27, 64, 125] >>> >>> a = [1,2,3,4,5] >>> even = lambda x : True if x % 2 == 0 else False >>> print (list(filter (even,a))) [2, 4] >>> from functools import reduce >>> a = [1,2,3,4,5] >>> sum = lambda x,y : x + y >>> reduce (sum,a) 15
1. Create function to check whether given number is prime or not. Use this function with list of numbers to print only the prime numbers from a list.
2. Write a python program to find the factorial of a number using recursive function call
3.1 Create a function to find the smallest element of a given list
3.2 Create another function to return the position of a given element in a list
3.3 Create one more function to delete the element in a given position
3.4 Using the above three functions find, print and remove the smallest number in a list.
Repeat this process till the list is empty to get numbers in sorted order.
Next chapter: Python training : Classes and Objects Part 1
Contact us and we'll get back to you within 24 hours.
#11, 8th cross, Indiranagar 1st stage, Bangalore, India
You can connect with us on Whatsapp 8884 884 844