Advertisement
here2share

# exec_built-in_func_demo.py ^ 2018.08

May 30th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. # exec_built-in_func_demo.py -- most likely will be updated
  2.  
  3. # All listed in py2x probably still works in Python 3.x
  4.  
  5. py2x = '''
  6. # abs(var)
  7.  
  8. print(abs(-23))
  9.  
  10. print(abs(3 + 5j))
  11.  
  12. # all(var)
  13.  
  14. print(all([]))
  15.  
  16. print(all([1, 2, 3, 4, 5]))
  17.  
  18. print(all("string"))
  19.  
  20. print(all([0, 0, 1]))
  21.  
  22. print(all([0, 0, 0]))
  23.  
  24. print(all([1, 1, 1]))
  25.  
  26. # any(var)
  27.  
  28. print(any([]))
  29.  
  30. print(any([1, 2, 3, 4, 5]))
  31.  
  32. print(any("string"))
  33.  
  34. print(any([0, 0, 1]))
  35.  
  36. print(any([0, 0, 0]))
  37.  
  38. print(any([1, 1, 1]))
  39.  
  40. # bin(int)
  41.  
  42. print(bin(8))
  43.  
  44. print(bin(256))
  45.  
  46. # bool([x])
  47.  
  48. print(bool())
  49.  
  50. print(bool(0))
  51.  
  52. print(bool(1))
  53.  
  54. print(bool("True"))
  55.  
  56. # bytearray([source, [encoding, [errors]]])
  57.  
  58. print(bytearray())
  59.  
  60. print(bytearray("string"))
  61.  
  62. print(bytearray(3))
  63.  
  64. # bytes([source, [encoding, [errors]]])
  65.  
  66. print(bytes())
  67.  
  68. print(bytes(3))
  69.  
  70. # callable(obj)
  71.  
  72. print(callable(1))
  73.  
  74. print(callable("string"))
  75.  
  76. # chr(i)
  77.  
  78. print(chr(34))
  79.  
  80. # complex([real, [imagine]])
  81.  
  82. print(complex())
  83.  
  84. print(complex(1))
  85.  
  86. print(complex(2, 3))
  87.  
  88. print(complex('1+4j'))
  89.  
  90. print(complex('5j'))
  91.  
  92. # dir([obj])
  93.  
  94. print(dir())
  95.  
  96. print(dir(complex))
  97.  
  98. # divmod(a, b)
  99.  
  100. print(divmod(4, 2))
  101.  
  102. print(divmod(7, 3))
  103.  
  104. # enumerate(iterable, start = 0)
  105.  
  106. print(enumerate([1, 2, 3, 4, 5, 6]))
  107.  
  108. print(enumerate(["one", "two", "three"], 1))
  109.  
  110. # eval(expr)
  111.  
  112. print(eval("1+6/3"))
  113.  
  114. print(eval("1 & 2 ^ 3 - 4"))
  115.  
  116. # filter(function, iterable)
  117.  
  118. filtered = filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])
  119. for item in filtered:
  120.   print("[", item, "]")
  121.  
  122. # float([x])
  123.  
  124. print(float())
  125.  
  126. print(float('inf'))
  127.  
  128. print(float("-inf"))
  129.  
  130. print(float("      nan"))
  131.  
  132. print(float("   213.434235235235235    "))
  133.  
  134. # format(obj, [spec])
  135.  
  136. print(format(1 + 2j))
  137.  
  138. print("{0} + {1} != {2} + {3}".format(1, 2, 3, 4))
  139.  
  140. # getattr(obj, attr, [default])
  141.  
  142. print(getattr([1, 2, 3, 4], "len", 0))
  143.  
  144. print(getattr([1, 2, 3, 4], "__init__"))
  145.  
  146. # globals()
  147.  
  148. print(globals())
  149.  
  150. # hasattr(obj, name)
  151.  
  152. print(hasattr("string", "__init__"))
  153.  
  154. print(hasattr("string", "size"))
  155.  
  156. # hash(obj)
  157.  
  158. print(hash("string"))
  159.  
  160. print(hash(10))
  161.  
  162. print(hash(12345678910111213141516171819))
  163.  
  164. # help([obj])
  165.  
  166. import os
  167.  
  168. help(os.system)
  169.  
  170. help(complex)
  171.  
  172. # hex(int)
  173.  
  174. print(hex(100))
  175.  
  176. print(hex(256))
  177.  
  178. print(hex(12345678910111213141516171819))
  179.  
  180. # id(obj)
  181.  
  182. print(id("str"))
  183.  
  184. print(id(0))
  185.  
  186. # input([prompt])
  187.  
  188. input("Enter a number: ")
  189.  
  190. # raw_input([prompt])
  191.  
  192. raw_input("Enter your name: ")
  193.  
  194. # int([x, [base]])
  195.  
  196. print(int("10"))
  197.  
  198. print(int("abacaba", 13))
  199.  
  200. print(int("10000", 2))
  201.  
  202. # len(obj)
  203.  
  204. print(len({1: 2, 3: 4, 5: 6}))
  205.  
  206. print(len("str"))
  207.  
  208. print(len([1, 2, 3, 4]))
  209.  
  210. # locals()
  211.  
  212. print(locals())
  213.  
  214. # map(function, iterable)
  215.  
  216. mapped = map(lambda x: (x ** 2) - 1, [0, 1, 2, 3])
  217. for var in mapped:
  218.   print("[", var, "]")
  219.  
  220. # max()
  221.  
  222. print(max([4, 3, 2, 7, 1, 5, 6]))
  223.  
  224. print(max(4, 3, 2, 7, 1, 5, 6))
  225.  
  226. print(max('foo','xyz','abc','woo'))
  227.  
  228. # min()
  229.  
  230. print(min([4, 3, 2, 1, 5, 6, 7]))
  231.  
  232. print(min(4, 3, 2, 1, 5, 6, 7))
  233.  
  234. print(min('foo','xyz','abc','woo'))
  235.  
  236. # oct(int)
  237.  
  238. print(oct(10))
  239.  
  240. print(oct(8))
  241.  
  242. print(oct(124124))
  243.  
  244. # ord(ch)
  245.  
  246. print(ord('a'))
  247.  
  248. # pow(x, y, [z])
  249.  
  250. print(pow(2, 8))
  251.  
  252. print(pow(2, 8, 3))
  253.  
  254. # range(start, [stop, [step]])
  255.  
  256. print(range(3))
  257.  
  258. print(range(3, 7))
  259.  
  260. print(range(3, 7, 2))
  261.  
  262. # round(x, [num])
  263.  
  264. print(round(0.55))
  265.  
  266. print(round(0.535353535, 4))
  267.  
  268. print(round(0.535353535, 3))
  269.  
  270. # slice(start, [stop, [step]])
  271.  
  272. print(slice(3))
  273.  
  274. print(slice(3, 7))
  275.  
  276. print(slice(3, 7, 2))
  277.  
  278. # sorted(iterable, [key, [reverse]])
  279.  
  280. print(sorted([1, 4, 2, 6, 8, 3, 1]))
  281.  
  282. print(sorted([1, 4, 2, 6, 8, 3, 1], reverse=True))
  283.  
  284. print(sorted(['123','xyz','abc',123,'*','-']))
  285.  
  286. # str()
  287.  
  288. print(str(1))
  289.  
  290. print(str(0o123))
  291.  
  292. print(str(0b100))
  293.  
  294. print(str([1, 2, 3, 4]))
  295.  
  296. # sum()
  297.  
  298. print(sum([1, 2, 3, 4, 5, 6]))
  299.  
  300. # type(obj)
  301.  
  302. print(type("str"))
  303.  
  304. print(type({1: 2}))
  305.  
  306. print(type(2))
  307.  
  308. print(type(1 + 3j))
  309.  
  310. print(type(range(1, 3)))
  311.  
  312. # zip(iterable)
  313.  
  314. zipped = zip([1, 2, 3], [4, 5, 6])
  315. for pair in zipped:
  316.   print("{", pair, "}")
  317. '''[1:-1]
  318.  
  319. py2x=py2x.split('\n\n')
  320.  
  321. def testrun(demo):
  322.     for i in demo:
  323.         if '#' in i: print '_'*30+'\n'
  324.         print i
  325.         print
  326.         try:
  327.             exec(i)
  328.         except:
  329.             print '!!! Invalid Entry !!!'
  330.         print
  331.         print
  332.  
  333. #
  334.  
  335. testrun(py2x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement