Skip to content

Commit f27cd7f

Browse files
authored
Merge branch 'master' into feat/py2-to-py3-in-scraping-section
2 parents ed39c48 + f15af06 commit f27cd7f

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

‎docs/writing/structure.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ compute x + 1, you have to create another integer and give it a name.
788788
789789
my_list = [1, 2, 3]
790790
my_list[0] = 4
791-
print my_list # [4, 2, 3] <- The same list has changed
791+
print(my_list) # [4, 2, 3] <- The same list has changed
792792
793793
x = 6
794794
x = x + 1 # The new x is another object
@@ -822,7 +822,7 @@ most idiomatic way to do this.
822822
nums = ""
823823
for n in range(20):
824824
nums += str(n) # slow and inefficient
825-
print nums
825+
print(nums)
826826
827827
**Better**
828828

@@ -832,15 +832,15 @@ most idiomatic way to do this.
832832
nums = []
833833
for n in range(20):
834834
nums.append(str(n))
835-
print "".join(nums) # much more efficient
835+
print("".join(nums)) # much more efficient
836836
837837
**Best**
838838

839839
.. code-block:: python
840840
841841
# create a concatenated string from 0 to 19 (e.g. "012..1819")
842842
nums = [str(n) for n in range(20)]
843-
print "".join(nums)
843+
print("".join(nums))
844844
845845
One final thing to mention about strings is that using ``join()`` is not always
846846
best. In the instances where you are creating a new string from a pre-determined

‎docs/writing/style.rst

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code.
6565

6666
.. code-block:: python
6767
68-
print 'one'; print 'two'
68+
print('one'); print('two')
6969
70-
if x == 1: print 'one'
70+
if x == 1: print('one')
7171
7272
if <complex comparison> and <other complex comparison>:
7373
# do something
@@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code.
7676

7777
.. code-block:: python
7878
79-
print 'one'
80-
print 'two'
79+
print('one')
80+
print('two')
8181
8282
if x == 1:
83-
print 'one'
83+
print('one')
8484
8585
cond1 = <complex comparison>
8686
cond2 = <other complex comparison>
@@ -357,9 +357,7 @@ Instead, use a list comprehension:
357357

358358
.. code-block:: python
359359
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)]
363361
364362
Create a string from a list
365363
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -584,26 +582,26 @@ list of what is considered false.
584582
.. code-block:: python
585583
586584
if attr == True:
587-
print 'True!'
585+
print('True!')
588586
589587
if attr == None:
590-
print 'attr is None!'
588+
print('attr is None!')
591589
592590
**Good**:
593591

594592
.. code-block:: python
595593
596594
# Just check the value
597595
if attr:
598-
print 'attr is truthy!'
596+
print('attr is truthy!')
599597
600598
# or check for the opposite
601599
if not attr:
602-
print 'attr is falsey!'
600+
print('attr is falsey!')
603601
604602
# or, since None is considered false, explicitly check for it
605603
if attr is None:
606-
print 'attr is None!'
604+
print('attr is None!')
607605
608606
Access a Dictionary Element
609607
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -617,22 +615,22 @@ or pass a default argument to :py:meth:`dict.get`.
617615
618616
d = {'hello': 'world'}
619617
if d.has_key('hello'):
620-
print d['hello'] # prints 'world'
618+
print(d['hello']) # prints 'world'
621619
else:
622-
print 'default_value'
620+
print('default_value')
623621
624622
**Good**:
625623

626624
.. code-block:: python
627625
628626
d = {'hello': 'world'}
629627
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'
632630
633631
# Or:
634632
if 'hello' in d:
635-
print d['hello']
633+
print(d['hello'])
636634
637635
Short Ways to Manipulate Lists
638636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -783,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.
783781
784782
a = [3, 4, 5]
785783
for i, item in enumerate(a):
786-
print i, item
784+
print(i, item)
787785
# prints
788786
# 0 3
789787
# 1 4
@@ -804,7 +802,7 @@ files for you.
804802
805803
f = open('file.txt')
806804
a = f.read()
807-
print a
805+
print(a)
808806
f.close()
809807
810808
**Good**:
@@ -813,7 +811,7 @@ files for you.
813811
814812
with open('file.txt') as f:
815813
for line in f:
816-
print line
814+
print(line)
817815
818816
The ``with`` statement is better because it will ensure you always close the
819817
file, even if an exception is raised inside the ``with`` block.

0 commit comments

Comments
 (0)