Browse Source

Toc, List, Dictionary, Set

pull/144/merge
Jure Šorn 4 months ago
parent
commit
d5820cefc5
3 changed files with 41 additions and 35 deletions
  1. 28
      README.md
  2. 43
      index.html
  3. 5
      parse.js

28
README.md

@ -28,21 +28,25 @@ if __name__ == '__main__': # Skips next line if file was imported.
List
----
```python
<list> = [<el_1>, <el_2>, ...] # Creates new list. Also list(<collection>).
```
```python
<el> = <list>[index] # First index is 0. Last -1. Allows assignments.
<list> = <list>[<slice>] # Or: <list>[from_inclusive : to_exclusive : ±step]
<list> = <list>[<slice>] # Also <list>[from_inclusive : to_exclusive : ±step].
```
```python
<list>.append(<el>) # Or: <list> += [<el>]
<list>.extend(<collection>) # Or: <list> += <collection>
<list>.append(<el>) # Appends element to the end. Also <list> += [<el>].
<list>.extend(<collection>) # Appends elements to the end. Also <list> += <coll>.
```
```python
<list>.sort() # Sorts in ascending order.
<list>.sort() # Sorts elements in ascending order.
<list>.reverse() # Reverses the list in-place.
<list> = sorted(<collection>) # Returns a new sorted list.
<iter> = reversed(<list>) # Returns reversed iterator.
<list> = sorted(<collection>) # Returns new list with sorted elements.
<iter> = reversed(<list>) # Returns reversed iterator of elements.
```
```python
@ -70,6 +74,10 @@ list_of_chars = list(<str>)
Dictionary
----------
```python
<dict> = {key_1: val_1, key_2: val_2, ...} # Use `<dict>[key]` to get or set the value.
```
```python
<view> = <dict>.keys() # Coll. of keys that reflects changes.
<view> = <dict>.values() # Coll. of values that reflects changes.
@ -101,17 +109,15 @@ value = <dict>.pop(key) # Removes item or raises KeyErro
>>> from collections import Counter
>>> counter = Counter(['blue', 'blue', 'blue', 'red', 'red'])
>>> counter['yellow'] += 1
>>> print(counter)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
>>> counter.most_common()[0]
('blue', 3)
>>> print(counter.most_common())
[('blue', 3), ('red', 2), ('yellow', 1)]
```
Set
---
```python
<set> = set() # `{}` returns a dictionary.
<set> = {<el_1>, <el_2>, ...} # Use `set()` for empty set.
```
```python

43
index.html

@ -54,12 +54,12 @@
<body>
<header>
<aside>October 7, 2024</aside>
<aside>October 9, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
<div><h1 id="comprehensivepythoncheatsheet">Comprehensive Python Cheatsheet</h1><p class="banner"><sup><a href="https://raw.githubusercontent.com/gto76/python-cheatsheet/main/README.md">Download text file</a>, <a href="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <a href="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a>, <a href="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a> or <a href="index.html?theme=dark3">Switch to dark theme</a>.
</sup></p><p class="banner"><img src="web/image_888.jpeg" alt="Monty Python"></p><script>
</sup></p><p class="banner" style="margin-bottom: 20px; padding-bottom: 7px;"><img src="web/image_888.jpeg" alt="Monty Python"></p><script>
// Changes the image and link to theme if URL ends with "index.html?theme=dark".
if (window.location.search.search(/[?&]theme=dark/) !== -1) {
@ -79,7 +79,7 @@
}
document.getElementsByClassName("banner")[1].firstChild.replaceWith(img_dark);
}
</script><br><div><h2 id="toc"><a href="#toc" name="toc">#</a>Contents</h2><pre><code class="hljs bash" style="line-height: 1.327em;"><strong>ToC</strong> = {
</script><pre style="border-left: none;padding-left: 1.9px;"><code class="hljs bash" style="line-height: 1.327em;"><strong>ToC</strong> = {
<strong><span class="hljs-string"><span class="hljs-string">'1. Collections'</span></span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],
<strong><span class="hljs-string"><span class="hljs-string">'2. Types'</span></span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regular_Exp</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>],
<strong><span class="hljs-string"><span class="hljs-string">'3. Syntax'</span></span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#imports">Import</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],
@ -89,8 +89,7 @@
<strong><span class="hljs-string"><span class="hljs-string">'7. Libraries'</span></span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#consoleapp">Console_App</a>, <a href="#guiapp">GUI</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>],
<strong><span class="hljs-string"><span class="hljs-string">'8. Multimedia'</span></span></strong>: [<a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#animation">Animation</a>, <a href="#audio">Audio</a>, <a href="#synthesizer">Synthesizer</a>, <a href="#pygame">Pygame</a>, <a href="#pandas">Pandas</a>, <a href="#plotly">Plotly</a>]
}
</code></pre></div></div>
</code></pre></div>
@ -100,17 +99,19 @@
main() <span class="hljs-comment"># Runs `def main(): ...` function.</span>
</code></pre></div>
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;el&gt; = &lt;list&gt;[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
&lt;list&gt; = &lt;list&gt;[&lt;slice&gt;] <span class="hljs-comment"># Or: &lt;list&gt;[from_inclusive : to_exclusive : ±step]</span>
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;list&gt; = [&lt;el_1&gt;, &lt;el_2&gt;, ...] <span class="hljs-comment"># Creates new list. Also list(&lt;collection&gt;).</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;list&gt; += [&lt;el&gt;]</span>
&lt;list&gt;.extend(&lt;collection&gt;) <span class="hljs-comment"># Or: &lt;list&gt; += &lt;collection&gt;</span>
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;list&gt;[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
&lt;list&gt; = &lt;list&gt;[&lt;slice&gt;] <span class="hljs-comment"># Also &lt;list&gt;[from_inclusive : to_exclusive : ±step].</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;list&gt;.sort() <span class="hljs-comment"># Sorts in ascending order.</span>
<pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Appends element to the end. Also &lt;list&gt; += [&lt;el&gt;].</span>
&lt;list&gt;.extend(&lt;collection&gt;) <span class="hljs-comment"># Appends elements to the end. Also &lt;list&gt; += &lt;coll&gt;.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;list&gt;.sort() <span class="hljs-comment"># Sorts elements in ascending order.</span>
&lt;list&gt;.reverse() <span class="hljs-comment"># Reverses the list in-place.</span>
&lt;list&gt; = sorted(&lt;collection&gt;) <span class="hljs-comment"># Returns a new sorted list.</span>
&lt;iter&gt; = reversed(&lt;list&gt;) <span class="hljs-comment"># Returns reversed iterator.</span>
&lt;list&gt; = sorted(&lt;collection&gt;) <span class="hljs-comment"># Returns new list with sorted elements.</span>
&lt;iter&gt; = reversed(&lt;list&gt;) <span class="hljs-comment"># Returns reversed iterator of elements.</span>
</code></pre>
<pre><code class="python language-python hljs">sum_of_elements = sum(&lt;collection&gt;)
elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)]
@ -132,11 +133,13 @@ list_of_chars = list(&lt;str&gt;)
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
&lt;list&gt;.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
</code></pre>
<div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs">&lt;view&gt; = &lt;dict&gt;.keys() <span class="hljs-comment"># Coll. of keys that reflects changes.</span>
&lt;view&gt; = &lt;dict&gt;.values() <span class="hljs-comment"># Coll. of values that reflects changes.</span>
&lt;view&gt; = &lt;dict&gt;.items() <span class="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
<div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs">&lt;dict&gt; = {key_1: val_1, key_2: val_2, ...} <span class="hljs-comment"># Use `&lt;dict&gt;[key]` to get or set the value.</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;view&gt; = &lt;dict&gt;.keys() <span class="hljs-comment"># Coll. of keys that reflects changes.</span>
&lt;view&gt; = &lt;dict&gt;.values() <span class="hljs-comment"># Coll. of values that reflects changes.</span>
&lt;view&gt; = &lt;dict&gt;.items() <span class="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
</code></pre>
<pre><code class="python language-python hljs">value = &lt;dict&gt;.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default if key is missing.</span>
value = &lt;dict&gt;.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span>
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value `&lt;type&gt;()`.</span>
@ -154,13 +157,11 @@ value = &lt;dict&gt;.pop(key) <span class="hljs-comment"
<div><h3 id="counter">Counter</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter
<span class="hljs-meta">&gt;&gt;&gt; </span>counter = Counter([<span class="hljs-string">'blue'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'red'</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span>counter[<span class="hljs-string">'yellow'</span>] += <span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(counter)
Counter({<span class="hljs-string">'blue'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'red'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'yellow'</span>: <span class="hljs-number">1</span>})
<span class="hljs-meta">&gt;&gt;&gt; </span>counter.most_common()[<span class="hljs-number">0</span>]
(<span class="hljs-string">'blue'</span>, <span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>print(counter.most_common())
[(<span class="hljs-string">'blue'</span>, <span class="hljs-number">3</span>), (<span class="hljs-string">'red'</span>, <span class="hljs-number">2</span>), (<span class="hljs-string">'yellow'</span>, <span class="hljs-number">1</span>)]
</code></pre></div>
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = set() <span class="hljs-comment"># `{}` returns a dictionary.</span>
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = {&lt;el_1&gt;, &lt;el_2&gt;, ...} <span class="hljs-comment"># Use `set()` for empty set.</span>
</code></pre></div>
<pre><code class="python language-python hljs">&lt;set&gt;.add(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;set&gt; |= {&lt;el&gt;}</span>
@ -2928,7 +2929,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>October 7, 2024</aside>
<aside>October 9, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

5
parse.js

@ -30,9 +30,7 @@ const hljs = require('highlightjs');
const TOC =
'<br>' +
'<h2 id="toc">Contents</h2>\n' +
'<pre><code class="hljs bash" style="line-height: 1.327em;"><strong>ToC</strong> = {\n' +
'<pre style="border-left: none;padding-left: 1.9px;"><code class="hljs bash" style="line-height: 1.327em;"><strong>ToC</strong> = {\n' +
' <strong><span class="hljs-string">\'1. Collections\'</span></strong>: [<a href="#list">List</a>, <a href="#dictionary">Dictionary</a>, <a href="#set">Set</a>, <a href="#tuple">Tuple</a>, <a href="#range">Range</a>, <a href="#enumerate">Enumerate</a>, <a href="#iterator">Iterator</a>, <a href="#generator">Generator</a>],\n' +
' <strong><span class="hljs-string">\'2. Types\'</span></strong>: [<a href="#type">Type</a>, <a href="#string">String</a>, <a href="#regex">Regular_Exp</a>, <a href="#format">Format</a>, <a href="#numbers">Numbers</a>, <a href="#combinatorics">Combinatorics</a>, <a href="#datetime">Datetime</a>],\n' +
' <strong><span class="hljs-string">\'3. Syntax\'</span></strong>: [<a href="#arguments">Args</a>, <a href="#inline">Inline</a>, <a href="#imports">Import</a>, <a href="#decorator">Decorator</a>, <a href="#class">Class</a>, <a href="#ducktypes">Duck_Types</a>, <a href="#enum">Enum</a>, <a href="#exceptions">Exception</a>],\n' +
@ -776,6 +774,7 @@ function insertLinks() {
function unindentBanner() {
const montyImg = $('img').first();
montyImg.parent().addClass('banner');
montyImg.parent().css({"margin-bottom": "20px", "padding-bottom": "7px"})
const downloadPraragrapth = $('p').first();
downloadPraragrapth.addClass('banner');
}

Loading…
Cancel
Save