|
1309 | 1309 | +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
|
1310 | 1310 | </code></pre></div>
|
1311 | 1311 |
|
| 1312 | +<div><h4 id="collectionsandtheirexeceptions">Collections and their execeptions:</h4><pre><code class="python language-python hljs">+-----------+------------+----------+----------+ |
| 1313 | +| | list | dict | set | |
| 1314 | ++-----------+------------+----------+----------+ |
| 1315 | +| getitem() | IndexError | KeyError | | |
| 1316 | +| pop() | IndexError | KeyError | KeyError | |
| 1317 | +| remove() | ValueError | | KeyError | |
| 1318 | +| index() | ValueError | | | |
| 1319 | ++-----------+------------+----------+----------+ |
| 1320 | +</code></pre></div> |
| 1321 | + |
1312 | 1322 | <div><h3 id="userdefinedexceptions">User-defined Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyError</span><span class="hljs-params">(Exception)</span>:</span>
|
1313 | 1323 | <span class="hljs-keyword">pass</span>
|
1314 | 1324 |
|
|
1326 | 1336 | <div><h3 id="prettyprint">Pretty Print</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> pprint <span class="hljs-keyword">import</span> pprint
|
1327 | 1337 | <span class="hljs-meta">>>> </span>pprint(dir())
|
1328 | 1338 | [<span class="hljs-string">'__annotations__'</span>,
|
1329 |
| - <span class="hljs-string">'__builtins__'</span>, |
1330 |
| - <span class="hljs-string">'__doc__'</span>, ...] |
| 1339 | + <span class="hljs-string">'__builtins__'</span>, ...] |
1331 | 1340 | </code></pre></div>
|
1332 | 1341 |
|
1333 | 1342 | <div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><ul>
|
1334 | 1343 | <li><strong>Reads a line from user input or pipe if present.</strong></li>
|
1335 | 1344 | <li><strong>Trailing newline gets stripped.</strong></li>
|
1336 | 1345 | <li><strong>Prompt string is printed to the standard output before reading input.</strong></li>
|
| 1346 | +<li><strong>Raises EOFError when user hits EOF or input stream gets exhausted.</strong></li> |
1337 | 1347 | </ul><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>)
|
1338 | 1348 | </code></pre></div>
|
1339 | 1349 |
|
1340 | 1350 |
|
1341 |
| -<div><h4 id="printslinesuntileof">Prints lines until EOF:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>: |
1342 |
| - <span class="hljs-keyword">try</span>: |
1343 |
| - print(input()) |
1344 |
| - <span class="hljs-keyword">except</span> EOFError: |
1345 |
| - <span class="hljs-keyword">break</span> |
1346 |
| -</code></pre></div> |
1347 |
| - |
1348 | 1351 | <div><h2 id="commandlinearguments"><a href="#commandlinearguments" name="commandlinearguments">#</a>Command Line Arguments</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
1349 | 1352 | script_name = sys.argv[<span class="hljs-number">0</span>]
|
1350 | 1353 | arguments = sys.argv[<span class="hljs-number">1</span>:]
|
|
0 commit comments