Guest User

Untitled

a guest
Jul 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. print("***** abs *****")
  2. x = -3
  3. y = abs(x)
  4.  
  5. # abs: Absolute value of a numeric type
  6. print(y)
  7.  
  8. # ---------------- OUTPUT ----------------
  9. # 3
  10.  
  11.  
  12. print("***** multiply *****")
  13. # True will be repeated 5 times
  14. x = [True] * 5
  15. print(x)
  16.  
  17. # to check all items are True
  18. print(all(x))
  19.  
  20. # append False to array and check again
  21. x.append(False)
  22. print(x)
  23. print(all(x))
  24.  
  25. # check if any is True
  26. print(any(x))
  27.  
  28. # ---------------- OUTPUT ----------------
  29. # ***** multiply *****
  30. # [True, True, True, True, True]
  31. # True
  32. # [True, True, True, True, True, False]
  33. # False
  34. # True
  35.  
  36.  
  37. print("***** enumerate *****")
  38. # used to loop over a list with a counter
  39.  
  40. for pos, letter in enumerate("python"):
  41. print(str(pos) + "=>" + letter)
  42.  
  43. # ---------------- OUTPUT ----------------
  44. # ***** enumerate *****
  45. # 0=>p
  46. # 1=>y
  47. # 2=>t
  48. # 3=>h
  49. # 4=>o
  50. # 5=>n
  51.  
  52.  
  53. print("***** eval *****")
  54. # eval interprets a string as code => allows to run a string as python code
  55. x = 1
  56. print(eval("x + 3"))
  57.  
  58. # ---------------- OUTPUT ----------------
  59. # ***** eval *****
  60. # 4
  61.  
  62.  
  63. print("***** filter array *****")
  64. ages = [6, 8, 12, 19, 7, 39, 24, 65]
  65.  
  66. def older(x):
  67. return x > 19
  68.  
  69. for item in filter(older, ages):
  70. print(item)
  71.  
  72. # ---------------- OUTPUT ----------------
  73. # ***** filter array *****
  74. # 39
  75. # 24
  76. # 65
  77.  
  78.  
  79. print("***** map *****")
  80. # use map to apply function to each item in a list
  81.  
  82. def square(x):
  83. return x * x
  84.  
  85. for item in map(square, range(1,10)):
  86. print("square %d" % item)
  87.  
  88. # ---------------- OUTPUT ----------------
  89. # ***** map *****
  90. # square 1
  91. # square 4
  92. # square 9
  93. # square 16
  94. # square 25
  95. # square 36
  96. # square 49
  97. # square 64
  98. # square 81
  99.  
  100.  
  101. print("***** pow *****")
  102. # power of a number
  103. x = 3
  104. y = 4
  105. print(pow(x, y))
  106.  
  107. # ---------------- OUTPUT ----------------
  108. # ***** pow *****
  109. # 81
  110.  
  111.  
  112. print("***** zip *****")
  113. # to create tuples from 2 arrays
  114.  
  115. x = [1, 2, 3, 4]
  116. y = ["a", "b", "c", "d"]
  117. z = zip(x, y)
  118.  
  119. for a in z:
  120. print(a)
  121.  
  122. # combine zip and dict to create dictionary from 2 arrays
  123. d = dict(zip(x,y))
  124. print(d)
  125.  
  126. # ---------------- OUTPUT ----------------
  127. # ***** zip *****
  128. # (1, 'a')
  129. # (2, 'b')
  130. # (3, 'c')
  131. # (4, 'd')
  132. # {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
Add Comment
Please, Sign In to add comment