|
276 | 276 | <li><strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong></li> |
277 | 277 | </ul> |
278 | 278 | <pre><code class="python language-python hljs"><int> = <list>.count(<el>) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span> |
279 | | -index = <list>.index(<el>) <span class="hljs-comment"># Returns index of first occurrence or raises ValueError.</span> |
280 | | -<list>.insert(index, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span> |
281 | | -<el> = <list>.pop([index]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span> |
282 | | -<list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of item or raises ValueError.</span> |
| 279 | +<int> = <list>.index(<el>) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span> |
| 280 | +<list>.insert(<int>, <el>) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span> |
| 281 | +<el> = <list>.pop([<int>]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span> |
| 282 | +<list>.remove(<el>) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span> |
283 | 283 | <list>.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span> |
284 | 284 | </code></pre> |
285 | 285 | <div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs"><view> = <dict>.keys() <span class="hljs-comment"># Coll. of keys that reflects changes.</span> |
|
450 | 450 | <pre><code class="python language-python hljs"><list> = <str>.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span> |
451 | 451 | <list> = <str>.split(sep=<span class="hljs-keyword">None</span>, maxsplit=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Splits on 'sep' str at most 'maxsplit' times.</span> |
452 | 452 | <list> = <str>.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Splits on \n,\r,\r\n. Keeps them if 'keepends'.</span> |
453 | | -<str> = <str>.join(<coll_of_strings>) <span class="hljs-comment"># Joins elements using string as separator.</span> |
| 453 | +<str> = <str>.join(<coll_of_strings>) <span class="hljs-comment"># Joins elements using string as a separator.</span> |
454 | 454 | </code></pre> |
455 | 455 | <pre><code class="python language-python hljs"><bool> = <sub_str> <span class="hljs-keyword">in</span> <str> <span class="hljs-comment"># Checks if string contains a substring.</span> |
456 | 456 | <bool> = <str>.startswith(<sub_str>) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span> |
457 | 457 | <bool> = <str>.endswith(<sub_str>) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span> |
458 | | -<int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of first match or -1.</span> |
| 458 | +<int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of the first match or -1.</span> |
459 | 459 | <int> = <str>.index(<sub_str>) <span class="hljs-comment"># Same but raises ValueError if missing.</span> |
460 | 460 | </code></pre> |
461 | 461 | <pre><code class="python language-python hljs"><str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span> |
|
614 | 614 | </code></pre></div> |
615 | 615 |
|
616 | 616 | <div><h3 id="random">Random</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random, randint, choice, shuffle, gauss, seed |
| 617 | + |
617 | 618 | <float> = random() |
618 | 619 | <int> = randint(from_inclusive, to_inclusive) |
619 | 620 | <el> = choice(<list>) |
620 | | -shuffle(<list>) |
621 | 621 | </code></pre></div> |
622 | 622 |
|
623 | 623 | <div><h3 id="binhex">Bin, Hex</h3><pre><code class="python language-python hljs"><int> = ±<span class="hljs-number">0</span>b<bin> <span class="hljs-comment"># Or: ±0x<hex></span> |
|
2087 | 2087 | <span class="hljs-keyword">while</span> ch != ascii.ESC: |
2088 | 2088 | height, _ = screen.getmaxyx() |
2089 | 2089 | screen.clear() |
2090 | | - <span class="hljs-keyword">for</span> y, path_ <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]): |
2091 | | - screen.addstr(y, <span class="hljs-number">0</span>, path_, A_REVERSE * (selected == first + y)) |
| 2090 | + <span class="hljs-keyword">for</span> y, a_path <span class="hljs-keyword">in</span> enumerate(paths[first : first+height]): |
| 2091 | + screen.addstr(y, <span class="hljs-number">0</span>, a_path, A_REVERSE * (selected == first + y)) |
2092 | 2092 | ch = screen.getch() |
2093 | 2093 | selected += (ch == KEY_DOWN) - (ch == KEY_UP) |
2094 | 2094 | selected = max(<span class="hljs-number">0</span>, min(len(paths)<span class="hljs-number">-1</span>, selected)) |
|
2143 | 2143 | </ul> |
2144 | 2144 | <div><h2 id="scraping"><a href="#scraping" name="scraping">#</a>Scraping</h2><div><h4 id="scrapespythonsurlversionnumberandlogofromitswikipediapage">Scrapes Python's URL, version number and logo from its Wikipedia page:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests beautifulsoup4</span> |
2145 | 2145 | <span class="hljs-keyword">import</span> requests, bs4, sys |
2146 | | -URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span> |
| 2146 | + |
| 2147 | +WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(programming_language)'</span> |
2147 | 2148 | <span class="hljs-keyword">try</span>: |
2148 | | - html = requests.get(URL).text |
2149 | | - doc = bs4.BeautifulSoup(html, <span class="hljs-string">'html.parser'</span>) |
2150 | | - table = doc.find(<span class="hljs-string">'table'</span>, class_=<span class="hljs-string">'infobox vevent'</span>) |
2151 | | - link = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Website'</span>).next_sibling.a[<span class="hljs-string">'href'</span>] |
2152 | | - ver = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__() |
2153 | | - url_i = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>] |
2154 | | - image = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{url_i}</span>'</span>).content |
| 2149 | + html = requests.get(WIKI_URL).text |
| 2150 | + document = bs4.BeautifulSoup(html, <span class="hljs-string">'html.parser'</span>) |
| 2151 | + table = document.find(<span class="hljs-string">'table'</span>, class_=<span class="hljs-string">'infobox vevent'</span>) |
| 2152 | + python_url = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Website'</span>).next_sibling.a[<span class="hljs-string">'href'</span>] |
| 2153 | + version = table.find(<span class="hljs-string">'th'</span>, text=<span class="hljs-string">'Stable release'</span>).next_sibling.strings.__next__() |
| 2154 | + logo_url = table.find(<span class="hljs-string">'img'</span>)[<span class="hljs-string">'src'</span>] |
| 2155 | + logo = requests.get(<span class="hljs-string">f'https:<span class="hljs-subst">{logo_url}</span>'</span>).content |
2155 | 2156 | <span class="hljs-keyword">with</span> open(<span class="hljs-string">'test.png'</span>, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file: |
2156 | | - file.write(image) |
2157 | | - print(link, ver) |
| 2157 | + file.write(logo) |
| 2158 | + print(python_url, version) |
2158 | 2159 | <span class="hljs-keyword">except</span> requests.exceptions.ConnectionError: |
2159 | 2160 | print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr) |
2160 | 2161 | </code></pre></div></div> |
|
2270 | 2271 | </code></pre> |
2271 | 2272 | <ul> |
2272 | 2273 | <li><strong>Shape is a tuple of dimension sizes.</strong></li> |
2273 | | -<li><strong>Axis is the index of a dimension that gets collapsed. The leftmost dimension has index 0.</strong></li> |
| 2274 | +<li><strong>Axis is an index of the dimension that gets collapsed. Leftmost dimension has index 0.</strong></li> |
2274 | 2275 | </ul> |
2275 | 2276 | <div><h3 id="indexing">Indexing</h3><pre><code class="python language-python hljs"><el> = <2d_array>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>] <span class="hljs-comment"># First element.</span> |
2276 | 2277 | <1d_view> = <2d_array>[<span class="hljs-number">0</span>] <span class="hljs-comment"># First row.</span> |
|
0 commit comments