Lecture02-syntax-3pp

# The variable in math means equals in python x1 xx1 in

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: absolute value Example of rounding up the up number Example of division 27 9 13 ­09 ­10 Delimiters Delimiters – help the parser (interpreter) figure out which sequences of characters belong together #!/usr/local/bin/python3 # Converts temperatures from Fahrenheit to Celsius # Ask user for input F = float( input("Please enter a temperature in Fahrenheit: ") ) # Do the conversion C = (F-32)*5/9 # Print the result print("The temperature in Celsius is: ", C ) 28 What happens when the program runs? •  Line 5: •  “input” asks for a string, while showing the message passed as an argument to it •  “ﬂoat” converts the string to a so ­called ﬂoat (=real) number •  The result is stored in variable F •  Line 8: •  32 is subtracted from the value of F, the result is mul3plied by 5 and then divided by 9 •  The result is store in variable C •  Line 11: “print” writes the message, followed by the value of C to the output 29 Assignments and equality tests • In Python, the symbol “=“ means assignment and is always used to assign a value to a variable   Seman3cs: evaluate the expression and assign to the variable • In math, “=“ means equals In Python: x=1 x=x+1 In Math: ✔ simply re-assigns the value of x x=1 x = x +1 ✗ NONSENSE! 30 10 13 ­09 ­10 Assignments and equality tests • In Python, we use “==“ to mean equals • “==“ is always used in logical tests   x == 5 “asks” the ques3on: is x equals to 5? In Py...
View Full Document

## This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online