1 / 37

CS 1301 Review

CS 1301 Review. Keith O’Hara keith.ohara@gatech.edu. http://wiki.roboteducation.org. 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

zeigler
Download Presentation

CS 1301 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 1301Review Keith O’Hara keith.ohara@gatech.edu http://wiki.roboteducation.org CS1301 - O'Hara

  2. 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 CS1301 - O'Hara

  3. 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 CS1301 - O'Hara

  4. 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 CS1301 - O'Hara

  5. 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] CS1301 - O'Hara

  6. 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 CS1301 - O'Hara

  7. 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 CS1301 - O'Hara

  8. 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” CS1301 - O'Hara

  9. 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) input function output CS1301 - O'Hara

  10. 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 CS1301 - O'Hara

  11. Writing Simple Functions def nudge(): print “going forward” forward(1) print “about to stop” stop() nudge() • Defining functions • Creates function • Does not execute/run them • Indenting implies “scope” or code ownership • Call functions from top-level or other functions Indent No Indention “Top Level” CS1301 - O'Hara

  12. Writing Simple Functions def function-name(): statement statement … statement name() CS1301 - O'Hara

  13. 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) CS1301 - O'Hara

  14. Octaves of A Do I need the parentheses around 2**octave? 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 CS1301 - O'Hara

  15. Writing Functions with Parameters def function-name(p1, p2, …, pn): statement statement … statement function-name(v1, v2, …, vn) CS1301 - O'Hara

  16. 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 CS1301 - O'Hara

  17. 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 CS1301 - O'Hara

  18. 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 CS1301 - O'Hara

  19. 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) CS1301 - O'Hara

  20. Functions with Local Variables def area(radius): a = 3.14 * radius**2 returna def circumference(diameter): c = 3.14 * diameter returnc print “Area of a 3 ft circle”, area(3) print “Circumference”, circumference(2*3) CS1301 - O'Hara

  21. 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 CS1301 - O'Hara

  22. Different Variables - Same Name def area(radius): a = 3.14 * radius**2 returna def circumference(radius): a = 3.14 * 2 * radius returna print “Area of a 3 ft circle”, area(3) print “Circumference”, circumference(3) print a CS1301 - O'Hara

  23. Writing Functions with Return Values def function-name(list-of-params): statement statement … statement return value output = function-name(list-of-params) CS1301 - O'Hara

  24. Passing variables to functions userinput = raw_input(“Enter a number”) number = int(userinput) print “Absolute value = “, abs(number) CS1301 - O'Hara

  25. 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) CS1301 - O'Hara

  26. Calling Your Own Functions def rect_area(length, width): area = length*width returnarea l = int(raw_input(“Enter the length”)) w = int(raw_input(“Enter the width”)) print “Area of rectangle”, rect_area(l,w) CS1301 - O'Hara

  27. Same Name - Different Variables def rect_area(length, width): area = length*width returnarea length = int(raw_input(“Enter the length”)) width = int(raw_input(“Enter the width”)) print “Area of rect”, rect_area(length, width) CS1301 - O'Hara

  28. 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 CS1301 - O'Hara

  29. Functions in general # description of this function # what it expects as input # what is provides as output def function (p0, p2, …, pn): statement … statement return value z = function(a0, a2, …, an) CS1301 - O'Hara

  30. Math Functions • import math • math.sin(math.pi) • math.log(100) • Math module • Set of useful math functions CS1301 - O'Hara

  31. Where’s the Error? def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3.0 print “Average Light Reading:”, avgLight() • Forgot to return the value! CS1301 - O'Hara

  32. 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() • No Indentation CS1301 - O'Hara

  33. 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(1) • Not calling function correctly CS1301 - O'Hara

  34. Where’s the Error? 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 • avg is a local variable to the avgLight function CS1301 - O'Hara

  35. Where’s the Error? def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = left + center + right / 3.0 returnavg print “Average Light Reading:”, avgLight() • Order of Operations wrong! CS1301 - O'Hara

  36. Where’s the Error? def avgLight(): left = getLight(‘left’) center = getLight(‘center’) right = getLight(‘right’) avg = (left + center + right) / 3 returnavg print “Average Light Reading:”, avgLight() • Integer Division CS1301 - O'Hara

  37. Test on Friday CS1301 - O'Hara

More Related