Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. # DEFINING FUNCTIONS
  2.  
  3. # define a function with no arguments and no return values
  4. ```python
  5. def print_text():
  6. print 'this is text'
  7. ```
  8.  
  9. ## call the function
  10. ```python
  11. print_text()
  12. ```
  13.  
  14. ## define a function with one argument and no return values
  15. ```python
  16. def print_this(x):
  17. print x
  18. ```
  19.  
  20. ## call the function
  21. ```python
  22. print_this(3) # prints 3
  23. n = print_this(3) # prints 3, but doesn't assign 3 to n
  24. # because the function has no return statement
  25. ```
  26.  
  27. ## define a function with one argument and one return value
  28. ```python
  29. def square_this(x):
  30. return x**2
  31. ```
  32.  
  33. ## include an optional docstring to describe the effect of a function
  34. ```python
  35. def square_this(x):
  36. """Return the square of a number."""
  37. return x**2
  38. ```
  39.  
  40. ## call the function
  41. ```python
  42. square_this(3) # prints 9
  43. var = square_this(3) # assigns 9 to var, but does not print 9
  44. ```
  45.  
  46. ## define a function with two 'positional arguments' (no default values) and
  47. one 'keyword argument' (has a default value)
  48. ```python
  49. def calc(a, b, op='add'):
  50. if op == 'add':
  51. return a + b
  52. elif op == 'sub':
  53. return a - b
  54. else:
  55. print 'valid operations are add and sub'
  56. ```
  57.  
  58. ## call the function
  59. ```python
  60. calc(10, 4, op='add') # returns 14
  61. calc(10, 4, 'add') # also returns 14: unnamed arguments are inferred by position
  62. calc(10, 4) # also returns 14: default for 'op' is 'add'
  63. calc(10, 4, 'sub') # returns 6
  64. calc(10, 4, 'div') # prints 'valid operations are add and sub'
  65. ```
  66.  
  67. # use 'pass' as a placeholder if you haven't written the function body
  68. ```python
  69. def stub():
  70. pass
  71. ```
  72.  
  73. ## return two values from a single function
  74. ```python
  75. def min_max(nums):
  76. return min(nums), max(nums)
  77. ```
  78.  
  79. ## return values can be assigned to a single variable as a tuple
  80. ```python
  81. nums = [1, 2, 3]
  82. min_max_num = min_max(nums) # min_max_num = (1, 3)
  83. ```
  84.  
  85. ## return values can be assigned into multiple variables using tuple unpacking
  86. ```python
  87. min_num, max_num = min_max(nums) # min_num = 1, max_num = 3
  88. ```
  89.  
  90.  
  91. # ANONYMOUS (LAMBDA) FUNCTIONS
  92. primarily used to temporarily define a function for use by another function
  93.  
  94. ### define a function the "usual" way
  95. ```python
  96. def squared(x):
  97. return x**2
  98. ```
  99.  
  100. ### define an identical function using lambda
  101. ```python
  102. squared = lambda x: x**2
  103. ```
  104.  
  105. ### sort a list of strings by the last letter (without using lambda)
  106. ```python
  107. simpsons = ['homer', 'marge', 'bart']
  108. def last_letter(word):
  109. return word[-1]
  110. sorted(simpsons, key=last_letter)
  111. ```
  112.  
  113. ### sort a list of strings by the last letter (using lambda)
  114. ```python
  115. sorted(simpsons, key=lambda word: word[-1])
  116. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement