# 10/16/13

## Lists a new type of sequence

``````

In [3]:

ages = [ 23, 45, 19, 48 ]
items = [ 5, 'dog', False, 7.34, None, type, raw_input ]
for item in items:
print item

``````
``````

5
dog
False
7.34
None
<type 'type'>
<function <lambda> at 0x02FECDF0>

``````
``````

In [5]:

print len(items)
print items[1]

``````
``````

7
dog

``````
``````

In [6]:

x = items[1:4]
for item in x:
print item

``````
``````

dog
False
7.34

``````
``````

In [7]:

print x[0]
x[0] = 'pie'
print x[0]

``````
``````

dog
pie

``````
``````

In [8]:

print len(x)
x.append(17)
print len(x)

``````
``````

3
4

``````
``````

In [10]:

x.remove(17)
print len(x)

``````
``````

3

``````
``````

In [12]:

numbers = [5, 6, 9,2, 0,10]
print numbers
numbers.sort()
print numbers

``````
``````

[5, 6, 9, 2, 0, 10]
[0, 2, 5, 6, 9, 10]

``````
``````

In [13]:

names = ['tom','bob','apple','jack']
print names
names.sort()
print names

``````
``````

['tom', 'bob', 'apple', 'jack']
['apple', 'bob', 'jack', 'tom']

``````
``````

In [14]:

many_lists = [ [ 1, 2,3 ], ['dog', 'cat', 'bird'], [9.7,3.4] ]
print many_lists[0]
print len(many_lists)

``````
``````

[1, 2, 3]
3

``````
``````

In [16]:

total = 0
for l in many_lists:
if type(l) == list:
total = total + len(l)
print total

``````
``````

8

``````
``````

In [19]:

new_list = []
new_list.append(5)
new_list.append('cat')

print new_list

``````
``````

[5, 'cat']

``````
``````

In [20]:

new_list = list()
print new_list

``````
``````

[]

``````
``````

In [21]:

a = []
b = [5,3]

print bool(a)
print bool(b)

``````
``````

False
True

``````
``````

In [22]:

items = [1 , 5, 7]
if 5 in items:
print "5 is in there"

``````
``````

5 is in there

``````
``````

In [23]:

name = raw_input("What is your name? ")
if name == 'Fred':
print "You are on the list"
elif name == 'Tome':
print "You are on the list"
elif name == 'Jane':
print "You are on the list"
elif name == 'Susan':
print "You are on the list"
else:
print "You are NOT on the list"

``````
``````

You are NOT on the list

``````

Can be simplified as

``````

In [24]:

allowed = ["Fred", "Tome","Jane","Susan"]
name = raw_input("What is your name? ")
if name in allowed:
print "You are on the list"
else:
print "You are NOT on the list"

``````
``````

You are on the list

``````
``````

In [25]:

print range(5)

``````
``````

[0, 1, 2, 3, 4]

``````
``````

In [26]:

print range(1,5)

``````
``````

[1, 2, 3, 4]

``````
``````

In [28]:

print range(1,6,2)

``````
``````

[1, 3, 5]

``````
``````

In [29]:

for n in range(5):
print n

``````
``````

0
1
2
3
4

``````
``````

In [29]:

``````
``````

In [31]:

word = "whifflebat"
for letter in word:
print letter
for i in range( len(word) ):
print word[i]

``````
``````

w
h
i
f
f
l
e
b
a
t
w
h
i
f
f
l
e
b
a
t

``````
``````

In [32]:

print [1,2,3] + [4, 5,6]
print ['a','b'] * 3

``````
``````

[1, 2, 3, 4, 5, 6]
['a', 'b', 'a', 'b', 'a', 'b']

``````
``````

In [34]:

print ( ['a','b'] * 3 )[2:5]

``````
``````

['a', 'b', 'a']

``````
``````

In [35]:

test = [raw_input,]
test[0]("Did this work?")

``````
``````

Did this work?yes

Out[35]:

'yes'

``````
``````

In [41]:

print x
del x[0]
print x

``````
``````

[False, 7.34]
[7.34]

``````
``````

In [42]:

x.append(5)
print x
x.pop()
print x

``````
``````

[7.34, 5]
[7.34]

``````
``````

In [43]:

x.append(4)
x.append(7)
print x
x.remove(4)
print x

``````
``````

[7.34, 4, 7]
[7.34, 7]

``````
``````

In [45]:

word = 'kite'
letters = list(word)
print letters

``````
``````

['k', 'i', 't', 'e']

``````
``````

In [ ]:

``````