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.

213 lines
2.5 KiB

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