sequences cmsc 120: visualizing information 2/26/08
DESCRIPTION
Graphics Window Step by Step def drawStuff(P1, P2, P3, win): # create a line L = Line(P2, P3) # create a circle C = Circle(P3, 10) C.setFill('red') # create a rectangle R = Rectangle(P1, P3) R.setFill('blue') # draw the objects L.draw(win) R.draw(win) C.draw(win)TRANSCRIPT
Sequences
CMSC 120: Visualizing Information2/26/08
Step by StepAlgorithm: a set of sequential instructions that are
followed to solve a problem
Computer programs are algorithms that provide a set of sequential instructions to the CPU
CPU processes instructions one at a time, in the order that they are received; i.e., sequentially
Graphics Window
Step by Stepdef drawStuff(P1, P2, P3, win):
# create a lineL = Line(P2, P3)
# create a circleC = Circle(P3, 10)C.setFill('red')
# create a rectangleR = Rectangle(P1, P3)R.setFill('blue')
# draw the objectsL.draw(win)R.draw(win)C.draw(win)
Graphics Window
Step by Stepdef drawStuff(P1, P2, P3, win):
# create a lineL = Line(P2, P3)
# create a circleC = Circle(P3, 10)C.setFill('red')
# create a rectangleR = Rectangle(P1, P3)R.setFill('blue')
# draw the objectsC.draw(win)R.draw(win)L.draw(win)
The AlternativeSequential Processing
PerceptionProgramming
Parallel ProcessingPerceptionProgrammingMultiple CPUs
Control Flow Statements
Control FlowAlter the direction of the flow of the code
No longer sequential
Loops: repetition
Conditionals: make decisions
Gotos: jump to another section
RepetitionPrint out the numbers from 0 to 9
for-statementloop statementRepeat something a fixed number of times
>>> for i in range(10): print i
for-Statements
for <variable> in <seqeunce>: <do something> <do something> ...
Sets up number of times the loop is repeated or iterated
Body: stuff to be repeated
for-Statements
<variable> is the index for the loop
Assign successive values of <sequence> to <variable><sequence> is a list of values
Statements in the body are executed for each value
for <variable> in <seqeunce>:
RepetitionPrint out the numbers from 0 to 9
<sequence> is range(10)generates a sequence of numbers up to but not
including the specified parameter value>>> range(5)[0, 1, 2, 3, 4]
>>> for i in range(10): print i
RepetitionPrint out the numbers from 0 to 9
The index variable, i will take on the values of 0 through 9 and for each value, it will execute the print statement
>>> for i in range(10): print i
0123456789
ListsCollections of information
Any sequence of “things”NumbersLettersStringsPointsfigures
Types of ListsEmpty List
>>> [][]
or
>>> L = []>>> print L[]
An empty list does not contain anything
Types of Lists
N = [2, 43, 6, 27]FamousNumbers = [3.1415, 2.718, 42]Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]MyCar = ['Mazda Protege', 1999, 'red']
Any collection of things, even those with different data types
Lists Operations
>>> N = [2, 43, 6, 27]>>> len(N)4
len(<sequence>): takes a list and returns the length, or the number of objects in the list
Lists Operations
>>> N = [2, 43, 6, 27]>>> FamousNumbers = [3.1415, 2.718, 42]>>> N + FamousNumbers[2, 43, 6, 27, 3.1415, 2.718, 42]
Concatenate (‘+’): joins two lists together
List Operations
>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']
>>> Cities[0]'Philadelphia'
Indexing: allows access individual elements in a list by their indexFirst element has an index of 0Last element has an index of n-1 where n is the length
of the list
Lists Operations
>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']
>>> Cities[1:3]['Hicksville', 'Troy']
Slice: allows access to a sublist by a set of indicesList[a:b] returns the elements from a to b-1
Lists Operations
>>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']
>>> 'Troy' in CitiesTrue>>> 'Cairo' in CitiesFalse
in: checks whether an element is in a list
Lists are Objects
>>> N = [2, 43, 6, 27]>>> N.sort()>>> print N[2, 6, 27, 43]
sort: arrange elements in ascending order
Lists are Objects
>>> N = [2, 43, 6, 27]>>> N.reverse()>>> print N[27, 6, 43, 2]
reverse: reverse order of elements
Lists are Objects
>>> N = [2, 43, 6, 27]>>> N.append(3)>>> print N[2, 43, 6, 27, 3]
append: add an element to the end of the list
All Lists are SequencesAll lists can be used to perform repetitionsfrom graphics import *
MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]
win = GraphWin('My Win', 400, 400)for point in MyPoints: c = Circle(point, 5) c.setFill('red') c.draw(win)
StringsStrings are also sequences
>>> ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'>>> len(ABC)26>>> for letter in ABC:
print letter
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Lists in PylabAll the data we have plotted so far has been stored
as a set of lists:
Year = [1979, 1980, 1982,…]Marijuana = [14.1, 13.3, 13.3, 12.5, …]
Lists in PylabThus, the actual syntax of the plot command is:
where the x- and y-data are actually lists of either int or float values
The syntax of the mean command is:
And the function calculates the mean of a list of int or float values
plot(<x-sequence>)Plot(<x-sequence>, <y-sequence>)
mean(<sequence>)
Lists in Pylab
Lists in PylabTicks:
Labels = ['Marijuana', 'Hallucinogens', 'Cocaine', 'Heroin', 'Cigarettes', 'Alcohol']
xticks(arange(6), Labels)
xticks(arange(<num-ticks>), <label-sequence>)
Lists in Pylab