2.7 Order of Operations
15
If either of the operands is a floating-point number, Python performs floating-point
division, and the result is a float:
>>> minute/60.0
0.98333333333333328
2.6
EXPRESSIONS
An
expression is a combination of values, variables, and operators. A value all by
itself is considered an expression, and so is a variable, so the following are all legal
expressions (assuming that the variable x has been assigned a value):
17
x
x + 17
If you type an expression in interactive mode, the interpreter
evaluates it and displays
the result:
>>> 1 + 1
2
But in a script, an expression all by itself doesn’t do anything! This is a common
source of confusion for beginners.
Exercise 2.2
Type the following statements in the Python interpreter to see what they do:
5
x = 5
x + 1
Now put the same statements into a script and run it. What is the output? Modify
the script by transforming each expression into a print statement and then run it
again.
Chia sẻ với bạn bè của bạn: