@@ -9,9 +9,9 @@ docs](http://docs.python.org/tut/node7.html).)
9
9
10
10
``` python
11
11
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
15
15
```
16
16
17
17
![ list of strings 'red' 'blue
@@ -34,8 +34,8 @@ is just like + with strings).
34
34
FOR and IN
35
35
----------
36
36
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 --
39
39
` for var in list ` -- is an easy way to look at each element in a list
40
40
(or other collection). Do not add or remove from the list during
41
41
iteration.
@@ -46,7 +46,7 @@ sum = 0
46
46
for num in squares:
47
47
sum += num
48
48
49
- print sum # # 30
49
+ print ( sum ) # # 30
50
50
```
51
51
52
52
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
55
55
types, your variable names are a key way for you to keep straight what
56
56
is going on.
57
57
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
59
59
appears in a list (or other collection) -- ` value in collection ` --
60
60
tests if the value is in the collection, returning True/False.
61
61
@@ -70,7 +70,7 @@ You may have habits from other languages where you start manually
70
70
iterating over a collection, where in Python you should just use for/in.
71
71
72
72
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
74
74
string.
75
75
76
76
### Range
@@ -83,13 +83,8 @@ a traditional numeric for loop:
83
83
``` python
84
84
# # print the numbers from 0 through 99
85
85
for i in range (100 ):
86
- print i
86
+ print (i)
87
87
```
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
-
93
88
### While Loop
94
89
95
90
Python also has the standard while-loop, and the * break* and
@@ -104,7 +99,7 @@ every 3rd element in a list:
104
99
i = 0
105
100
nums = range (100 )
106
101
while i < len (nums):
107
- print nums[i]
102
+ print ( nums[i])
108
103
i = i + 3
109
104
```
110
105
@@ -139,23 +134,23 @@ l = ['larry', 'curly', 'moe']
139
134
l.append(' shemp' ) # # append elem at end
140
135
l.insert(0 , ' xxx' ) # # insert elem at index 0
141
136
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
144
139
145
140
l.remove(' curly' ) # # search and remove that element
146
141
l.pop(1 ) # # removes and returns 'larry'
147
- print l # # ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
142
+ print (l) # # ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
148
143
```
149
144
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
151
146
list, they just modify the original list.
152
147
153
148
``` python
154
149
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
156
151
# # Correct pattern:
157
152
l.append(4 )
158
- print l # # [1, 2, 3, 4]
153
+ print (l) # # [1, 2, 3, 4]
159
154
```
160
155
161
156
### List Build Up
@@ -176,9 +171,9 @@ change sub-parts of the list.
176
171
177
172
``` python
178
173
l = [' a' , ' b' , ' c' , ' d' ]
179
- print l[1 :- 1 ] # # ['b', 'c']
174
+ print ( l[1 :- 1 ]) # # ['b', 'c']
180
175
l[0 :2 ] = ' z' # # replace ['a', 'b'] with ['z']
181
- print l # # ['z', 'c', 'd']
176
+ print (l) # # ['z', 'c', 'd']
182
177
```
183
178
184
179
Exercise: list1.py
0 commit comments