lists

10
[ lists ]

Upload: salma-subh

Post on 13-May-2015

211 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Lists

[ lists ]

Page 2: Lists

Defining Lists

Page 3: Lists

* First, you define a list of five elements. Note that they retain their original order. This is not an accident. A listis an ordered set of elements enclosed in square brackets.** A list can be used like a zero−based array. The first element of any non−empty list is always names [0].*** The last element of this five−element list is names [4], because lists are always zero−based.

Page 4: Lists

Negative List Indices

Slicing a List

Page 5: Lists

Slicing Shorthand

* If the left slice index is 0, you can leave it out, and 0 is implied. So colours[:3] is the same as colours[0:3]** Similarly, if the right slice index is the length of the list, you can leave it out. So colours[3:] is the same as colours[3:5], because this list has five elements.

Page 6: Lists

Adding Elements to Lists

.append

.insert

.extend

append adds a single element to the end of the list.

insert inserts a single element into a list.

extend concatenates lists.

Page 7: Lists

example

Page 8: Lists

THE DIFFERENCE BETWEEN EXTEND AND APPEND

Page 9: Lists
Page 10: Lists

By \ salma subh

GOOD LUKE