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

  1. Python and Libraries Cheatsheet
  2. ===============================
  3. Main
  4. ----
  5. ```
  6. if __name__ == '__main__':
  7. main()
  8. ```
  9. Range
  10. -----
  11. ```python
  12. range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward.
  13. ```
  14. List
  15. ----
  16. ```python
  17. <list>[<inclusive from>:<exclusive to>:<step size>]
  18. ```
  19. Dictionary
  20. ----------
  21. ```
  22. <dict>.items()
  23. <dict>.get(<key>, <default>)
  24. <dict>.setdefault(<key>, <default>)
  25. ```
  26. Enumerate
  27. ---------
  28. ```
  29. for i, <el> in enumerate(<list/dict/set>)
  30. ```
  31. Inline
  32. ------
  33. ### For
  34. ```pythonstub
  35. [i+j for i in range(10) for j in range(10) if i+j > 5]
  36. ```
  37. ### Lambda
  38. ```
  39. lambda <arg1>, <arg2>: <return value>
  40. ```
  41. String
  42. ------
  43. ### Print
  44. ```
  45. print(<el1>, <el2>, end='', sep='', file=<file>)
  46. ```
  47. ### Regex
  48. ```
  49. import re
  50. re.sub(<regex>, <new>, <text>)
  51. re.search(<regex>, <text>)
  52. ```
  53. ### Format
  54. ```
  55. {:<min width>} -> '<el> '
  56. {:><min width>} -> ' <el>'
  57. {:^<min width>} -> ' <el> '
  58. {:_<min width>} -> '<el>____'
  59. {:.<max width>} -> '<e>'
  60. {:<max widht>.<min width>} -> ' <e>'
  61. {:<max width>.<no of decimals>f} -> ' 3.14'
  62. ```
  63. Infinity
  64. --------
  65. ```
  66. float("inf")
  67. ```
  68. Class
  69. -----
  70. ### Class
  71. ```
  72. class <name>:
  73. def __init__(self, <arg>):
  74. self.a = <arg>
  75. def __repr__(self):
  76. return str({'a': self.a})
  77. def __str__(self):
  78. return str(self.a)
  79. ```
  80. ### Enum
  81. ```
  82. import enum
  83. class <name>(enum.Enum):
  84. <value> = <index>
  85. ```
  86. ### Copy
  87. ```
  88. import copy
  89. copy.copy(<object>)
  90. copy.deepcopy(<object>)
  91. ```
  92. Random
  93. ------
  94. ```
  95. import random
  96. random.random()
  97. random.randint(<from inclusive>, <to inclusive>)
  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. ```