Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1.  
  2.  
  3. # Python has variables!
  4. foo = 1
  5. bar = 2
  6.  
  7. print(foo + bar)
  8.  
  9. # and functions
  10. def my_sum(a, b, offset=0):
  11. return a + b + offset
  12.  
  13. print(my_sum(9, 1))
  14.  
  15. # the offset is a default argument, but you can use it
  16. print(my_sum(1, 2, 5))
  17.  
  18. # funny thing? your function may return functions
  19. def offseted_sum_factory(offset):
  20. def fcn(a, b):
  21. return my_sum(a, b, offset)
  22.  
  23. return fcn
  24.  
  25. by_ten = offseted_sum_factory(10)
  26. by_five = offseted_sum_factory(5)
  27.  
  28. print(by_ten(1,1))
  29. print(by_five(1,1))
  30.  
  31. # take a time to think about it.... maybe you find this useless, but google for closures
  32.  
  33. # and this functions are first class, so:
  34. pointer = my_sum
  35. print(pointer(3,2))
  36.  
  37. # Pythons has lists
  38. foo = [1, 2, 3, "banana"]
  39. foo.append("mango")
  40.  
  41. print(foo)
  42.  
  43. print("Here goes the" + foo.pop())
  44.  
  45. print(foo)
  46.  
  47. # Python has tuples, which are immutable
  48. foo = (1, 2, 3, 4)
  49.  
  50. # You may loop lists and tuples using
  51. for item in foo:
  52. print(item)
  53.  
  54. # well.... actually it's the only for statement python knows, so if you wanna count
  55. for number in range(0, 100):
  56. print(number)
  57.  
  58. # which replaces your good friend for(int i=0, i<100, i++)
  59.  
  60. # You may use the tuple() and list() to convert them
  61. # which is nice, but useless...
  62. foo = [1, 2, 3]
  63. bar = tuple(foo)
  64. print(bar)
  65. print(list(bar))
  66.  
  67. # Both support O(log(N)) search
  68. print(2 in bar)
  69. print(5 in foo)
  70.  
  71. # Both support slicing using the [start:offset:pace/direction] method
  72. print(foo[0])
  73. print(foo[1])
  74. print(foo[1:3])
  75. print(foo[1::-1])
  76.  
  77. # Sometimes you need a hash map, Python calls it a dict
  78. foo = {1: "one", 2: "two", "three": 3}
  79.  
  80. # Keys must be hashable, values can be anything
  81. # remember: Dicts arent ordered
  82.  
  83. # looping
  84.  
  85. for key, value in foo.items():
  86. print([key, value])
  87.  
  88. for key in foo.keys():
  89. print(key)
  90.  
  91. for value in foo.values():
  92. print(value)
  93.  
  94. # Access
  95. print(foo[2])
  96. print(foo['three'])
  97.  
  98. # you may want to use the get method to avoid a KeyError if you aren't sure it exists
  99. value = foo.get(4)
  100. print(value)
  101.  
  102. value = foo.get(5, "Bananas!")
  103. # is way better than using the ternary
  104. value = "Bananas" if foo.get(5) is None else foo[5]
  105.  
  106. # which will go bad if foo[5] is really None
  107. # and don't even think about "correcting" it like:
  108.  
  109. try:
  110. value = foo[5]
  111. except KeyError:
  112. value = "Bananas"
  113.  
  114. # even if it works properly
  115.  
  116. # well..... guess you found out that Python has exceptions
  117.  
  118. try:
  119. foo = 1 + 2
  120. raise RuntimeError
  121.  
  122. except RuntimeError:
  123. print("Foo")
  124.  
  125. finally:
  126. print("Remember, the finally clause is always with you")
  127.  
  128. # oh.... and what about that RuntimeError thing?
  129. # that's a subclass of Exception
  130.  
  131. class MyException(Exception):
  132. pass
  133.  
  134. # so, yeah, Python has classes.... and inheritance (multiple, diamond pattern inheritance)
  135. # static methods, class methods, instance methods, properties and so on....
  136. # it doesn't have visibility controls like private and protected. We use __ and _ prefix to indicate that.
  137.  
  138. class Car():
  139.  
  140. class_var = "Foobar"
  141.  
  142. @classmethod
  143. def say_it(cls):
  144. print cls.class_var
  145.  
  146. # Constructor
  147. def __init__(self):
  148. self._on = False
  149.  
  150. def toggle_it(self):
  151. self._on = not self._on
  152.  
  153. def drive(self, to_where):
  154. print("Drove to:" + to_where)
  155.  
  156. @property
  157. def is_on(self):
  158. return self._on
  159.  
  160. @is_on.setter
  161. def is_on(self, value):
  162. assert (value in (False, True))
  163. self._on = value
  164.  
  165.  
  166. # The class knows some tricks
  167. print(Car.class_var)
  168. Car.say_it()
  169.  
  170. # your car works as expected
  171. ford = Car() # there's no new and the self parameter is supplied by the language
  172. ford.say_it()
  173. print(ford.is_on)
  174. ford.toggle_it()
  175. print(ford.is_on)
  176. ford.is_on = False
  177.  
  178. # Let's see....
  179. Car.class_var = "Alice"
  180. Car.say_it()
  181.  
  182. print(ford.class_var)
  183. # or even
  184. ford.say_it()
  185.  
  186. # and if you get a fiat
  187. fiat = Car()
  188. fiat.say_it()
  189.  
  190. # So all instances share the class variable!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement