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

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
The University of Texas – Pan American
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
More Looping Structures
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Introduction. 2COMPSCI Computer Science Fundamentals.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Built-in Data Structures in Python An Introduction.
Introduction to Computer Programming
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Chapter 6: Loops.
Tuples and Lists.
Lecture 7: Repeating a Known Number of Times
From Think Python How to Think Like a Computer Scientist
Numbering System TODAY AND TOMORROW 11th Edition
Chapter 8 Arrays Objectives
Programming Fundamentals
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Arrays, For loop While loop Do while loop
CMSC 120: Visualizing Information 3/25/08
Lists in Python.
Chapter 8 Arrays Objectives
1) C program development 2) Selection structure
Chapter (3) - Looping Questions.
Programming Training Main Points:
String and Lists Dr. José M. Reyes Álamo.
More Looping Structures
CS2011 Introduction to Programming I Arrays (I)
Topics Sequences Introduction to Lists List Slicing
CS1110 Today: collections.
Chapter 8 Arrays Objectives
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Introduction to Strings
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
More Looping Structures
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Introduction to Computer Science
Presentation transcript:

Sequences CMSC 120: Visualizing Information 2/26/08

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

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)

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)

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

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

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

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

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 :

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

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

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

Types of Lists Empty 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( ) : 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, , 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 index First element has an index of 0 Last 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 indices List[a:b] returns the elements from a to b-1

Lists Operations >>> Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin'] >>> 'Troy' in Cities True >>> 'Cairo' in Cities False 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 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)

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

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, …]

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

Lists in Pylab

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

Lists in Pylab