CS-104 Final Exam Review Victor Norman.

Slides:



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

Container Types in Python
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Strings Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
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.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CSC 212 Object-Oriented Programming and Java Part 2.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Python Let’s get started!.
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.
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
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.
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.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
G. Pullaiah College of Engineering and Technology
Topics Designing a Program Input, Processing, and Output
Classes and Objects – digging deeper
COMPSCI 107 Computer Science Fundamentals
CSc 120 Introduction to Computer Programing II Adapted from slides by
Sequences and Indexing
Python Let’s get started!.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CS 115 Lecture 8 Structured Programming; for loops
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CS 302 Week 11 Jim Williams, PhD.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Object Oriented Programming (OOP) LAB # 8
Topics Introduction to File Input and Output
CS190/295 Programming in Python for Life Sciences: Lecture 6
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
Python Primer 1: Types and Operators
Basic String Operations
EECE.2160 ECE Application Programming
Topics Designing a Program Input, Processing, and Output
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Topics Basic String Operations String Slicing
Topics Designing a Program Input, Processing, and Output
EECE.2160 ECE Application Programming
Topics Sequences Introduction to Lists List Slicing
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Python Review
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
EECE.2160 ECE Application Programming
Topics Basic String Operations String Slicing
Introduction to Computer Science
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

CS-104 Final Exam Review Victor Norman

Overview Multiple choice – 55 questions. Lots of “Is there a syntax error in the following code?” questions. Covers everything from 1st day to last day.

Topics that will be covered Assignment statements Arithmetic expressions For loops, with range() Function definitions Function calls and method calls. Accumulator pattern boolean expressions while loops if statements, if-else, if-elif-else, etc. creating lists, indexing, slicing, searching with “in”, appending. tuples: creating, indexing, assigning. strings: creating, indexing, concatenating, etc. class definitions Object-oriented programming terminology: classes, objects, instances, subclass, superclass, instance variable, attribute, method, self, is-a, has-a, inherits, constructor… Implementing __str__, __init__, __add__, etc. Ability to follow logic of code. mutable/immutable types. Optional parameters with default values.

Assignment Statements, Arithmetic Expr. variable-on-left = expression-on-right variable must have proper format: letters, digits, underscores not starting with digit. value + value, or -, *, /, //, %, ** produces an integer or float

For loops and range() for loopVar in sequence: statements (aka body) loopVar is set to first item in the sequence, then 2nd item, then 3rd, etc. range(start, stop, step): generates a sequence of integers starting at start, and stopping before stop, going up by step each time. start is optional: uses 0. step is optional: uses 1.

Item-based vs. index-based loops. for item in sequence: (e.g., list or string) item will refer to the item in the sequence each time. Won’t be able to tell where in the sequence the item is. for idx in range(len(sequence)): range(len(sequence)) converts sequence into a list of indices to index into the sequence from beginning to end. use sequence[idx] in the loop.

Functions Before calling a function, you have to define it. Functions usually return a computed value. Or don’t, but make the Scribbler move, or print something. You must either store the result or print it or use it for something! e.g., y = sin(x) is good. sin(x) is not useful. When calling a function, you are passing values in. The function refers to those values with its parameters.

Function definitions def funcName(parameter-list): statements (body of the function) parameters must be legal identifiers (like variables). parameters refer to the values being passed in from the caller. statements compute some value. May use temporary variables. usually return some computed value at the end. parameters and variables defined in the function do not exist after the function call returns.

Accumulator pattern Very common pattern in programming, with this general format: accVar = 0 (or [ ] or 1 or “”) for loopVar in sequence: if something(loopVar): accVar = accVar someoperator loopVar # e.g., accVar = accVar + loopVar Example: given list of values, create a string that has all of the values concatenated together: def stringify(aList): res = “” for item in aList: res = res + str(item) return res

Boolean expressions <somevalue> == <somevalue> or !=, <, >, <=, >=. results in a True or False value can combine with logical operators: and, or, not similar to and, or, and not gates in ENGR. have to have a true-blue boolean expression on each side of and or or. x < y and x > z Can’t do x < y and > z

while loops while booleanExpression : statements Loops as long as booleanExpression is True – an indeterminate number of times. If in the loop you do not change some variable being used in the expression, the while loop will loop infinitely.

break and continue Only used inside of a loop – for loop or while loop – and almost only ever useful inside of a if statement (or else/elif) in a loop. break transfers control to the statement after the loop: the loop is “broken out of” and the code continues executing after the loop. continue transfer control back up to the top of the loop: to the next iteration of the loop.

if statements if booleanExpression: statements # one-way decision: do this or do nothing. statements # two-way decision: do this or do the other. else: statements statements # n-way decision: do one of the things that is true elif booleanExpression: statements … else: # optional statements

Lists anyoneWannaPlay = [ 10, “s”, “n”, “e”, 1 ] Hold multiple items of any types in order. Can be indexed, starting at 0, and going to n-1, or using negatives. anyoneWannaPlay[2] is ”n” Is mutable: can be changed after creation: anyoneWannaPlay[-1] = 2 Methods on it: append(), sort(), remove(), pop(), insert(), etc. All methods above return None, except pop, which returns the item that was removed.

Lists (continued) Can search if an item is in the list with in: if 17 in listofNums: Can do slicing on it to get a new list from the list: anyoneWannaPlay = [ 10, “s”, “n”, “e”, 1 ] print(anyoneWannaPlay[0:2] + anyoneWannaPlay[3:])

Tuples Just like a list, but immutable: cannot change after creation. Not used often, but can be useful for representing x, y coordinates, r, g, b colors, etc. Create with parens and , : color = (127, 33, 33) Can break a tuple apart with multiple assignment: red, green, blue = color # red is 127, green 33, blue 33.

strings Like a list, but contains only single characters. Create with single, double, triple-single, or triple-double quotes. name = ”Jared” (or ‘Jared’ or ‘’’Jared’’’ or “””Jared”””) Can be indexed (from 0 to n-1) to get a single character or can be sliced: nickname = name[0:2] Immutable: every method that seems to modify it actually creates a new string: capitalize(), upper(), lower(), etc. Can search with in. Can concatenate with other strings: print(name + “ Jones”)

classes A class definition defines a new type. class Student: Contains the functionality (methods) that can be done on an instance of the class. Contains the data the objects contains. class Student: def __init__(self, name, id): self._name = name self._id = id mary = Student(“Mary”, 123456789)

getters, setters Typically created for each instance variable: def get_name(self): return self._name def set_name(self, new_name): self._name = new_name Similarly for self._id A.k.a. accessors and mutators

self An object refers to itself as self in code in the class definition. The object has another name in code outside of the class definition. joe = Student(“Joe Jones”, 234567890) joe.set_name(“Joseph Jones”) set_name() code refers to the object as self.

Overriding built-in functionality A class can define __str__ so that when an object is “string-ified”, this method is called. print() calls str(val) for each value it needs to print. __str__ must return a string representation of the object. You can implement __add__() if you want to allow the user to add two objects of your class together.

Optional parameters with default values Often a constructor is defined such that you can provide no values and get default values used, or you can provide initial values: class Student: def __init__(self, name=“Namey McNameface”, id=0): self._name = name self._id = id noname = Student() hiro = Student(“Hiro Nakayama”, 345678901)

Inheritancce You should know what it means and why it is useful. You should know how you code it up. Know how to call the super-class constructor.