Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update examples to Python 3 in structure section #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/writing/structure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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[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 = x + 1 # The new x is another object
Expand Down Expand Up @@ -822,7 +822,7 @@ most idiomatic way to do this.
nums = ""
for n in range(20):
nums += str(n) # slow and inefficient
print nums
print(nums)

**Better**

Expand All @@ -832,15 +832,15 @@ most idiomatic way to do this.
nums = []
for n in range(20):
nums.append(str(n))
print "".join(nums) # much more efficient
print("".join(nums)) # much more efficient

**Best**

.. code-block:: python

# create a concatenated string from 0 to 19 (e.g. "012..1819")
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
best. In the instances where you are creating a new string from a pre-determined
Expand Down