Saturday, March 24, 2018

Basic List Operations


Basic List Operations in Python 3

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the Strings chapter.


Python Expression
Results
Description
len([1, 2, 3])
3
Length
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
Concatenation
[‘Hi!’] * 3
[‘Hi!’, ‘Hi!’, ‘Hi!’,]
Repetition
3 in [1, 2, 3]
True
Membership
For x in [1,2,3] : print (x,end=’’)
1 2 3
iteration




<<< Lists                                   Index                                   Indexing, Slicing & Matrixes >>>