From d252dfebe9584234839d925421caed9ce7844192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 27 Apr 2025 19:56:45 +0200 Subject: [PATCH] Tuple, Enumerate, Iterator --- README.md | 19 +++++++++---------- index.html | 20 +++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index b79562d..d833967 100644 --- a/README.md +++ b/README.md @@ -165,14 +165,11 @@ Tuple ```python >>> from collections import namedtuple >>> Point = namedtuple('Point', 'x y') ->>> p = Point(1, y=2); p +>>> p = Point(1, y=2) +>>> print(p) Point(x=1, y=2) ->>> p[0] -1 ->>> p.x -1 ->>> getattr(p, 'y') -2 +>>> p[0], p.x +(1, 1) ``` @@ -180,9 +177,9 @@ Range ----- **Immutable and hashable sequence of integers.** ```python - = range(stop) # range(to_exclusive) - = range(start, stop) # range(from_inclusive, to_exclusive) - = range(start, stop, ±step) # range(from_inclusive, to_exclusive, ±step_size) + = range(stop) # I.e. range(to_exclusive). + = range(start, stop) # I.e. range(from_inclusive, to_exclusive). + = range(start, stop, ±step) # I.e. range(from_inclusive, to_exclusive, ±step). ``` ```python @@ -201,6 +198,8 @@ for i, el in enumerate(, start=0): # Returns next element and its index Iterator -------- +**Potentially endless stream of elements.** + ```python = iter() # `iter()` returns unmodified iterator. = iter(, to_exclusive) # A sequence of return values until 'to_exclusive'. diff --git a/index.html b/index.html index f640964..891bd53 100644 --- a/index.html +++ b/index.html @@ -206,20 +206,17 @@ value = <dict>.pop(key)

Named Tuple

Tuple's subclass with named elements.

>>> from collections import namedtuple
 >>> Point = namedtuple('Point', 'x y')
->>> p = Point(1, y=2); p
+>>> p = Point(1, y=2)
+>>> print(p)
 Point(x=1, y=2)
->>> p[0]
-1
->>> p.x
-1
->>> getattr(p, 'y')
-2
+>>> p[0], p.x
+(1, 1)
 
-

#Range

Immutable and hashable sequence of integers.

<range> = range(stop)                      # range(to_exclusive)
-<range> = range(start, stop)               # range(from_inclusive, to_exclusive)
-<range> = range(start, stop, ±step)        # range(from_inclusive, to_exclusive, ±step_size)
+

#Range

Immutable and hashable sequence of integers.

<range> = range(stop)                      # I.e. range(to_exclusive).
+<range> = range(start, stop)               # I.e. range(from_inclusive, to_exclusive).
+<range> = range(start, stop, ±step)        # I.e. range(from_inclusive, to_exclusive, ±step).
 
@@ -230,12 +227,13 @@ Point(x=1, y=2 ...
-

#Iterator

<iter> = iter(<collection>)                # `iter(<iter>)` returns unmodified iterator.
+

#Iterator

Potentially endless stream of elements.

<iter> = iter(<collection>)                # `iter(<iter>)` returns unmodified iterator.
 <iter> = iter(<function>, to_exclusive)    # A sequence of return values until 'to_exclusive'.
 <el>   = next(<iter> [, default])          # Raises StopIteration or returns 'default' on end.
 <list> = list(<iter>)                      # Returns a list of iterator's remaining elements.
 
+

Itertools

import itertools as it