Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4a54da

Browse files
committed
List, String, Numbers, Curses, Scraping
1 parent bcf0282 commit b4a54da

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ list_of_chars = list(<str>)
5757

5858
```python
5959
<int> = <list>.count(<el>) # Returns number of occurrences. Also works on strings.
60-
index = <list>.index(<el>) # Returns index of first occurrence or raises ValueError.
61-
<list>.insert(index, <el>) # Inserts item at index and moves the rest to the right.
62-
<el> = <list>.pop([index]) # Removes and returns item at index or from the end.
63-
<list>.remove(<el>) # Removes first occurrence of item or raises ValueError.
60+
<int> = <list>.index(<el>) # Returns index of the first occurrence or raises ValueError.
61+
<list>.insert(<int>, <el>) # Inserts item at index and moves the rest to the right.
62+
<el> = <list>.pop([<int>]) # Removes and returns item at index or from the end.
63+
<list>.remove(<el>) # Removes first occurrence of the item or raises ValueError.
6464
<list>.clear() # Removes all items. Also works on dictionary and set.
6565
```
6666

@@ -309,14 +309,14 @@ String
309309
<list> = <str>.split() # Splits on one or more whitespace characters.
310310
<list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
311311
<list> = <str>.splitlines(keepends=False) # Splits on \n,\r,\r\n. Keeps them if 'keepends'.
312-
<str> = <str>.join(<coll_of_strings>) # Joins elements using string as separator.
312+
<str> = <str>.join(<coll_of_strings>) # Joins elements using string as a separator.
313313
```
314314

315315
```python
316316
<bool> = <sub_str> in <str> # Checks if string contains a substring.
317317
<bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
318318
<bool> = <str>.endswith(<sub_str>) # Pass tuple of strings for multiple options.
319-
<int> = <str>.find(<sub_str>) # Returns start index of first match or -1.
319+
<int> = <str>.find(<sub_str>) # Returns start index of the first match or -1.
320320
<int> = <str>.index(<sub_str>) # Same but raises ValueError if missing.
321321
```
322322

@@ -512,10 +512,10 @@ from statistics import mean, median, variance, stdev, pvariance, pstdev
512512
### Random
513513
```python
514514
from random import random, randint, choice, shuffle, gauss, seed
515+
515516
<float> = random()
516517
<int> = randint(from_inclusive, to_inclusive)
517518
<el> = choice(<list>)
518-
shuffle(<list>)
519519
```
520520

521521
### Bin, Hex
@@ -2381,8 +2381,8 @@ def main(screen):
23812381
while ch != ascii.ESC:
23822382
height, _ = screen.getmaxyx()
23832383
screen.clear()
2384-
for y, path_ in enumerate(paths[first : first+height]):
2385-
screen.addstr(y, 0, path_, A_REVERSE * (selected == first + y))
2384+
for y, a_path in enumerate(paths[first : first+height]):
2385+
screen.addstr(y, 0, a_path, A_REVERSE * (selected == first + y))
23862386
ch = screen.getch()
23872387
selected += (ch == KEY_DOWN) - (ch == KEY_UP)
23882388
selected = max(0, min(len(paths)-1, selected))
@@ -2448,18 +2448,19 @@ Scraping
24482448
```python
24492449
# $ pip3 install requests beautifulsoup4
24502450
import requests, bs4, sys
2451-
URL = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
2451+
2452+
WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
24522453
try:
2453-
html = requests.get(URL).text
2454-
doc = bs4.BeautifulSoup(html, 'html.parser')
2455-
table = doc.find('table', class_='infobox vevent')
2456-
link = table.find('th', text='Website').next_sibling.a['href']
2457-
ver = table.find('th', text='Stable release').next_sibling.strings.__next__()
2458-
url_i = table.find('img')['src']
2459-
image = requests.get(f'https:{url_i}').content
2454+
html = requests.get(WIKI_URL).text
2455+
document = bs4.BeautifulSoup(html, 'html.parser')
2456+
table = document.find('table', class_='infobox vevent')
2457+
python_url = table.find('th', text='Website').next_sibling.a['href']
2458+
version = table.find('th', text='Stable release').next_sibling.strings.__next__()
2459+
logo_url = table.find('img')['src']
2460+
logo = requests.get(f'https:{logo_url}').content
24602461
with open('test.png', 'wb') as file:
2461-
file.write(image)
2462-
print(link, ver)
2462+
file.write(logo)
2463+
print(python_url, version)
24632464
except requests.exceptions.ConnectionError:
24642465
print("You've got problems with connection.", file=sys.stderr)
24652466
```
@@ -2614,7 +2615,7 @@ indexes = <array>.argmin(axis)
26142615
```
26152616

26162617
* **Shape is a tuple of dimension sizes.**
2617-
* **Axis is the index of a dimension that gets collapsed. The leftmost dimension has index 0.**
2618+
* **Axis is an index of the dimension that gets collapsed. Leftmost dimension has index 0.**
26182619

26192620
### Indexing
26202621
```bash

index.html

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@
276276
<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>
277277
</ul>
278278
<pre><code class="python language-python hljs">&lt;int&gt; = &lt;list&gt;.count(&lt;el&gt;) <span class="hljs-comment"># Returns number of occurrences. Also works on strings.</span>
279-
index = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of first occurrence or raises ValueError.</span>
280-
&lt;list&gt;.insert(index, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
281-
&lt;el&gt; = &lt;list&gt;.pop([index]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span>
282-
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of item or raises ValueError.</span>
279+
&lt;int&gt; = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span>
280+
&lt;list&gt;.insert(&lt;int&gt;, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
281+
&lt;el&gt; = &lt;list&gt;.pop([&lt;int&gt;]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span>
282+
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
283283
&lt;list&gt;.clear() <span class="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
284284
</code></pre>
285285
<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>
@@ -450,12 +450,12 @@
450450
<pre><code class="python language-python hljs">&lt;list&gt; = &lt;str&gt;.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span>
451451
&lt;list&gt; = &lt;str&gt;.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>
452452
&lt;list&gt; = &lt;str&gt;.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Splits on \n,\r,\r\n. Keeps them if 'keepends'.</span>
453-
&lt;str&gt; = &lt;str&gt;.join(&lt;coll_of_strings&gt;) <span class="hljs-comment"># Joins elements using string as separator.</span>
453+
&lt;str&gt; = &lt;str&gt;.join(&lt;coll_of_strings&gt;) <span class="hljs-comment"># Joins elements using string as a separator.</span>
454454
</code></pre>
455455
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;sub_str&gt; <span class="hljs-keyword">in</span> &lt;str&gt; <span class="hljs-comment"># Checks if string contains a substring.</span>
456456
&lt;bool&gt; = &lt;str&gt;.startswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
457457
&lt;bool&gt; = &lt;str&gt;.endswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
458-
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of first match or -1.</span>
458+
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
459459
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same but raises ValueError if missing.</span>
460460
</code></pre>
461461
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
@@ -614,10 +614,10 @@
614614
</code></pre></div>
615615

616616
<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+
617618
&lt;float&gt; = random()
618619
&lt;int&gt; = randint(from_inclusive, to_inclusive)
619620
&lt;el&gt; = choice(&lt;list&gt;)
620-
shuffle(&lt;list&gt;)
621621
</code></pre></div>
622622

623623
<div><h3 id="binhex">Bin, Hex</h3><pre><code class="python language-python hljs">&lt;int&gt; = ±<span class="hljs-number">0</span>b&lt;bin&gt; <span class="hljs-comment"># Or: ±0x&lt;hex&gt;</span>
@@ -2087,8 +2087,8 @@
20872087
<span class="hljs-keyword">while</span> ch != ascii.ESC:
20882088
height, _ = screen.getmaxyx()
20892089
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))
20922092
ch = screen.getch()
20932093
selected += (ch == KEY_DOWN) - (ch == KEY_UP)
20942094
selected = max(<span class="hljs-number">0</span>, min(len(paths)<span class="hljs-number">-1</span>, selected))
@@ -2143,18 +2143,19 @@
21432143
</ul>
21442144
<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>
21452145
<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>
21472148
<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
21552156
<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)
21582159
<span class="hljs-keyword">except</span> requests.exceptions.ConnectionError:
21592160
print(<span class="hljs-string">"You've got problems with connection."</span>, file=sys.stderr)
21602161
</code></pre></div></div>
@@ -2270,7 +2271,7 @@
22702271
</code></pre>
22712272
<ul>
22722273
<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>
22742275
</ul>
22752276
<div><h3 id="indexing">Indexing</h3><pre><code class="python language-python hljs">&lt;el&gt; = &lt;2d_array&gt;[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>] <span class="hljs-comment"># First element.</span>
22762277
&lt;1d_view&gt; = &lt;2d_array&gt;[<span class="hljs-number">0</span>] <span class="hljs-comment"># First row.</span>

0 commit comments

Comments
 (0)