@@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code.
65
65
66
66
.. code-block :: python
67
67
68
- print ' one' ; print ' two'
68
+ print ( ' one' ) ; print ( ' two' )
69
69
70
- if x == 1 : print ' one'
70
+ if x == 1 : print ( ' one' )
71
71
72
72
if < complex comparison> and < other complex comparison> :
73
73
# do something
@@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code.
76
76
77
77
.. code-block :: python
78
78
79
- print ' one'
80
- print ' two'
79
+ print ( ' one' )
80
+ print ( ' two' )
81
81
82
82
if x == 1 :
83
- print ' one'
83
+ print ( ' one' )
84
84
85
85
cond1 = < complex comparison>
86
86
cond2 = < other complex comparison>
@@ -357,9 +357,7 @@ Instead, use a list comprehension:
357
357
358
358
.. code-block :: python
359
359
360
- four_lists = [[] for __ in xrange (4 )]
361
-
362
- Note: Use range() instead of xrange() in Python 3.
360
+ four_lists = [[] for __ in range (4 )]
363
361
364
362
Create a string from a list
365
363
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -584,26 +582,26 @@ list of what is considered false.
584
582
.. code-block :: python
585
583
586
584
if attr == True :
587
- print ' True!'
585
+ print ( ' True!' )
588
586
589
587
if attr == None :
590
- print ' attr is None!'
588
+ print ( ' attr is None!' )
591
589
592
590
**Good **:
593
591
594
592
.. code-block :: python
595
593
596
594
# Just check the value
597
595
if attr:
598
- print ' attr is truthy!'
596
+ print ( ' attr is truthy!' )
599
597
600
598
# or check for the opposite
601
599
if not attr:
602
- print ' attr is falsey!'
600
+ print ( ' attr is falsey!' )
603
601
604
602
# or, since None is considered false, explicitly check for it
605
603
if attr is None :
606
- print ' attr is None!'
604
+ print ( ' attr is None!' )
607
605
608
606
Access a Dictionary Element
609
607
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -617,22 +615,22 @@ or pass a default argument to :py:meth:`dict.get`.
617
615
618
616
d = {' hello' : ' world' }
619
617
if d.has_key(' hello' ):
620
- print d[' hello' ] # prints 'world'
618
+ print ( d[' hello' ]) # prints 'world'
621
619
else :
622
- print ' default_value'
620
+ print ( ' default_value' )
623
621
624
622
**Good **:
625
623
626
624
.. code-block :: python
627
625
628
626
d = {' hello' : ' world' }
629
627
630
- print d.get(' hello' , ' default_value' ) # prints 'world'
631
- print d.get(' thingy' , ' default_value' ) # prints 'default_value'
628
+ print ( d.get(' hello' , ' default_value' ) ) # prints 'world'
629
+ print ( d.get(' thingy' , ' default_value' ) ) # prints 'default_value'
632
630
633
631
# Or:
634
632
if ' hello' in d:
635
- print d[' hello' ]
633
+ print ( d[' hello' ])
636
634
637
635
Short Ways to Manipulate Lists
638
636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -783,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.
783
781
784
782
a = [3 , 4 , 5 ]
785
783
for i, item in enumerate (a):
786
- print i, item
784
+ print ( i, item)
787
785
# prints
788
786
# 0 3
789
787
# 1 4
@@ -804,7 +802,7 @@ files for you.
804
802
805
803
f = open (' file.txt' )
806
804
a = f.read()
807
- print a
805
+ print (a)
808
806
f.close()
809
807
810
808
**Good **:
@@ -813,7 +811,7 @@ files for you.
813
811
814
812
with open (' file.txt' ) as f:
815
813
for line in f:
816
- print line
814
+ print ( line)
817
815
818
816
The ``with `` statement is better because it will ensure you always close the
819
817
file, even if an exception is raised inside the ``with `` block.
0 commit comments