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.

61 lines
6.4 KiB

4 years ago
1 year ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1 year ago
4 years ago
4 years ago
1 year ago
  1. <details open><summary><strong>Python 2 or Python 3?</strong></summary><br>
  2. &nbsp;&nbsp;&nbsp;&nbsp;Python 3.8 (or higher).
  3. </details><br>
  4. <details open><summary><strong>What is the best way to use it?</strong></summary><br>
  5. &nbsp;&nbsp;&nbsp;&nbsp;I keep the text file open on separate desktop at all times. It is also in a different text editor than the one I usually use, so it's easier to switch to with <code>Ctrl+↹</code> / <code>⌘↹</code>. Cheatsheet consists of minimal text and short examples so things are easy to find with <code>Ctrl+F</code> / <code>⌘F</code>. If you're on the webpage, searching for <code>'#&lt;name&gt;'</code> will only search for the titles.<br><br>
  6. &nbsp;&nbsp;&nbsp;&nbsp;I also keep the Python console open at all times to test little snippets of code, to check out the available functions of a module using code completion and above all, to use <code>help(&lt;module/object/function/type/str&gt;)</code> command. If something is still unclear, then I search the Python docs by googling <code>'python docs &lt;module/function&gt;'</code>.
  7. </details><br>
  8. <details open><summary><strong>What does the '&lt;type&gt;' signify?</strong></summary><br>
  9. &nbsp;&nbsp;&nbsp;&nbsp;It is a placeholder for an object. It needs to be replaced by an expression, literal or a variable that returns/is of that type.
  10. </details><br>
  11. <details open><summary><strong>Why the '&lt;type&gt;' semantics?</strong></summary><br>
  12. &nbsp;&nbsp;&nbsp;&nbsp;It makes examples much less ambiguous.
  13. </details><br>
  14. <details open><summary><strong>Why are some basics like <code>'&lt;list&gt; = [&lt;el_1&gt;, &lt;el_2&gt;, ...]'</code> and <code>'&lt;el&gt; = &lt;list&gt;[&lt;int&gt;]'</code> missing?</strong></summary><br>
  15. &nbsp;&nbsp;&nbsp;&nbsp;This cheatsheet is not intended for complete beginners. This way it can save some valuable space. Nonetheless, it tries to be simple enough to be useful for somebody who completed the introductory course. A nice one-page cheatsheet that can help you get started can be found <a href="https://github.com/kickstartcoding/cheatsheets/blob/master/build/topical/python.pdf">here</a>.</details><br>
  16. <details open><summary><strong>What exactly is <code>&lt;el&gt;</code>?</strong></summary><br>
  17. &nbsp;&nbsp;&nbsp;&nbsp;El is short for element and can be any object, but it usually denotes an object that is an item of a collection.
  18. </details><br>
  19. <details open><summary><strong>What exactly is <code>&lt;collection&gt;</code>?</strong></summary><br>
  20. &nbsp;&nbsp;&nbsp;&nbsp;Collection is my name for an iterable object. An iterable object in Python is any object that has at least one of iter() and getitem() special methods defined. By convention, <code>&lt;object&gt;.__iter__()</code> should return an iterator of object's items and <code>&lt;object&gt;.__getitem__(&lt;index&gt;)</code> an item at that index. I chose not to use the name iterable because it sounds scarier and more vague than collection, even though it has a precise definition.<br><br>
  21. &nbsp;&nbsp;&nbsp;&nbsp;To make matters a bit more confusing, an abstract base class called Iterable doesn't fully follow this definition. An expression <code>instanceof(&lt;object&gt;, collections.abc.Iterable)</code> only checks whether an object has iter() special method, disregarding the getitem().<br><br>
  22. &nbsp;&nbsp;&nbsp;&nbsp;Although collection has no definition in Python's <a href="https://docs.python.org/3/glossary.html">glossary</a>, there exists a Collection abstract base class. Expression <code>instanceof(&lt;object&gt;, collections.abc.Collection)</code> returns 'True' for any object that has len(), iter() and contains() special methods defined. <code>&lt;object&gt;.__len__()</code> should return the number of elements and <code>&lt;object&gt;.__contains__(&lt;el&gt;)</code> should check if object contains the passed element.
  23. </details><br>
  24. <details open><summary><strong>What about PEP 8?</strong></summary><br>
  25. &nbsp;&nbsp;&nbsp;&nbsp;Check out <a href="https://google.github.io/styleguide/pyguide.html">Google Style Guide</a> and use <code>Ctrl+Alt+L</code> / <code>⌥⌘L</code> shortcut in PyCharm to automatically reformat code.
  26. </details><br>
  27. <details open><summary><strong>Why are there no blank lines between method definitions?</strong></summary><br>
  28. &nbsp;&nbsp;&nbsp;&nbsp;This way classes can be copy-pasted into the Python console, which would otherwise raise IndentationError.
  29. </details><br>
  30. <details open><summary><strong>Why are virtual environments and tests not covered?</strong></summary><br>
  31. &nbsp;&nbsp;&nbsp;&nbsp;Check out <a href="https://docs.python-guide.org/">The Hitchhiker’s Guide to Python</a> for a nice overview of the mentioned topics.<br><br>
  32. &nbsp;&nbsp;&nbsp;&nbsp;A quick overview of virtual environments in Unix:<br>
  33. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>python3 -m venv env&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Creates virtual environment in current directory.</code><br>
  34. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>source env/bin/activate&nbsp;&nbsp;# Activates virtual environment.</code><br>
  35. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>deactivate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Deactivates virtual environment.</code><br><br>
  36. &nbsp;&nbsp;&nbsp;&nbsp;And Windows:<br>
  37. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>py -m venv env&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Creates virtual environment in current directory.</code><br>
  38. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>.\env\Scripts\activate&nbsp;&nbsp;&nbsp;# Activates virtual environment.</code><br>
  39. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>deactivate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Deactivates virtual environment.</code><br><br>
  40. </details><br>
  41. <details open><summary><strong>Django?</strong></summary><br>
  42. &nbsp;&nbsp;&nbsp;&nbsp;Here is a nice <a href="https://github.com/kickstartcoding/cheatsheets/blob/master/build/topical/django.jpg">Django cheatsheet</a>.
  43. </details><br>
  44. <details open><summary><strong>Why are there no concrete Regex examples?</strong></summary><br>
  45. &nbsp;&nbsp;&nbsp;&nbsp;Regular expressions are a separate technology that is independent from Python. There are many resources available online.
  46. </details><br>
  47. <details open><summary><strong>Why are descriptors not covered?</strong></summary><br>
  48. &nbsp;&nbsp;&nbsp;&nbsp;Because property decorator is sufficient for everyday use.
  49. </details><br>