tutorial1 list



In [1]:
fruits = ['apple','orange','pear','banana']

In [2]:
fruits[0]


Out[2]:
'apple'

In [3]:
otherFruits=['kiwi','strawberry']
fruits + otherFruits


Out[3]:
['apple', 'orange', 'pear', 'banana', 'kiwi', 'strawberry']

In [4]:
fruits[-2]


Out[4]:
'pear'

In [5]:
fruits.pop()


Out[5]:
'banana'

In [6]:
fruits


Out[6]:
['apple', 'orange', 'pear']

In [7]:
fruits.append('grapefruit')

In [8]:
fruits


Out[8]:
['apple', 'orange', 'pear', 'grapefruit']

In [9]:
fruits[-1] = 'pineapple'

In [10]:
fruits


Out[10]:
['apple', 'orange', 'pear', 'pineapple']

In [11]:
fruits[0:2]


Out[11]:
['apple', 'orange']

In [12]:
fruits[2:]


Out[12]:
['pear', 'pineapple']

In [14]:
fruits[:3]


Out[14]:
['apple', 'orange', 'pear']

In [15]:
len(fruits)


Out[15]:
4

In [16]:
lstOfLsts = [['a','b','c'],[1,2,3],['one','two','three']]

In [18]:
lstOfLsts[1][2]


Out[18]:
3

In [19]:
lstOfLsts[0].pop()


Out[19]:
'c'

In [20]:
lstOfLsts


Out[20]:
[['a', 'b'], [1, 2, 3], ['one', 'two', 'three']]

In [21]:
dir(list)


Out[21]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__delslice__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__setslice__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

In [28]:
help(list.reverse)


Help on method_descriptor:

reverse(...)
    L.reverse() -- reverse *IN PLACE*

In [4]:
lst=['a','b','c']

In [5]:
lst.reverse()

In [6]:
lst


Out[6]:
['c', 'b', 'a']

In [ ]: