From f2b86db2190f58f6f62b3b84db419391aefd04bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 7 Jul 2024 11:42:07 +0200 Subject: [PATCH] Main, Dictionary, Enumerate --- README.md | 13 ++++++------- index.html | 16 ++++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cf2e215..a6a7957 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Contents Main ---- ```python -if __name__ == '__main__': # Runs main() if file wasn't imported. - main() +if __name__ == '__main__': # Skips next line if file was imported. + main() # Runs `def main(): ...` function. ``` @@ -99,9 +99,9 @@ value = .pop(key) # Removes item or raises KeyErro ### Counter ```python >>> from collections import Counter ->>> colors = ['blue', 'blue', 'blue', 'red', 'red'] ->>> counter = Counter(colors) +>>> 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) @@ -153,11 +153,10 @@ Tuple ### Named Tuple **Tuple's subclass with named elements.** - ```python >>> from collections import namedtuple >>> Point = namedtuple('Point', 'x y') ->>> p = Point(1, y=2) +>>> p = Point(1, y=2); p Point(x=1, y=2) >>> p[0] 1 @@ -186,7 +185,7 @@ Range Enumerate --------- ```python -for i, el in enumerate( [, i_start]): +for i, el in enumerate(, start=0): # Returns iterator of `(index+start, )` tuples. ... ``` diff --git a/index.html b/index.html index ba54b96..4986253 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -96,8 +96,8 @@ -

#Main

if __name__ == '__main__':      # Runs main() if file wasn't imported.
-    main()
+

#Main

if __name__ == '__main__':      # Skips next line if file was imported.
+    main()                      # Runs `def main(): ...` function.
 

#List

<el>   = <list>[index]          # First index is 0. Last -1. Allows assignments.
@@ -152,9 +152,9 @@ value = <dict>.pop(key)                         for k, v in <dict>.items() if k in keys}  # Filters the dictionary by keys.
 

Counter

>>> from collections import Counter
->>> colors = ['blue', 'blue', 'blue', 'red', 'red']
->>> counter = Counter(colors)
+>>> 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)
@@ -192,7 +192,7 @@ Counter({'blue': 3

Named Tuple

Tuple's subclass with named elements.

>>> from collections import namedtuple
 >>> Point = namedtuple('Point', 'x y')
->>> p = Point(1, y=2)
+>>> p = Point(1, y=2); p
 Point(x=1, y=2)
 >>> p[0]
 1
@@ -212,7 +212,7 @@ Point(x=1, y=2
 
>>> [i for i in range(3)]
 [0, 1, 2]
 
-

#Enumerate

for i, el in enumerate(<collection> [, i_start]):
+

#Enumerate

for i, el in enumerate(<coll>, start=0):   # Returns iterator of `(index+start, <el>)` tuples.
     ...
 
@@ -2931,7 +2931,7 @@ $ deactivate # Deactivates the activ