Professional Documents
Culture Documents
Programming
• Algorithm
- A set of rules or steps used to solve a problem
• Data Structure
- A particular way of organizing data in a computer
https://en.wikipedia.org/wiki/Algorithm
https://en.wikipedia.org/wiki/Data_structure
What is Not a “Collection”?
Most of our variables have one value in them - when we put a new
value in the variable, the old value is overwritten
$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of Collection
• A collection allows us to put many values in a single “variable”
Just like strings, we can get at any single element in a list using an
index specified in square brackets
http://docs.python.org/tutorial/datastructures.html
Building a List from Scratch
>>> stuff = list()
• We can create an empty list >>> stuff.append('book')
and then add elements using >>> stuff.append(99)
the append method >>> print(stuff)
['book', 99]
• The list stays in order and >>> stuff.append('cookie')
new elements are added at >>> print(stuff)
['book', 99, 'cookie']
the end of the list
Is Something in a List?
• Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
in a list True
>>> 15 in some
False
• These are logical operators >>> 20 not in some
that return True or False True
>>>
• They do not modify the list
Lists are in Order
• A list can hold many
items and keeps those
items in the order until >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
we do something to >>> friends.sort()
change the order >>> print(friends)
['Glenn', 'Joseph', 'Sally']
• A list can be sorted >>> print(friends[1])
Joseph
(i.e., change its order) >>>
• The sort method
(unlike in strings)
means “sort yourself”
Built-in Functions and Lists
>>> nums = [3, 41, 12, 9, 74, 15]
• There are a number of >>> print(len(nums))
functions built into Python 6
that take lists as >>> print(max(nums))
parameters 74
>>> print(min(nums))
3
• Remember the loops we
>>> print(sum(nums))
built? These are much 154
simpler. >>> print(sum(nums)/len(nums))
25.6
total = 0 Enter a number: 3
count = 0
while True : Enter a number: 9
inp = input('Enter a number: ') Enter a number: 5
if inp == 'done' : break
value = float(inp) Enter a number: done
total = total + value Average: 5.66666666667
count = count + 1
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'] ● When you do not specify a
>>>
>>> line = 'first;second;third' delimiter, multiple spaces are
>>> thing = line.split()
>>> print(thing) treated like one delimiter
['first;second;third']
>>> print(len(thing))
1 ● You can specify what delimiter
>>> thing = line.split(';')
>>> print(thing) character to use in the splitting
['first', 'second', 'third']
>>> print(len(thing))
3
>>>
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
words = line.split()
email = words[1]
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] stephen.marquard@uct.ac.za
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] stephen.marquard@uct.ac.za
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
The Double Split Pattern
words = line.split()
email = words[1] stephen.marquard@uct.ac.za
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print(pieces[1]) 'uct.ac.za'
List Summary
• Concept of a collection • Slicing lists
write a program that defines a list of numbers asks the user for a set of
numbers and stores them in the list, then find the median value
the median value is the value that is less than half the numbers in the set is
the values at the midpoint when the set of values is sorted, otherwise the
median is the average of the two values surrounding the midpoint.