diff options
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/classes.rst | 2 | ||||
-rw-r--r-- | Doc/tutorial/datastructures.rst | 23 | ||||
-rw-r--r-- | Doc/tutorial/stdlib.rst | 4 |
3 files changed, 18 insertions, 11 deletions
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 0940a75..d9e2f46 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -811,7 +811,7 @@ Examples:: 260 >>> from math import pi, sin - >>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91)) + >>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)} >>> unique_words = set(word for line in page for word in line.split()) diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst index 206f056..e2f218c 100644 --- a/Doc/tutorial/datastructures.rst +++ b/Doc/tutorial/datastructures.rst @@ -273,7 +273,7 @@ Here are some nested for loops and other fancy behavior:: List comprehensions can be applied to complex expressions and nested functions:: - >>> [str(round(355/113.0, i)) for i in range(1, 6)] + >>> [str(round(355/113, i)) for i in range(1, 6)] ['3.1', '3.14', '3.142', '3.1416', '3.14159'] @@ -437,6 +437,9 @@ Here is a brief demonstration:: >>> fruit = set(basket) # create a set without duplicates >>> fruit {'orange', 'pear', 'apple', 'banana'} + >>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists + >>> fruit + {'orange', 'apple'} >>> 'orange' in fruit # fast membership testing True >>> 'crabgrass' in fruit @@ -457,6 +460,11 @@ Here is a brief demonstration:: >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'} +Like for lists, there is a set comprehension syntax:: + + >>> a = {x for x in 'abracadabra' if x not in 'abc'} + >>> a + {'r', 'd'} @@ -518,12 +526,12 @@ comprehensions can compactly specify the key-value list. :: >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) {'sape': 4139, 'jack': 4098, 'guido': 4127} - >>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension - {2: 4, 4: 16, 6: 36} -Later in the tutorial, we will learn about Generator Expressions which are even -better suited for the task of supplying key-values pairs to the :func:`dict` -constructor. +In addition, dict comprehensions can be used to create dictionaries from +arbitrary key and value expressions:: + + >>> {x: x**2 for x in (2, 4, 6)} + {2: 4, 4: 16, 6: 36} When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:: @@ -532,9 +540,8 @@ keyword arguments:: {'sape': 4139, 'jack': 4098, 'guido': 4127} +.. XXX Find out the right way to do these DUBOIS .. _tut-loopidioms: -.. % - Find out the right way to do these DUBOIS Looping Techniques ================== diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst index a474975..725817c 100644 --- a/Doc/tutorial/stdlib.rst +++ b/Doc/tutorial/stdlib.rst @@ -121,7 +121,7 @@ The :mod:`math` module gives access to the underlying C library functions for floating point math:: >>> import math - >>> math.cos(math.pi / 4.0) + >>> math.cos(math.pi / 4) 0.70710678118654757 >>> math.log(1024, 2) 10.0 @@ -264,7 +264,7 @@ documentation:: >>> print(average([20, 30, 70])) 40.0 """ - return sum(values, 0.0) / len(values) + return sum(values) / len(values) import doctest doctest.testmod() # automatically validate the embedded tests |