int
and
float
. The standard arithmetic operations, +, -, *, /, and ** (exponentiation), can be used with parentheses forcing the order of operations away from normal operator precedence.
Other very useful operations are the remainder (modulo) operator, %, and integer division, //. Note that when two integers are divided, the result is a floating point. The integer division operator returns the integer portion of the quotient by truncating
any fractional part.bool
class, will be quite useful for representing truth values. The possible state values for
a boolean object are True
and False
with the standard boolean operators,
and
, or
, and not
.>>> True True >>> False False >>> False or True True >>> not (False or True) False >>> True and True True
Table 1: Relational and Logical Operators
Operation Name | Operator | Explanation |
---|---|---|
less than | < |
Less than operator |
greater than | > | Greater than operator |
less than or equal | <= | Less than or equal to operator |
greater than or equal | >= | Greater than or equal to operator |
equal | == | Equality operator |
not equal | ! = | Not equal operator |
logical and | and | Both operands True for result to be True |
logical or |
or | One or the other operand is True for the result to be True |
logical not | not | Negates the truth value, False becomes True, True becomes False |
>>> theSum = 0 >>> theSum 0 >>> theSum = theSum + 1 >>> theSum 1 >>> theSum = True >>> theSum True
theSum = 0
creates a variable called theSum
and lets it hold the reference to the data object 0
(see Figure 3). In general, the right-hand side of the assignment statement
is evaluated and a reference to the resulting data object is "assigned" to the name on the left-hand side. At this point in our example, the type of the variable is integer as that is the type of the data currently being referred to by theSum
. If the type of the data changes (see Figure 4), as shown above with the boolean value True
, so does the type of the variable
(theSum
is now of the type boolean). The assignment statement changes the reference being held by the variable. This is a dynamic characteristic of Python. The same variable
can refer to many different types of data.
Figure 3: Variables
Hold References to Data Objects
Figure 4: Assignment Changes the Reference