@@ -582,26 +582,26 @@ list of what is considered false.
582
582
.. code-block :: python
583
583
584
584
if attr == True :
585
- print ' True!'
585
+ print ( ' True!' )
586
586
587
587
if attr == None :
588
- print ' attr is None!'
588
+ print ( ' attr is None!' )
589
589
590
590
**Good **:
591
591
592
592
.. code-block :: python
593
593
594
594
# Just check the value
595
595
if attr:
596
- print ' attr is truthy!'
596
+ print ( ' attr is truthy!' )
597
597
598
598
# or check for the opposite
599
599
if not attr:
600
- print ' attr is falsey!'
600
+ print ( ' attr is falsey!' )
601
601
602
602
# or, since None is considered false, explicitly check for it
603
603
if attr is None :
604
- print ' attr is None!'
604
+ print ( ' attr is None!' )
605
605
606
606
Access a Dictionary Element
607
607
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -615,22 +615,22 @@ or pass a default argument to :py:meth:`dict.get`.
615
615
616
616
d = {' hello' : ' world' }
617
617
if d.has_key(' hello' ):
618
- print d[' hello' ] # prints 'world'
618
+ print ( d[' hello' ]) # prints 'world'
619
619
else :
620
- print ' default_value'
620
+ print ( ' default_value' )
621
621
622
622
**Good **:
623
623
624
624
.. code-block :: python
625
625
626
626
d = {' hello' : ' world' }
627
627
628
- print d.get(' hello' , ' default_value' ) # prints 'world'
629
- 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'
630
630
631
631
# Or:
632
632
if ' hello' in d:
633
- print d[' hello' ]
633
+ print ( d[' hello' ])
634
634
635
635
Short Ways to Manipulate Lists
636
636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -781,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.
781
781
782
782
a = [3 , 4 , 5 ]
783
783
for i, item in enumerate (a):
784
- print i, item
784
+ print ( i, item)
785
785
# prints
786
786
# 0 3
787
787
# 1 4
@@ -802,7 +802,7 @@ files for you.
802
802
803
803
f = open (' file.txt' )
804
804
a = f.read()
805
- print a
805
+ print (a)
806
806
f.close()
807
807
808
808
**Good **:
@@ -811,7 +811,7 @@ files for you.
811
811
812
812
with open (' file.txt' ) as f:
813
813
for line in f:
814
- print line
814
+ print ( line)
815
815
816
816
The ``with `` statement is better because it will ensure you always close the
817
817
file, even if an exception is raised inside the ``with `` block.
0 commit comments