calculating-3

calculating-3 - Sep 8 2010 CMSC143 O'Hara 1 CMSC 143...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sep 8 2010 CMSC143 - O'Hara 1 CMSC 143 Calculating with Python Keith O’Hara [email protected] QUIZ Sep 8 2010 CMSC143 - O'Hara 2 Write a function named “announce” that speaks the robot’s name three times Sep 8 2010 CMSC143 - O'Hara 3 Useful Functions A function is a piece of code you can use over and over again Treat it like a black box You can pass it values, it does some work, and it can 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 Sep 8 2010 CMSC143 - O'Hara 4 Functions Perform Actions def beepA( length, octave ): beep( length , 440 * (2** octave )) beepA ( 1,0 ) # A4 beepA ( 2,1 ) # A5 beepA ( 3,2 ) # A6 A4 : 440 Hz A5: 880 Hz A6: 1760 Hz A7: 3520 Hz Sep 8 2010 CMSC143 - O'Hara 5 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 Sep 8 2010 CMSC143 - O'Hara 6 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 – a named value x = 3 + 2 Sep 8 2010 CMSC143 - O'Hara 7 Types of Values Integers (like integers in math) -1, -2, 300000, 0 Float ing Points (like “decimals”) -1.5, 1.5, 3.1415, 1.0 Booleans (a truth value) True or False Strings (a string of characters) “CMSC”, “143”, “rocks” Sequences [1, 2, 3], “123”, (1, 2, 3) Objects (anything!) Pictures, sounds, shapes >>> type(3.0) <type 'float'> >>> type(3) <type 'int'> >>> type ("3") <type 'str'> Sep 8 2010 CMSC143 - O'Hara 8 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 Sep 8 2010 CMSC143 - O'Hara 9 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 Sep 8 2010 CMSC143 - O'Hara 10 Integer Arithmetic Addition (+) Subtraction (-) Multiplication (*) Integer Division (/) 4/2 # evals to 2 5/2 # evals to 2 Modulo/Remainder(%) 4%2 # evals to 0 5%2 # evals to 1 Exponentiation (**) 2**3 # evals to 8 2**4 # evals to 16 Sep 8 2010 CMSC143 - O'Hara 11 Float Arithmetic Addition (+) Subtraction (-) Multiplication (*) Integer Division (/) 4.0/2.0 # evals to 2.0 5.0/2.0 # evals to 2.5 5....
View Full Document

Page1 / 49

calculating-3 - Sep 8 2010 CMSC143 O'Hara 1 CMSC 143...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online