Watch Me Code 1: List Basics


In [1]:
colors = [] #empty list

In [2]:
type(colors)


Out[2]:
list

In [3]:
dir(colors)


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

In [3]:
help(colors.index)


Help on built-in function index:

index(...) method of builtins.list instance
    L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.


In [5]:
colors.append("orange")
colors.append("blue")
colors.append("green")
colors.append("white")
print(colors)
print(len(colors))


['orange', 'blue', 'green', 'white']
4

In [6]:
colors.remove("green")
print(colors)


['orange', 'blue', 'white']

In [7]:
#lists are mutable
colors.reverse()
colors


Out[7]:
['white', 'blue', 'orange']

In [8]:
index = colors.index('blue')
print(index)


Out[8]:
1

In [6]:
colors.index('brown') # ValueError


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-8737937d04fe> in <module>()
----> 1 colors.index('brown') # ValueError

ValueError: 'brown' is not in list

In [ ]: