In [3]:
ages = [ 23, 45, 19, 48 ]
items = [ 5, 'dog', False, 7.34, None, type, raw_input ]
for item in items:
print item
In [5]:
print len(items)
print items[1]
In [6]:
x = items[1:4]
for item in x:
print item
In [7]:
print x[0]
x[0] = 'pie'
print x[0]
In [8]:
print len(x)
x.append(17)
print len(x)
In [10]:
x.remove(17)
print len(x)
In [12]:
numbers = [5, 6, 9,2, 0,10]
print numbers
numbers.sort()
print numbers
In [13]:
names = ['tom','bob','apple','jack']
print names
names.sort()
print names
In [14]:
many_lists = [ [ 1, 2,3 ], ['dog', 'cat', 'bird'], [9.7,3.4] ]
print many_lists[0]
print len(many_lists)
In [16]:
total = 0
for l in many_lists:
if type(l) == list:
total = total + len(l)
print total
In [19]:
new_list = []
new_list.append(5)
new_list.append('cat')
print new_list
In [20]:
new_list = list()
print new_list
In [21]:
a = []
b = [5,3]
print bool(a)
print bool(b)
In [22]:
items = [1 , 5, 7]
if 5 in items:
print "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"
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"
In [25]:
print range(5)
In [26]:
print range(1,5)
In [28]:
print range(1,6,2)
In [29]:
for n in range(5):
print n
In [29]:
In [31]:
word = "whifflebat"
for letter in word:
print letter
for i in range( len(word) ):
print word[i]
In [32]:
print [1,2,3] + [4, 5,6]
print ['a','b'] * 3
In [34]:
print ( ['a','b'] * 3 )[2:5]
In [35]:
test = [raw_input,]
test[0]("Did this work?")
Out[35]:
In [41]:
print x
del x[0]
print x
In [42]:
x.append(5)
print x
x.pop()
print x
In [43]:
x.append(4)
x.append(7)
print x
x.remove(4)
print x
In [45]:
word = 'kite'
letters = list(word)
print letters
In [ ]: