You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
2.5 KiB

7 years ago
7 years ago
7 years ago
  1. Minimal Python Cheatsheet
  2. =========================
  3. Main
  4. ----
  5. ```
  6. if __name__ == '__main__':
  7. main()
  8. ```
  9. Ranges
  10. ------
  11. ```python
  12. range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward.
  13. <list>[<from inclusive>:<to exclusive>:<step size>] # Negative step for backward.
  14. random.randint(<from inclusive>, <to inclusive>)
  15. ```
  16. List
  17. ----
  18. ```python
  19. ```
  20. Dictionary
  21. ----------
  22. ```
  23. <dict>.items()
  24. <dict>.get(<key>, <default>)
  25. <dict>.setdefault(<key>, <default>)
  26. ```
  27. Enumerate
  28. ---------
  29. ```
  30. for i, <el> in enumerate(<list/dict/set>)
  31. ```
  32. Inline
  33. ------
  34. ### For
  35. ```pythonstub
  36. [i+j for i in range(10) for j in range(10) if i+j > 5]
  37. ```
  38. ### Lambda
  39. ```
  40. lambda <arg1>, <arg2>: <return value>
  41. ```
  42. String
  43. ------
  44. ### Print
  45. ```
  46. print(<el1>, <el2>, end='', sep='', file=<file>)
  47. ```
  48. ### Regex
  49. ```
  50. import re
  51. re.sub(<regex>, <new>, <text>)
  52. re.search(<regex>, <text>)
  53. ```
  54. ### Format
  55. ```
  56. {:<min width>} -> '<el> '
  57. {:><min width>} -> ' <el>'
  58. {:^<min width>} -> ' <el> '
  59. {:_<min width>} -> '<el>____'
  60. {:.<max width>} -> '<e>'
  61. {:<max widht>.<min width>} -> ' <e>'
  62. {:<max width>.<no of decimals>f} -> ' 3.14'
  63. ```
  64. Infinity
  65. --------
  66. ```
  67. float("inf")
  68. ```
  69. Class
  70. -----
  71. ### Class
  72. ```
  73. class <name>:
  74. def __init__(self, <arg>):
  75. self.a = <arg>
  76. def __repr__(self):
  77. return str({'a': self.a})
  78. def __str__(self):
  79. return str(self.a)
  80. ```
  81. ### Enum
  82. ```
  83. import enum
  84. class <name>(enum.Enum):
  85. <value> = <index>
  86. ```
  87. ### Copy
  88. ```
  89. import copy
  90. copy.copy(<object>)
  91. copy.deepcopy(<object>)
  92. ```
  93. Random
  94. ------
  95. ```
  96. import random
  97. random.random()
  98. random.shuffle(<list>)
  99. ```
  100. Datetime
  101. --------
  102. ```
  103. import datetime
  104. now = datetime.datetime.now()
  105. now.strftime('%Y%m%d')
  106. now.strftime('%Y%m%d%H%M%S')
  107. ```
  108. System
  109. ------
  110. ### Arguments
  111. ```
  112. import sys
  113. sys.argv
  114. ```
  115. ### Read
  116. ```
  117. with open(<filename>, encoding='utf-8') as file:
  118. return file.readlines()
  119. ```
  120. ### Write
  121. ```
  122. with open(<filename>, 'w', enconding='utf-8') as file:
  123. file.write(<text>)
  124. ```
  125. ### Execute Command
  126. ```
  127. import os
  128. os.popen(<command>).read()
  129. ```
  130. JSON
  131. ----
  132. ```
  133. import json
  134. ```
  135. ### Read
  136. ```
  137. with open(<filename>, encoding='utf-8') as file:
  138. return json.load(file)
  139. ```
  140. ### Write
  141. ```
  142. with open(<filename>, 'w', enconding='utf-8') as file:
  143. file.write(json.dumps(<object>))
  144. ```
  145. SQLite
  146. ------
  147. ```
  148. import sqlite3
  149. db = sqlite3.connect(<filename>)
  150. ```
  151. ### Read
  152. ```
  153. cursor = db.execute(<query>)
  154. if cursor:
  155. cursor.<fetchone/fetchall>()
  156. db.close()
  157. ```
  158. ### Write
  159. ```
  160. db.execute(<query>)
  161. db.commit()
  162. ```