SHARE
TWEET

Untitled

a guest Jun 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. In Python, function is a group of related statements that perform a specific task.
  2. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.
  3. Furthermore, it avoids repetition and makes code reusable.
  4. Syntax of Function
  5. def function_name(parameters):
  6.     """docstring"""
  7.     statement(s)
  8.  
  9. Above shown is a function definition which consists of following components.
  10. Keyword def marks the start of function header.
  11. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.
  12. Parameters (arguments) through which we pass values to a function. They are optional.
  13. A colon (:) to mark the end of function header.
  14. Optional documentation string (docstring) to describe what the function does.
  15. One or more valid python statements that make up the function body. Statements must have same indentation level (usually 4 spaces).
  16. An optional return statement to return a value from the function.
  17. def my_function(country = "Norway"):
  18. print("I am from " + country)
  19.  
  20. my_function("Sweden")
  21. my_function("India")
  22. my_function()
  23. my_function("Brazil")
  24.    
  25. def  add(a,b):
  26.         return  a+b
  27.     result=add(3,6)
  28.  
  29.  
  30. Lambda
  31. lambda operator or lambda function is used for creating small, one-time and anonymous function objects in Python.
  32.  
  33.  
  34. add=lambda a,b : a+b
  35. result=add(2,3)
  36.  
  37. if we check type of add, it is a function.
  38. Mostly lambda functions are passed as parameters to a function which expects a function objects as parameter like map, reduce, filter functions
  39. map
  40. Basic syntax
  41. map(function_object, iterable1, iterable2,...)
  42. map functions expects a function object and any number of iterables like list, dictionary, etc. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.
  43.  
  44. def mul1(a):
  45.     for i in a:
  46.     i= i*2
  47.   return a
  48.  
  49.  mul1([1,2,3,4])
  50.  
  51.  def multiply2(x):
  52.   return x * 2
  53.    
  54. map(multiply2, [1, 2, 3, 4])  # Output [2, 4, 6, 8]
  55.  
  56.  
  57. map(lambda x : x*2, [1, 2, 3, 4])
  58.  
  59. multiple iterable:
  60.  
  61. ist_a = [1, 2, 3]
  62. list_b = [10, 20, 30]
  63.  
  64. map(lambda x, y: x + y, list_a, list_b)
  65.  
  66. filter
  67.  
  68. Basic syntax
  69.  
  70. filter(function_object, iterable)
  71. filter function expects two arguments, function_object and an iterable. function_object returns a boolean value. function_object is called for each element of the iterable and filter returns only those element for which the function_object returns true.
  72.  
  73. Like map function, filter function also returns a list of element. Unlike map function filter function can only have one iterable as input.
  74.  
  75. Example:
  76.  
  77. Even number using filter function
  78.  
  79. a = [1, 2, 3, 4, 5, 6]
  80. filter(lambda x : x % 2 == 0, a)
  81.  
  82. ict_a = [{'name': 'python', 'points': 10}, {'name': 'java', 'points': 8}]
  83.  
  84. filter(lambda x : x['name'] == 'python', dict_a) # Output: [{'name': 'python', 'points': 10}]
  85. view raw
  86.  
  87.  
  88.  
  89. Scope and Lifetime of variables
  90. Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.
  91.  
  92. Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.
  93.  
  94. They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  95.  
  96. def my_func():
  97.     x = 10
  98.     print("Value inside function:",x)
  99.  
  100. x = 20
  101. my_func()
  102. print("Value outside function:",x)
  103.  
  104.  
  105.  
  106.  
  107. def my_func():
  108.    
  109.    
  110.     print("Value inside function:",x)
  111.  
  112. x = 20
  113. my_func()
  114. print("Value outside function:",x)
  115.  
  116. def my_func():
  117.     global x
  118.     x=10
  119.     print("Value inside function:",x)
  120.  
  121. x = 20
  122. my_func()
  123. print("Value outside function:",x)
  124.  
  125. def my_func():
  126.     x = 10
  127.     print("Value inside function:",x)
  128.  
  129.  
  130. my_func()
  131. print("Value outside function:",x)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top