Merge branch 'master' into feat/py2-to-py3-in-scraping-section

This commit is contained in:
Dan Bader
2021-02-23 09:07:41 -08:00
committed by GitHub
2 changed files with 23 additions and 25 deletions
+4 -4
View File
@@ -788,7 +788,7 @@ compute x + 1, you have to create another integer and give it a name.
my_list = [1, 2, 3] my_list = [1, 2, 3]
my_list[0] = 4 my_list[0] = 4
print my_list # [4, 2, 3] <- The same list has changed print(my_list) # [4, 2, 3] <- The same list has changed
x = 6 x = 6
x = x + 1 # The new x is another object x = x + 1 # The new x is another object
@@ -822,7 +822,7 @@ most idiomatic way to do this.
nums = "" nums = ""
for n in range(20): for n in range(20):
nums += str(n) # slow and inefficient nums += str(n) # slow and inefficient
print nums print(nums)
**Better** **Better**
@@ -832,7 +832,7 @@ most idiomatic way to do this.
nums = [] nums = []
for n in range(20): for n in range(20):
nums.append(str(n)) nums.append(str(n))
print "".join(nums) # much more efficient print("".join(nums)) # much more efficient
**Best** **Best**
@@ -840,7 +840,7 @@ most idiomatic way to do this.
# create a concatenated string from 0 to 19 (e.g. "012..1819") # create a concatenated string from 0 to 19 (e.g. "012..1819")
nums = [str(n) for n in range(20)] nums = [str(n) for n in range(20)]
print "".join(nums) print("".join(nums))
One final thing to mention about strings is that using ``join()`` is not always One final thing to mention about strings is that using ``join()`` is not always
best. In the instances where you are creating a new string from a pre-determined best. In the instances where you are creating a new string from a pre-determined
+19 -21
View File
@@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code.
.. code-block:: python .. code-block:: python
print 'one'; print 'two' print('one'); print('two')
if x == 1: print 'one' if x == 1: print('one')
if <complex comparison> and <other complex comparison>: if <complex comparison> and <other complex comparison>:
# do something # do something
@@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code.
.. code-block:: python .. code-block:: python
print 'one' print('one')
print 'two' print('two')
if x == 1: if x == 1:
print 'one' print('one')
cond1 = <complex comparison> cond1 = <complex comparison>
cond2 = <other complex comparison> cond2 = <other complex comparison>
@@ -357,9 +357,7 @@ Instead, use a list comprehension:
.. code-block:: python .. code-block:: python
four_lists = [[] for __ in xrange(4)] four_lists = [[] for __ in range(4)]
Note: Use range() instead of xrange() in Python 3.
Create a string from a list Create a string from a list
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -584,10 +582,10 @@ list of what is considered false.
.. code-block:: python .. code-block:: python
if attr == True: if attr == True:
print 'True!' print('True!')
if attr == None: if attr == None:
print 'attr is None!' print('attr is None!')
**Good**: **Good**:
@@ -595,15 +593,15 @@ list of what is considered false.
# Just check the value # Just check the value
if attr: if attr:
print 'attr is truthy!' print('attr is truthy!')
# or check for the opposite # or check for the opposite
if not attr: if not attr:
print 'attr is falsey!' print('attr is falsey!')
# or, since None is considered false, explicitly check for it # or, since None is considered false, explicitly check for it
if attr is None: if attr is None:
print 'attr is None!' print('attr is None!')
Access a Dictionary Element Access a Dictionary Element
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -617,9 +615,9 @@ or pass a default argument to :py:meth:`dict.get`.
d = {'hello': 'world'} d = {'hello': 'world'}
if d.has_key('hello'): if d.has_key('hello'):
print d['hello'] # prints 'world' print(d['hello']) # prints 'world'
else: else:
print 'default_value' print('default_value')
**Good**: **Good**:
@@ -627,12 +625,12 @@ or pass a default argument to :py:meth:`dict.get`.
d = {'hello': 'world'} d = {'hello': 'world'}
print d.get('hello', 'default_value') # prints 'world' print(d.get('hello', 'default_value')) # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value' print(d.get('thingy', 'default_value')) # prints 'default_value'
# Or: # Or:
if 'hello' in d: if 'hello' in d:
print d['hello'] print(d['hello'])
Short Ways to Manipulate Lists Short Ways to Manipulate Lists
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -783,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.
a = [3, 4, 5] a = [3, 4, 5]
for i, item in enumerate(a): for i, item in enumerate(a):
print i, item print(i, item)
# prints # prints
# 0 3 # 0 3
# 1 4 # 1 4
@@ -804,7 +802,7 @@ files for you.
f = open('file.txt') f = open('file.txt')
a = f.read() a = f.read()
print a print(a)
f.close() f.close()
**Good**: **Good**:
@@ -813,7 +811,7 @@ files for you.
with open('file.txt') as f: with open('file.txt') as f:
for line in f: for line in f:
print line print(line)
The ``with`` statement is better because it will ensure you always close the The ``with`` statement is better because it will ensure you always close the
file, even if an exception is raised inside the ``with`` block. file, even if an exception is raised inside the ``with`` block.