http://www.cs.indiana.edu/classes/a201 Let's examine Python. We start IDLE and we get a prompt. Type numbers: integers, floating point numbers, negative numbers. We are in fact typing expressions. Python evaluates them and writes back their values. Write some expressions with numbers in Python. Expressions are composed of operands and operators. For example: 3 + 5 3 and 5 are operands and + is an operator. List some operators for numeric expressions: + addition - subtraction (use: 3 - 5 this is a binary operator or: -2 shows the sign of a number unary operator * multiplication / division Use parentheses to force an order for operations: ( 3 + 5 ) - 2 evaluates to 8 - 2 which evaluates to 6 3 + ( 5 - 2) evaluates to 3 + 3 which evaluates to 6 Try to predict the values of these expressions: a) 3 - 5 + 2 This evaluates to 0 because + and - have the same priority as operators and without parens we evaluate operators with the same priority in order left to right. 3 + 5 - 2 3 - 5 + 2 3 - 5 * 2 This evaluates to -7 because * has higher priority than -. If it were ( 3 - 5 ) * 2 then the parens would force a different order of evaluations and the answer is -2. / and * are at the same level, just above + and -. b) 3 - ( 5 + 2 ) evaluates to 3 - 7 which evaluates to -4. c) 7 / 2 this is division. Integer division. The answer is the quotient of the division. There are three 2's in 7 so the answer is 3. What is left unaccounted for? The remainder. The % operator calculates the remainder. So we had some examples: 7 % 2 evaluates to 1 7 / 2 evaluates to 3 *, / and % have the same priority +, - come right under them d) 1 / 2 evaluates to 0 but if at least one of the operands are floating-point the result is a floating-point number. Floating-point number = number with a fractional part. e) 6 * 2 / 3 evaluates to 12 / 3 which is 4 f) 2 / 3 * 6 evaluates to 0 * 6 which is 0 But as we have noticed 2.0 / 3 * 6 is 4.0 Yet 2 / 3 * 6.0 is 0.0 because the floating-point number appears too late in the expression. dgerman@indiana.edu >>> >>> >>> >>> >>> >>> >>> nothing Traceback (most recent call last): File "", line 1, in nothing NameError: name 'nothing' is not defined >>> >>> >>> >>> 3 3 >>> 5 5 >>> 12 12 >>> 109 109 >>> -3 -3 >>> 2.5 2.5 >>> 1 + 2 3 >>> 3 * 4 12 >>> 1 - 2 -1 >>> 5 + 3 - 2 6 >>> 5 + ( 3 - 2 ) 6 >>> (5 + 3) - 2 6 >>> 3 - 5 + 2 0 >>> 3 - 5 + 2 0 >>> 3 + 5 - 2 6 >>> 3 - 5 + 2 0 >>> 3 - 5 * 2 -7 >>> (3 - 5) * 2 -4 >>> 3 / 2 1 >>> 13 / 4 3 >>> 13 % 4 1 >>> 3 % 2 1 >>> 13 % 10 3 >>> 65 % 25 15 >>> 65 / 25 2 >>> 6 / 3 2 >>> 6 % 3 0 >>> 3 / 4 0 >>> 3 % 4 3 >>> 2 % 5 2 >>> 4 / 3 1 >>> 4.0 / 3.0 1.3333333333333333 >>> 1 / 3 0 >>> 1.0 / 3.0 0.33333333333333331 >>> 4.35 * 100 434.99999999999994 >>> 2 / 3 * 6 0 >>> 2.0 / 3 * 6 4.0 >>> 2 / 3 * 6.0 0.0 >>> 3 + 4 - 1 6 >>> nothing Traceback (most recent call last): File "", line 1, in nothing NameError: name 'nothing' is not defined >>> "nothing" 'nothing' >>> "blue" + "berry" 'blueberry' >>> "blue" + 3 Traceback (most recent call last): File "", line 1, in "blue" + 3 TypeError: cannot concatenate 'str' and 'int' objects >>> "blue" * 3 'blueblueblue' >>>