Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.

Slides:



Advertisements
Similar presentations
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.
Advertisements

5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Geography 465 Assignments, Conditionals, and Loops.
Python Control of Flow.
Python quick start guide
4. Python - Basic Operators
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Announcements Additional office hours tomorrow: 3-6pm in HAAS 142 Midterm Bring a #2 pencil (test given via scantron sheet) Photo ID No Labs or Recitation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
I Power Higher Computing Software Development High Level Language Constructs.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
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.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Language Find the latest version of this document at
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Learning Javascript From Mr Saem
1 CS Review, iClicker -Questions Week 15. Announcements 2.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Information and Computer Sciences University of Hawaii, Manoa
G. Pullaiah College of Engineering and Technology
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python: Control Structures
Chapter 6 More Conditionals and Loops
Basic operators - strings
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Practice with loops! What is the output of each function below?
String and Lists Dr. José M. Reyes Álamo.
Statement-Level Control Structures
Computer Science Core Concepts
Chapter8: Statement-Level Control Structures April 9, 2019
CHAPTER 6: Control Flow Tools (for and while loops)
Introduction to Computer Science
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Class code for pythonroom.com cchsp2cs
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import math vs from math import *) String operations: len, ord, +, * Math operations (math.sqrt, %, /, //, **)

Material to Review Go over all your Pre Labs No questions on the Robot Specific commands Pre lab 6 NOT on the exam Go over all your Lab Solutions Lab 6 will NOT be covered by the exam Understand the results of the various clicker questions

Chapters Covered Chapter 1 – 3 Sections 5.1, 5.2, 5.4, 5.5 Chapter 6 Sections 7.1 – 7.3 Sections 8.1 – 8.3 NOTE: no graphics specific questions!

Python Wiki Book The Python Wiki Book provides very concise definitions and examples Good for review!

= vs == In Python we leverage “=“ for assignment Example: x = 5 This sets the variable x to the value 5 In Python we leverage “==“ to test equality Example: print(x==5) This will check if x is equal to 5 and then print True if it is, or False if it is not Typically used in conjunction with an if statement

Functions Functions allow us to “name” a region of code in a similar fashion that a variable allows us to name a value Functions provide us a mechanism to reuse code def name(input) : code to execute when function is called return (output)

def name(input) : code to execute when function is called return output Abstract: x = 3 y = 4 def myFun(a, b) : b = a + b <--- this creates a local variable return b x = myFun(x, y) <--- this is the call site print (x) <--- this prints 7 print (y) <--- this prints 4 Concrete:

Local vs Global Variables Functions introduce a new “scope” This scope defines the lifetime of local variables The scope is the function body def name(input) : code to execute <--- scope of local variables return output

Local vs Global Variables a = 3 b = 4 def myFun(a, b) : b = a + b <--- this creates a local variable return b a = myFun(a, b) <--- this is the call site print (a) <--- this prints 7 print (b) <--- this prints 4

Local vs Global Variables a = 3 b = 4 def myFun(x, y) : global b b = x + y <--- this assigns to the global variable return b a = myFun(a, b) <--- this is the call site print (a) <--- this prints 7 print (b) <--- this prints 7

Important Concepts Only ONE return is ever executed The return is the LAST statement executed in a function If there are statements that occur after the return they are ignored! If there is no return that is executed the function returns the special python value: NONE The return specifies the value that the function “outputs” If you return a variable the function outputs the value stored in that variable

Examples def noReturn(a): x = a +5 y = x + 5 print (y) x = noReturn(10) print (x) def noReturn(a): if a > 30: return -1 x = a +5 y = x + 5 print (y) x = noReturn(10) print (x)

Loops Loops allow us to execute a “set of actions” some number of times The number of times is specified differently for the two types of loops There are two types of loops For Loops and While Loops We call the execution of the body of the loop an iteration

For Loops For loops allow us to execute the body of the loop once for each element in a sequence A sequence can be either a Range, a List, or a String For loops assign to the variable each element of a list or each character of the string The value of the variable changes with each iteration of the loop for variable in sequence : code to execute for each element in the sequence

for variable in sequence : code to execute for each element in the sequence Abstract: str = “hello” for char in str : print(char) for num in [0,1,2,3,4,5] : print(num) for p in range(10, 0, -2) : print(p) Concrete:

While Loops While loops allow us to execute the body of the loop until the loop condition is false The loop condition is checked prior to the execution of the loop body (and on every iteration) while (condition) : code to execute until condition is false

while (condition) : code to execute until condition is false Abstract: str = “hello” i = 0 while (i < len(str)) : print(str) i = i +1 Concrete:

Nested Loops We know that each statement in the body of a loop is executed for each iteration What happens if we have a loop nested within another loop? for x in range(0, 10) : for y in range(0, 100): < --- this loop is the body print (y) of the outer loop print (x)

Conditionals Conditionals allow us to test for a condition and execute based on whether the condition is True or False if condition : code to execute if condition is True else: code to execute if condition is False

if condition : code to execute if condition is True else: code to execute if condition is False If x==5 : x = 4 else: x = 3 print (x) Abstract: Concrete:

Things to remember The else clause is optional There MUST be a condition to check after an elif The else clause is still optional here too Anything we can express with elif we can express with a nested if if x < 10: print(“Hello”) else: if y > 30: print(“World”) if x < 10: print(“Hello”) elif y > 30: print(“World”)

Strings and Lists Strings are defined by quotation marks Example: “this is a string” Example: “””this is a string””” List are defined by [] Example: [0,1,2,3,4,5] Strings and lists are 0 indexed We mean that the logically first element in either the string or list occurs at the 0 th position

Example: Reversing Strings def reverse(str): output = "” for i in str: output = i + output print (output)