Download presentation
Presentation is loading. Please wait.
Published byVirginia Bond Modified over 9 years ago
1
Sep 3 2007CS1301 - O'Hara1 CS 1301 Review Keith O’Hara keith.ohara@gatech.edu http://wiki.roboteducation.org
2
Sep 3 2007CS1301 - O'Hara2 Programming Jargon Value - fundamental programming quantity with a type Float - 3.0 Integer - 3 String - “3”, “Three” Boolean - True, False Expression - Evaluates to a value. 3 + 2 - 6 *8 Statement - segment of code python executes and does something print 3 + 2 Variable - name that refers to a value x = 3 + 2
3
Sep 3 2007CS1301 - O'Hara3 Expressions Code snippets that evaluate to some value. 3**2 #evaluates to 9 (3+2)*(4+2) 3.0/2.0 “hello” + “world” (3 == 4) #evals to False (3 != 4) #evals to True (3 < 4) #evals to True “abc” < “bcd” #evals to True
4
Sep 3 2007CS1301 - O'Hara4 Types of Values Integers (like integers in math) -1, -2, 300000, 0 Floating Points (like “decimals”) -1.5, 1.5, 3.1415, 1.0 Character (like symbol of an alphabet) ‘a’, ‘b’, ‘c’, ‘z’ Strings (a string of characters) “CS”, “1301”, “rocks” Booleans (a truth value) True or False
5
Sep 3 2007CS1301 - O'Hara5 Have Type-Sense Your expressions should make sense in terms of what type they are Some make perfect sense 3 + 4 = 7 [int + int = int] Some expressions make no sense “hello” + 4 [ string + int] Some expressions make (uncommon) sense 4.0 + 3 [float + int] 4.0 + 3 = 7.0 [float + int = float] “CS” * 3 = “CSCSCS” [string * int = string]
6
Sep 3 2007CS1301 - O'Hara6 Order of Operations Order an expression is evaluated PEMDAS Parentheses Exponentiation Multiplication, Division Addition, Subtraction Left-to-Right (3-2)*(4+2)**2 (1)*(4+2)**2 (1) * (6)**2 (1) * (36) 36
7
Sep 3 2007CS1301 - O'Hara7 Variables Variables refer to values b = 3 + 2 # b = 5 a = b * 2 # a = 10 myName = “Keith” inCS1301 = True “=“ means assignment not equality b = 3 + 2 # store 5 in the variable b b = 3 * 2 # store 6 in the variable b x = x +1
8
Sep 3 2007CS1301 - O'Hara8 Statements Code snippets that do stuff! Driving the robot forward(1, 0.5) stop beep(1, 440) Assignment classname = “cs1301” Displaying to the screen print classname print “We love”, classname, “it’s great”
9
Sep 3 2007CS1301 - O'Hara9 Useful Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it values, it does some work, and it returns values You “call it”,”invoke it”, or “use it” by using its name and parentheses The things you pass it go inside the parentheses output = function(input) function input output
10
Sep 3 2007CS1301 - O'Hara10 Using Simple Functions forward(1) stop() beep(1, 440) Functions that interact with the robot forward (speed) beep(time, frequency) Pass them arguments Execute in sequential order flow of execution Top-level not in any function
11
Sep 3 2007CS1301 - O'Hara11 Writing Simple Functions def nudge(): print “going forward” forward(1) print “about to stop” stop() nudge() Inden t Defining functions Creates function Does not execute/run them Indenting implies “scope” or code ownership Call functions from top-level or other functions No Indention “Top Level”
12
Sep 3 2007CS1301 - O'Hara12 Writing Simple Functions def function-name(): statement … name()
13
Sep 3 2007CS1301 - O'Hara13 Writing Functions with Parameters def nudge(speed): print “Going forward with speed”, speed forward(speed) print “About to stop” stop() nudge(.2) nudge(.9) nudge(1)
14
Sep 3 2007CS1301 - O'Hara14 Octaves of A def beepA(length, octave): beep(length, 27.5 * (2**octave)) beepA(1,4) # A4 beepA(1,1) # A5 beepA(3,6) # A6 A4 : 440 Hz A5: 880 Hz A6: 1760 Hz A7: 3520 Hz Do I need the parentheses around 2**octave ?
15
Sep 3 2007CS1301 - O'Hara15 Writing Functions with Parameters def function-name(p1, p2, …, pn): statement … function-name(v1, v2, …, vn)
16
Sep 3 2007CS1301 - O'Hara16 Using Functions that Return Values name = raw_input(“Enter your name”) print “Hello”, name print “Robot battery voltage”, getBattery() p = takePicture() show(p) v = abs(-3) print “Absolute value of (-3) =“, v
17
Sep 3 2007CS1301 - O'Hara17 Converting between types float(3000) # returns 3000.0 int(3.0) # returns 3 int(3.99999) # returns 3 str(3.9) # returns ‘3.9’ int(“3”) # returns ‘3’ int(“3.0”) # error
18
Sep 3 2007CS1301 - O'Hara18 Composing Functions print abs(int(0 - 3.5)) print abs(int(-3.5)) print abs(-3) print 3 show(takePicture()) n = int(raw_input(“Enter a number”)) n = int(“9”) n = 9
19
Sep 3 2007CS1301 - O'Hara19 Writing Functions that Return Values def area(radius): return 3.14 * radius**2 def circumference(diameter): return 3.14 * diameter print “Area of a 3 ft circle”, area(3) print “Circumference”, circumference(2*3)
20
Sep 3 2007CS1301 - O'Hara20 Functions with Local Variables def area(radius): a = 3.14 * radius**2 return a def circumference(diameter): c = 3.14 * diameter return c print “Area of a 3 ft circle”, area(3) print “Circumference”, circumference(2*3)
21
Sep 3 2007CS1301 - O'Hara21 Variables in a Function are Local Variables in a function are private Including the parameters Each function has its own variables Even when the names are the same Allows you to write functions independently without worry about using the same name
22
Sep 3 2007CS1301 - O'Hara22 Different Variables - Same Name def area(radius): a = 3.14 * radius**2 return a def circumference(radius): a = 3.14 * 2 * radius return a print “Area of a 3 ft circle”, area(3) print “Circumference”, circumference(3) print a
23
Sep 3 2007CS1301 - O'Hara23 Writing Functions with Return Values def function-name(list-of-params): statement … return value output = function-name(list-of-params)
24
Sep 3 2007CS1301 - O'Hara24 Passing variables to functions userinput = raw_input(“Enter a number”) number = int(userinput) print “Absolute value = “, abs(number)
25
Sep 3 2007CS1301 - O'Hara25 Calling Your Own Functions def area(radius): return 3.14 * radius**2 invalue = raw_input(“Enter the radius”) r = int(invalue) Print “Area of a”, r, “ft circle”, area(r)
26
Sep 3 2007CS1301 - O'Hara26 Calling Your Own Functions def rect_area(length, width): area = length*width return area l = int(raw_input(“Enter the length”)) w = int(raw_input(“Enter the width”)) print “Area of rectangle”, rect_area(l,w)
27
Sep 3 2007CS1301 - O'Hara27 Same Name - Different Variables def rect_area(length, width): area = length*width return area length = int(raw_input(“Enter the length”)) width = int(raw_input(“Enter the width”)) print “Area of rect”, rect_area(length, width)
28
Sep 3 2007CS1301 - O'Hara28 Same Name - Different Variables def rect_area(length, width): area = length*width length = 0 width = 0 return area length = int(raw_input(“Enter the length”)) width = int(raw_input(“Enter the width”)) area = rect_area(length, width) print “The rectangle length =”, length print “The rectangle width =”, width print “The rectangle area =”, area
29
Sep 3 2007CS1301 - O'Hara29 Functions in general # description of this function # what it expects as input # what is provides as output def function (p 0, p 2, …, p n ): statement … return value z = function(a 0, a 2, …, a n )
30
Sep 3 2007CS1301 - O'Hara30 Math Functions import math math.sin(math.pi) math.log(100) Math module Set of useful math functions
31
Sep 3 2007CS1301 - O'Hara31 Where’s the Error? Forgot to return the value! def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3.0 print “Average Light Reading:”, avgLight()
32
Sep 3 2007CS1301 - O'Hara32 Where’s the Error? No Indentation def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3.0 return avg print “Average Light Reading:”, avgLight()
33
Sep 3 2007CS1301 - O'Hara33 Where’s the Error? Not calling function correctly def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3.0 return avg print “Average Light Reading:”, avgLight(1)
34
Sep 3 2007CS1301 - O'Hara34 Where’s the Error? avg is a local variable to the avgLight function def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3.0 return avg avgLight() print “Average Light Reading:”, avg
35
Sep 3 2007CS1301 - O'Hara35 Where’s the Error? def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = left + center + right / 3.0 return avg print “Average Light Reading:”, avgLight() Order of Operations wrong!
36
Sep 3 2007CS1301 - O'Hara36 Where’s the Error? def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3 return avg print “Average Light Reading:”, avgLight() Integer Division
37
Sep 3 2007CS1301 - O'Hara37 Test on Friday
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.