23
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, %, /, //, **)

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

Embed Size (px)

DESCRIPTION

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!

Citation preview

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

Midterm ReviewImportant control structures

FunctionsLoopsConditionals

Important things to reviewBinary Boolean operators (and, or, not)Libraries (import math vs from math import *)String operations: len, ord, +, *Math operations (math.sqrt, %, /, //, **)

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

Material to ReviewGo over all your Pre Labs

No questions on the Robot Specific commandsPre lab 6 NOT on the exam

Go over all your Lab SolutionsLab 6 will NOT be covered by the exam

Understand the results of the various clicker questions

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

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

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

Python Wiki BookThe Python Wiki Book provides very concise

definitions and examplesGood for review!

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

= vs ==In Python we leverage “=“ for assignment

Example: x = 5This sets the variable x to the value 5

In Python we leverage “==“ to test equalityExample: print(x==5)This will check if x is equal to 5 and then print True

if it is, or False if it is notTypically used in conjunction with an if statement

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

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

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

def name(input) : code to execute when function is called return output

Abstract:

x = 3y = 4def myFun(a, b) : b = a + b <--- this creates a local variable return bx = myFun(x, y) <--- this is the call siteprint (x) <--- this prints 7print (y) <--- this prints 4

Concrete:

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

Local vs Global VariablesFunctions introduce a new “scope”

This scope defines the lifetime of local variablesThe scope is the function body

def name(input) : code to execute <--- scope of local variables return output

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

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

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

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

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

Important ConceptsOnly 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

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

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

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

LoopsLoops allow us to execute a “set of actions”

some number of timesThe number of times is specified differently for the

two types of loopsThere are two types of loops

For Loops and While LoopsWe call the execution of the body of the loop an

iteration

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

For LoopsFor loops allow us to execute the body of the

loop once for each element in a sequenceA sequence can be either a Range, a List, or a

StringFor loops assign to the variable each element of

a list or each character of the stringThe value of the variable changes with each

iteration of the loop

for variable in sequence : code to execute for each element in the sequence

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

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:

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

While LoopsWhile loops allow us to execute the body of the

loop until the loop condition is falseThe 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

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

while (condition) : code to execute until condition is false

Abstract:

str = “hello”i = 0while (i < len(str)) : print(str) i = i +1

Concrete:

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

Nested LoopsWe know that each statement in the body of a

loop is executed for each iterationWhat 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)

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

ConditionalsConditionals 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 Trueelse: code to execute if condition is False

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

if condition : code to execute if condition is Trueelse: code to execute if condition is False

If x==5 : x = 4else: x = 3print (x)

Abstract:

Concrete:

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

Things to rememberThe else clause is optionalThere MUST be a condition to check after an elif

The else clause is still optional here tooAnything 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”)

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

Strings and ListsStrings 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 indexedWe mean that the logically first element in either

the string or list occurs at the 0th position

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

Example: Reversing Strings

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