We can create a new list by adding two existing lists together
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41, 12]
>>> t[:4]
[9, 41, 12, 3]
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Remember: Just like in strings, the second number is “up to but not including”
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'inser', 'pop', 'remove',
'reverse', 'sor']
https://docs.python.org/3/tutorial/datastructures.html
>>> stuff = list()
>>> stuff.append('book')
>>> studd.append(99)
>>> print(stuff)
['book', 99]
>>> stuff.append('cookies')
>>> print(stuff)
['book', 99, 'cookie']
>>> some = [1, 9, 21, 10, 16]
>>> 9 in some
True
>>> 15 in some
False
>>> 20 not in some
True
>>> friends = ['Joseph', 'Glenn', 'Sally']
>>> friends.sort()
>>> print(friends)
['Glenn', 'Joseph', 'Sally']
Joseph
>>> nums = [3, 41, 12, 9, 74, 15]
>>> print(len(nums))
6
>>> print(max(nums))
74
>>> print(min(nums))
3
>>> print(sum(nums))
154
>>> print(sum(nums)/len(nums))
25.6
numlist = list()
while True:
inp = input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)
average = sum(numlist) / len(numlist)
print('Average:', average)
>>> abc = 'With three words'
>>> stuff = abc.split()
>>> print(stuff)
['With', 'three', 'words']
>>> print(len(stuff))
3
>>> print(stuff[0])
With
Split breaks a string into parts and produces a list of strings. We think of these as words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces']
>>>
>>> line = 'first;second;third'
>>> thing = line.split()
>>> print(thing)
['first;second;third']
>>> print(len(thing))
1
>>> thing = line.split(';')
>>> print(thing)
['first', 'second', 'third']
>>> print(len(thing))
3
From stephen.marquard@uct.ac.za Sat Jan 5:09:14:16 2008
fhand = open('mbox-short.txt')
for line in fhand:
line = line.rstrip()
if not line.startswitch('From ') : continue
words = line.split()
print(words[2])
From stephen.marquard@uct.ac.za Sat Jan 5:09:14:16 2008
words = line.split()
email = words[1]
pieces = email.split('@')
print(pieces[1])
>>> stephen.marquard@uct.ac.za
>>> ['stephen.marquard', 'uct.ac.za']
>>> 'uct.ac.za'