Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Microsoft Office Word is an example of ____ software. a. Database b
24-Aug-14 HTML Forms. 2 What are forms? is just another kind of HTML tag HTML forms are used to create (rather primitive) GUIs on Web pages Usually the.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Arrays.
Container Types in Python
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Python Basics: Statements Expressions Loops Strings Functions.
Order of Operations And Real Number Operations
How do Methods Work?. Let’s write a method that adds two integer values together and returns the result.
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Sequences The range function returns a sequence
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Lists in Python.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Python Functions.
What on Earth? LEXEMETOKENPATTERN print p,r,i,n,t (leftpar( 4number4 *arith* 5number5 )rightpar) userAnswerID Letter followed by letters and digits “Game.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Compsci 101.2, Fall Plan for October l Review Catchup and Midterm and Future  Make sure everyone understand options l Review Assignment.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Python Variable Types.
Introduction to Python
When to use Tuples instead of Lists
CS-104 Final Exam Review Victor Norman.
CISC101 Reminders Quiz 2 this week.
File Handling Programming Guides.
Topics Introduction to File Input and Output
Ch2: Data Representation
References.
CS190/295 Programming in Python for Life Sciences: Lecture 3
CS 177 Week 3 Recitation Slides
CISC101 Reminders Assignment 2 due today.
Arrays Arrays A few types Structures of related data items
Python Review
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Topics Introduction to File Input and Output
Presentation transcript:

Sections of Ch. 12, 13, 14 Prof. V. Norman PhD, MS, BS, DDS, Esq.

Tuples Q: I don’t understand the difference between a tuple and a list. Can you explain it? A: A tuple is essentially an immutable list. (Like a string, which is an immutable list of characters.) Once the tuple has been created, it can’t be changed.

Tuple creation Q: Is any bunch of values separated by commas a tuple? The book said they're usually inside parentheses, but that the parentheses are unnecessary; so why would people put the parentheses in? A: The comma operator is really the tuple creation operator, but sometimes it is necessary to use the parentheses. E.g., to pass a tuple literal to a function: s = function( ( ‘a’, ‘tuple’, ‘of’, ‘strings’ ) )

What good are tuples? Q: What good are tuples? Why use them? A: Tuples are not used that much… Some things seem natural as a tuple, like an (x, y) coordinate, or an (r, g, b) color. (Tuples have to be used when using dictionary keys, as keys have to be immutable.) Note: lists are more versatile, because they can be modified.

Tuple example Suppose x and y coordinates of a Scribbler need to be stored in a variable. You could store them as a tuple. Then, you can do a “transformation” on the point, by multiplying it by a factor: def transform(point, factor): return (point[0] * factor, point[1] * factor) orig_coord = (30, 50) # create tuple new_coord = transform(orig_coord, 0.5)

More on Tuples Q: Is there any reason to create single element tuples? A: IMO, no. Q: Doesn’t the last example of 12.1 modify a tuple? A: No: it creates a new tuple and then makes t refer to it. Q: Can a tuple only hold single-character strings? A: No, they can hold anything of any type, like a list can.

Tuple Assignment Q: Can you give more tuple assignment examples? A: Sure… coords = (3, 4) # a tuple with 2 values x, y = coords # x and y created and given # values from coords r, g, b = getColor(pixel) # getColor returns tuple color = (r, g, b) # put them back in a tuple

Tuple as a parameter # Assume we have a pixel type for which # you can set its color. def setColor(pixel, aTuple): pixel.setRed(aTuple[0]) pixel.setGreen(aTuple[1]) pixel.setBlue(aTuple[2]) setColor(myPixel, (30, 255, 192))

Optional Parameters Terminology Required Parameter: a parameter for which a value must be supplied by the caller. Optional Parameter: a parameter in a function’s parameter list that the caller does not have to supply a value for. Default value: the value the code provides for the optional parameter in the function definition. It is used when the caller does not supply a value.

Optional Parameter Example def setColor(pixel, r=0, g=0, b=0): pixel.setColor( (r, g, b) ) setColor(pixel, 255) # full red only setColor(pixel) # (0, 0, 0) is black setColor(pixel, 0, 255, 0) # green setColor(pixel, 255, 255, 255) # white

File operations Q: Why when one opens a file using the write method does all the data in that file get deleted? A: If you open a file for writing, the file contents are wiped out. If you open a file for reading, nothing happens, and you can’t write to it. You can open a file for writing and appending, in which case you don’t wipe out the contents. Prof. Norman to provide examples here…

More useful file operations Can read entire file line by line: f = open(“aFile.txt”) # read-mode for line in f: # do stuff with this line # etc f.close()

More useful file operations Or: f = open(“aFile.txt”) lines = f.readlines() # lines is a list of lines from the file. Or: f = open(“aFIle.txt”) contents = f.read() # entire file read in.

Format Operator Pattern: % Format string contains format sequences: – %d : format number as an integer – %f or %g: format number as a float – %s : format number as a string – %x : format number as an integer in hexadecimal notation

Format operator Format sequence can contain modifiers to describe exactly how to print something out: – %7.3f : format float as using 7 columns, with 3 behind the decimal point – %-8s : format string as using 8 columns, left justified Without this, there is no way to print out stuff exactly as you want it.

Format examples print “girder id: %d, weight %10.5f” % (17, ) Produces : girder id: 17, weight print “Scribbler info: (%d, %d), angle %6.1f” % (x, y, angle) When called with (30, 20, -145) Scribbler info: (30, 20), angle