Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences CMSC 120: Visualizing Information 2/26/08.

Similar presentations


Presentation on theme: "Sequences CMSC 120: Visualizing Information 2/26/08."— Presentation transcript:

1 Sequences CMSC 120: Visualizing Information 2/26/08

2 Step by Step Algorithm: 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

3 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)

4 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 C.draw(win) R.draw(win) L.draw(win)

5 The Alternative Sequential Processing Perception Programming Parallel Processing Perception Programming Multiple CPUs Control Flow Statements

6 Control Flow Alter the direction of the flow of the code No longer sequential Loops: repetition Conditionals: make decisions Gotos: jump to another section

7 Repetition Print out the numbers from 0 to 9 for-statement loop statement Repeat something a fixed number of times >>> for i in range(10): print i

8 for-Statements for in :... Sets up number of times the loop is repeated or iterated Body: stuff to be repeated

9 for-Statements is the index for the loop Assign successive values of to is a list of values Statements in the body are executed for each value for in :

10 Repetition Print out the numbers from 0 to 9 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

11 Repetition Print 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 01234567890123456789

12 Lists Collections of information Any sequence of “things” Numbers Letters Strings Points figures

13 Types of Lists Empty List >>> [] [] or >>> L = [] >>> print L [] An empty list does not contain anything

14 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

15 Lists Operations >>> N = [2, 43, 6, 27] >>> len(N) 4 len( ) : takes a list and returns the length, or the number of objects in the list

16 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

17 List Operations >>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin'] >>> Cities[0] 'Philadelphia' Indexing: allows access individual elements in a list by their index First element has an index of 0 Last element has an index of n-1 where n is the length of the list

18 Lists Operations >>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin'] >>> Cities[1:3] ['Hicksville', 'Troy'] Slice: allows access to a sublist by a set of indices List[a:b] returns the elements from a to b-1

19 Lists Operations >>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin'] >>> 'Troy' in Cities True >>> 'Cairo' in Cities False in: checks whether an element is in a list

20 Lists are Objects >>> N = [2, 43, 6, 27] >>> N.sort() >>> print N [2, 6, 27, 43] sort: arrange elements in ascending order

21 Lists are Objects >>> N = [2, 43, 6, 27] >>> N.reverse() >>> print N [27, 6, 43, 2] reverse: reverse order of elements

22 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

23 All Lists are Sequences All lists can be used to perform repetitions from 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)

24 Strings Strings are also sequences >>> ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> len(ABC) 26 >>> for letter in ABC: print letter A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

25 Lists in Pylab All 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, …]

26 Lists in Pylab Thus, 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( ) Plot(, ) plot( ) Plot(, ) mean( )

27 Lists in Pylab

28 Ticks: Labels = ['Marijuana', 'Hallucinogens', 'Cocaine', 'Heroin', 'Cigarettes', 'Alcohol'] xticks(arange(6), Labels) xticks(arange( ), )

29 Lists in Pylab


Download ppt "Sequences CMSC 120: Visualizing Information 2/26/08."

Similar presentations


Ads by Google