|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>September 16, 2025</aside> |
| 59 | + <aside>October 5, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
107 | 107 |
|
108 | 108 |
|
109 | 109 |
|
110 | | -<div><h2 id="main"><a href="#main" name="main">#</a>Main</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-comment"># Skips next line if file was imported.</span> |
111 | | - main() <span class="hljs-comment"># Runs `def main(): ...` function.</span> |
| 110 | +<div><h2 id="main"><a href="#main" name="main">#</a>Main</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-comment"># Skips indented lines of code if file was imported.</span> |
| 111 | + main() <span class="hljs-comment"># Executes user-defined `def main(): ...` function.</span> |
112 | 112 | </code></pre></div> |
113 | 113 |
|
114 | | -<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs"><list> = [<el_1>, <el_2>, ...] <span class="hljs-comment"># Creates a list object. Also list(<collection>).</span> |
| 114 | +<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs"><list> = [<el_1>, <el_2>, ...] <span class="hljs-comment"># Creates a new list object. Also list(<collection>).</span> |
115 | 115 | </code></pre></div> |
116 | 116 |
|
117 | | -<pre><code class="python language-python hljs"><el> = <list>[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span> |
| 117 | +<pre><code class="python language-python hljs"><el> = <list>[index] <span class="hljs-comment"># First index is 0, last -1. Also `<list>[i] = <el>`.</span> |
118 | 118 | <list> = <list>[<slice>] <span class="hljs-comment"># Also <list>[from_inclusive : to_exclusive : ±step].</span> |
119 | 119 | </code></pre> |
120 | | -<pre><code class="python language-python hljs"><list>.append(<el>) <span class="hljs-comment"># Appends element to the end. Also <list> += [<el>].</span> |
121 | | -<list>.extend(<collection>) <span class="hljs-comment"># Appends elements to the end. Also <list> += <coll>.</span> |
| 120 | +<pre><code class="python language-python hljs"><list>.append(<el>) <span class="hljs-comment"># Appends element to the end. Also `<list> += [<el>]`.</span> |
| 121 | +<list>.extend(<collection>) <span class="hljs-comment"># Appends multiple elements. Also `<list> += <coll>`.</span> |
122 | 122 | </code></pre> |
123 | | -<pre><code class="python language-python hljs"><list>.sort() <span class="hljs-comment"># Sorts the elements in ascending order.</span> |
124 | | -<list>.reverse() <span class="hljs-comment"># Reverses the order of list's elements.</span> |
125 | | -<list> = sorted(<collection>) <span class="hljs-comment"># Returns a new list with sorted elements.</span> |
126 | | -<iter> = reversed(<list>) <span class="hljs-comment"># Returns reversed iterator of elements.</span> |
| 123 | +<pre><code class="python language-python hljs"><list>.sort(reverse=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Sorts the elements of the list in ascending order.</span> |
| 124 | +<list>.reverse() <span class="hljs-comment"># Reverses the order of elements. Takes linear time.</span> |
| 125 | +<list> = sorted(<collection>) <span class="hljs-comment"># Returns a new sorted list. Accepts `reverse=True`.</span> |
| 126 | +<iter> = reversed(<list>) <span class="hljs-comment"># Returns reversed iterator. Also list(<iterator>).</span> |
127 | 127 | </code></pre> |
128 | | -<pre><code class="python language-python hljs"><el> = max(<collection>) <span class="hljs-comment"># Returns largest element. Also min(<el_1>, ...).</span> |
129 | | -<num> = sum(<collection>) <span class="hljs-comment"># Returns sum of elements. Also math.prod(<coll>).</span> |
| 128 | +<pre><code class="python language-python hljs"><el> = max(<collection>) <span class="hljs-comment"># Returns the largest element. Also min(<el_1>, ...).</span> |
| 129 | +<num> = sum(<collection>) <span class="hljs-comment"># Returns a sum of elements. Also math.prod(<coll>).</span> |
130 | 130 | </code></pre> |
131 | 131 | <pre><code class="python language-python hljs">elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)] |
132 | 132 | sorted_by_second = sorted(<collection>, key=<span class="hljs-keyword">lambda</span> el: el[<span class="hljs-number">1</span>]) |
|
143 | 143 | <int> = <list>.index(<el>) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span> |
144 | 144 | <el> = <list>.pop() <span class="hljs-comment"># Removes and returns item from the end or at index if passed.</span> |
145 | 145 | <list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at passed index and moves the rest to the right.</span> |
146 | | -<list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span> |
| 146 | +<list>.remove(<el>) <span class="hljs-comment"># Removes the first occurrence or raises ValueError exception.</span> |
147 | 147 | <list>.clear() <span class="hljs-comment"># Removes all list's items. Also works on dictionary and set.</span> |
148 | 148 | </code></pre> |
149 | 149 | <div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs"><dict> = {key_1: val_1, key_2: val_2, ...} <span class="hljs-comment"># Use `<dict>[key]` to get or set the value.</span> |
|
153 | 153 | <view> = <dict>.values() <span class="hljs-comment"># Collection of values that reflects changes.</span> |
154 | 154 | <view> = <dict>.items() <span class="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span> |
155 | 155 | </code></pre> |
156 | | -<pre><code class="python language-python hljs">value = <dict>.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default argument if key is missing.</span> |
| 156 | +<pre><code class="python language-python hljs">value = <dict>.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns argument default if key is missing.</span> |
157 | 157 | value = <dict>.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span> |
158 | | -<dict> = collections.defaultdict(<type>) <span class="hljs-comment"># Returns a dict with default value `<type>()`.</span> |
159 | | -<dict> = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Returns a dict with default value 1.</span> |
| 158 | +<dict> = collections.defaultdict(<type>) <span class="hljs-comment"># Dict with automatic default value `<type>()`.</span> |
| 159 | +<dict> = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Dictionary with automatic default value 1.</span> |
160 | 160 | </code></pre> |
161 | 161 | <pre><code class="python language-python hljs"><dict> = dict(<collection>) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span> |
162 | | -<dict> = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dict from two collections.</span> |
163 | | -<dict> = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dict from collection of keys.</span> |
| 162 | +<dict> = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dictionary from two collections.</span> |
| 163 | +<dict> = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dictionary from collection of keys.</span> |
164 | 164 | </code></pre> |
165 | 165 | <pre><code class="python language-python hljs"><dict>.update(<dict>) <span class="hljs-comment"># Adds items. Replaces ones with matching keys.</span> |
166 | 166 | value = <dict>.pop(key) <span class="hljs-comment"># Removes item or raises KeyError if missing.</span> |
|
174 | 174 | [(<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>)] |
175 | 175 | </code></pre></div> |
176 | 176 |
|
177 | | -<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs"><set> = {<el_1>, <el_2>, ...} <span class="hljs-comment"># Use `set()` for empty set.</span> |
| 177 | +<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs"><set> = {<el_1>, <el_2>, ...} <span class="hljs-comment"># Coll. of unique items. Also set(), set(<coll>).</span> |
178 | 178 | </code></pre></div> |
179 | 179 |
|
180 | | -<pre><code class="python language-python hljs"><set>.add(<el>) <span class="hljs-comment"># Or: <set> |= {<el>}</span> |
181 | | -<set>.update(<collection> [, ...]) <span class="hljs-comment"># Or: <set> |= <set></span> |
| 180 | +<pre><code class="python language-python hljs"><set>.add(<el>) <span class="hljs-comment"># Adds item to the set. Also `<set> |= {<el>}`.</span> |
| 181 | +<set>.update(<collection> [, ...]) <span class="hljs-comment"># Adds items to the set. Also `<set> |= <set>`.</span> |
182 | 182 | </code></pre> |
183 | | -<pre><code class="python language-python hljs"><set> = <set>.union(<coll.>) <span class="hljs-comment"># Or: <set> | <set></span> |
184 | | -<set> = <set>.intersection(<coll.>) <span class="hljs-comment"># Or: <set> & <set></span> |
185 | | -<set> = <set>.difference(<coll.>) <span class="hljs-comment"># Or: <set> - <set></span> |
186 | | -<set> = <set>.symmetric_difference(<coll.>) <span class="hljs-comment"># Or: <set> ^ <set></span> |
187 | | -<bool> = <set>.issubset(<coll.>) <span class="hljs-comment"># Or: <set> <= <set></span> |
188 | | -<bool> = <set>.issuperset(<coll.>) <span class="hljs-comment"># Or: <set> >= <set></span> |
| 183 | +<pre><code class="python language-python hljs"><set> = <set>.union(<coll>) <span class="hljs-comment"># Returns a set of all items. Also <set> | <set>.</span> |
| 184 | +<set> = <set>.intersection(<coll>) <span class="hljs-comment"># Returns all shared items. Also <set> & <set>.</span> |
| 185 | +<set> = <set>.difference(<coll>) <span class="hljs-comment"># Returns set's unique items. Also <set> - <set>.</span> |
| 186 | +<set> = <set>.symmetric_difference(<coll>) <span class="hljs-comment"># Returns non-shared items. Also <set> ^ <set>.</span> |
| 187 | +<bool> = <set>.issuperset(<coll>) <span class="hljs-comment"># Returns False if collection has unique items.</span> |
| 188 | +<bool> = <set>.issubset(<coll>) <span class="hljs-comment"># Is collection a superset? Also <set> <= <set>.</span> |
189 | 189 | </code></pre> |
190 | | -<pre><code class="python language-python hljs"><el> = <set>.pop() <span class="hljs-comment"># Raises KeyError if empty.</span> |
191 | | -<set>.remove(<el>) <span class="hljs-comment"># Raises KeyError if missing.</span> |
192 | | -<set>.discard(<el>) <span class="hljs-comment"># Doesn't raise an error.</span> |
| 190 | +<pre><code class="python language-python hljs"><el> = <set>.pop() <span class="hljs-comment"># Removes and returns an item or raises KeyError.</span> |
| 191 | +<set>.remove(<el>) <span class="hljs-comment"># Removes the item or raises KeyError if missing.</span> |
| 192 | +<set>.discard(<el>) <span class="hljs-comment"># Same as remove() but it doesn't raise an error.</span> |
193 | 193 | </code></pre> |
194 | 194 | <div><h3 id="frozenset">Frozen Set</h3><ul> |
195 | 195 | <li><strong>Is immutable and hashable.</strong></li> |
196 | | -<li><strong>That means it can be used as a key in a dictionary or as an element in a set.</strong></li> |
| 196 | +<li><strong>That means it can be used as a key in a dictionary or as an item in a set.</strong></li> |
197 | 197 | </ul><pre><code class="python language-python hljs"><frozenset> = frozenset(<collection>) |
198 | 198 | </code></pre></div> |
199 | 199 |
|
@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2934 | 2934 |
|
2935 | 2935 |
|
2936 | 2936 | <footer> |
2937 | | - <aside>September 16, 2025</aside> |
| 2937 | + <aside>October 5, 2025</aside> |
2938 | 2938 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2939 | 2939 | </footer> |
2940 | 2940 |
|
|
0 commit comments