Skip to content

Commit 01a39a7

Browse files
committed
fix lists page for python3
1 parent 4b269a3 commit 01a39a7

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

Diff for: lists.md

+18-23
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ docs](http://docs.python.org/tut/node7.html).)
99

1010
```python
1111
colors = ['red', 'blue', 'green']
12-
print colors[0] ## red
13-
print colors[2] ## green
14-
print len(colors) ## 3
12+
print(colors[0]) ## red
13+
print(colors[2]) ## green
14+
print(len(colors)) ## 3
1515
```
1616

1717
![list of strings 'red' 'blue
@@ -34,8 +34,8 @@ is just like + with strings).
3434
FOR and IN
3535
----------
3636

37-
Python's \*for\* and \*in\* constructs are extremely useful, and the
38-
first use of them we'll see is with lists. The \*for\* construct --
37+
Python's *for* and *in* constructs are extremely useful, and the
38+
first use of them we'll see is with lists. The *for* construct --
3939
`for var in list` -- is an easy way to look at each element in a list
4040
(or other collection). Do not add or remove from the list during
4141
iteration.
@@ -46,7 +46,7 @@ sum = 0
4646
for num in squares:
4747
sum += num
4848

49-
print sum ## 30
49+
print(sum) ## 30
5050
```
5151

5252
If you know what sort of thing is in the list, use a variable name in
@@ -55,7 +55,7 @@ the loop that captures that information such as "num", or "name", or
5555
types, your variable names are a key way for you to keep straight what
5656
is going on.
5757

58-
The \*in\* construct on its own is an easy way to test if an element
58+
The *in* construct on its own is an easy way to test if an element
5959
appears in a list (or other collection) -- `value in collection` --
6060
tests if the value is in the collection, returning True/False.
6161

@@ -70,7 +70,7 @@ You may have habits from other languages where you start manually
7070
iterating over a collection, where in Python you should just use for/in.
7171

7272
You can also use for/in to work on a string. The string acts like a list
73-
of its chars, so `for ch in s: print ch` prints all the chars in a
73+
of its chars, so `for ch in s: print(ch)` prints all the chars in a
7474
string.
7575

7676
### Range
@@ -83,13 +83,8 @@ a traditional numeric for loop:
8383
```python
8484
## print the numbers from 0 through 99
8585
for i in range(100):
86-
print i
86+
print(i)
8787
```
88-
89-
There is a variant xrange() which avoids the cost of building the whole
90-
list for performance sensitive cases (in Python 3, `range()` will have
91-
the good performance behavior and you can forget about `xrange()`).
92-
9388
### While Loop
9489

9590
Python also has the standard while-loop, and the *break* and
@@ -104,7 +99,7 @@ every 3rd element in a list:
10499
i = 0
105100
nums = range(100)
106101
while i < len(nums):
107-
print nums[i]
102+
print(nums[i])
108103
i = i + 3
109104
```
110105

@@ -139,23 +134,23 @@ l = ['larry', 'curly', 'moe']
139134
l.append('shemp') ## append elem at end
140135
l.insert(0, 'xxx') ## insert elem at index 0
141136
l.extend(['yyy', 'zzz']) ## add list of elems at end
142-
print l ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
143-
print l.index('curly') ## 2
137+
print(l) ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
138+
print(l.index('curly')) ## 2
144139

145140
l.remove('curly') ## search and remove that element
146141
l.pop(1) ## removes and returns 'larry'
147-
print l ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
142+
print(l) ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
148143
```
149144

150-
Common error: note that the above methods do not \*return\* the modified
145+
Common error: note that the above methods do not *return* the modified
151146
list, they just modify the original list.
152147

153148
```python
154149
l = [1, 2, 3]
155-
print l.append(4) ## NO, does not work, append() returns None
150+
print(l.append(4)) ## NO, does not work, append() returns None
156151
## Correct pattern:
157152
l.append(4)
158-
print l ## [1, 2, 3, 4]
153+
print(l) ## [1, 2, 3, 4]
159154
```
160155

161156
### List Build Up
@@ -176,9 +171,9 @@ change sub-parts of the list.
176171

177172
```python
178173
l = ['a', 'b', 'c', 'd']
179-
print l[1:-1] ## ['b', 'c']
174+
print(l[1:-1]) ## ['b', 'c']
180175
l[0:2] = 'z' ## replace ['a', 'b'] with ['z']
181-
print l ## ['z', 'c', 'd']
176+
print(l) ## ['z', 'c', 'd']
182177
```
183178

184179
Exercise: list1.py

0 commit comments

Comments
 (0)