5353
5454 # Python 2 and 3:
5555 print (' Hello' )
56+
5657 To print multiple strings, import ``print_function `` to prevent Py2 from
5758interpreting it as a tuple:
5859
@@ -96,6 +97,7 @@ Raising exceptions
9697
9798 # Python 2 and 3:
9899 raise ValueError (" dodgy value" )
100+
99101 Raising bare string exceptions:
100102
101103.. code :: python
@@ -106,6 +108,7 @@ Raising bare string exceptions:
106108
107109 # Python 2 and 3:
108110 raise Exception (" dodgy value" )
111+
109112 Raising exceptions with a traceback:
110113
111114.. code :: python
@@ -137,16 +140,18 @@ Catching exceptions
137140
138141.. code :: python
139142
143+ # Python 2 only:
140144 try :
141- # ...
142- except ValueError , e: # Python 2 only
143- # ...
145+ ...
146+ except ValueError , e:
147+ ...
144148 .. code :: python
145149
150+ # Python 2 and 3:
146151 try :
147- # ...
148- except ValueError as e: # Python 2 and 3
149- # ...
152+ ...
153+ except ValueError as e:
154+ ...
150155 Division
151156~~~~~~~~
152157
@@ -160,6 +165,7 @@ Integer division (rounding down):
160165
161166 # Python 2 and 3:
162167 assert 2 // 3 == 0
168+
163169 "True division" (float division):
164170
165171.. code :: python
@@ -172,6 +178,7 @@ Integer division (rounding down):
172178 from __future__ import division # (at top of module)
173179
174180 assert 3 / 2 == 1.5
181+
175182 "Old division" (i.e. compatible with Py2 behaviour):
176183
177184.. code :: python
@@ -293,6 +300,7 @@ prefixes:
293300 # Python 2 and 3
294301 s1 = u ' The Zen of Python'
295302 s2 = u ' きたないのよりきれいな方がいい\n '
303+
296304 The ``futurize `` and ``python-modernize `` tools do not currently offer
297305an option to do this automatically.
298306
@@ -306,6 +314,7 @@ this idiom to make all string literals in a module unicode strings:
306314
307315 s1 = ' The Zen of Python'
308316 s2 = ' きたないのよりきれいな方がいい\n '
317+
309318 See http://python-future.org/unicode\_ literals.html for more discussion
310319on which style to use.
311320
@@ -408,6 +417,7 @@ StringIO
408417 # Python 2 and 3:
409418 from io import BytesIO # for handling byte strings
410419 from io import StringIO # for handling unicode strings
420+
411421 Imports relative to a package
412422-----------------------------
413423
@@ -437,12 +447,14 @@ and the code below is in ``submodule1.py``:
437447 # To make Py2 code safer (more like Py3) by preventing
438448 # implicit relative imports, you can also add this to the top:
439449 from __future__ import absolute_import
450+
440451 Dictionaries
441452------------
442453
443454.. code :: python
444455
445456 heights = {' Fred' : 175 , ' Anne' : 166 , ' Joe' : 192 }
457+
446458 Iterating through ``dict `` keys/values/items
447459~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448460
@@ -487,6 +499,7 @@ Iterable dict values:
487499
488500 for key in itervalues(heights):
489501 # ...
502+
490503 Iterable dict items:
491504
492505.. code :: python
@@ -508,6 +521,7 @@ Iterable dict items:
508521
509522 for (key, value) in iteritems(heights):
510523 # ...
524+
511525 dict keys/values/items as a list
512526~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
513527
@@ -523,6 +537,7 @@ dict keys as a list:
523537 # Python 2 and 3:
524538 keylist = list (heights)
525539 assert isinstance (keylist, list )
540+
526541 dict values as a list:
527542
528543.. code :: python
@@ -556,6 +571,7 @@ dict values as a list:
556571 from six import itervalues
557572
558573 valuelist = list (itervalues(heights))
574+
559575 dict items as a list:
560576
561577.. code :: python
@@ -1058,7 +1074,7 @@ reload()
10581074 from imp import reload
10591075 reload (mymodule)
10601076 Standard library
1061- ================
1077+ ----------------
10621078
10631079StringIO module
10641080~~~~~~~~~~~~~~~
0 commit comments