The object itself (number, string, or otherwise) is usually changed
only when you explicitly set the object??™s label (or pointer) to the new value, as follows:
>>> n1 = 5
>>> n1 ** 2 # Display value of 5^2
25
>>> n1 # n1, however is still set to 5
5
>>> n1 = n1 ** 2 # Set n1 = 5^2
>>> n1 # Now n1 is set to 25
25
Lists
The next type of built-in object we??™ll cover is the list. You can throw any kind of object
into a list. Lists are usually created by adding [ and ] around an object or a group of
objects. You can do the same kind of clever ???slicing??? as with strings. Slicing refers to our
string example of returning only a subset of the object??™s values, for example, from the
fifth value to the tenth with label1[5:10]. Let??™s demonstrate how the list type works:
>>> mylist = [1,2,3]
>>> len(mylist)
3
>>> mylist*4 # Display mylist, mylist, mylist, mylist
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 1 in mylist # Check for existence of an object
True
>>> 4 in mylist
False
>>> mylist[1:] # Return slice of list from index 1 and on
[2, 3]
>>> biglist = [['Dilbert', 'Dogbert', 'Catbert'],
... ['Wally', 'Alice', 'Asok']] # Set up a two-dimensional list
>>> biglist[1][0]
'Wally'
>>> biglist[0][2]
'Catbert'
>>> biglist[1] = 'Ratbert' # Replace the second row with 'Ratbert'
>>> biglist
[['Dilbert', 'Dogbert', 'Catbert'], 'Ratbert']
>>> stacklist = biglist[0] # Set another list = to the first row
>>> stacklist
['Dilbert', 'Dogbert', 'Catbert']
>>> stacklist = stacklist + ['The Boss']
>>> stacklist
['Dilbert', 'Dogbert', 'Catbert', 'The Boss']
>>> stacklist.
Pages:
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306