Python Basic 2
Python Basic 2
Outline
3
Type Conversions and Rounding
Can you perform binary operations with two operands of
different types?
Yes. If an integer and a float are involved in a binary
operation, Python automatically converts the integer to a
float value.
This is called type conversion.
If one of the
So, 3 * 4.5 is the same as 3.0 * 4.5. operands for the
numeric operators is
a float value, the
result will be a float
value.
4
Type Conversions and Rounding
>>> value =
5.6
>>>
int(value)
5
>>> value =
5.6
>>>
round(value)
6
>>> value =
5.6
>>>
round(value)
6
>>> value
5.6
>>> 5
Arithmetic Operators
The operators for numeric data types include the standard arithmetic operators,
as shown in Table 2.1. The operands are the values operated by an operator.
6
Arithmetic Operators
The // operator performs an integer division; the result is an integer, and any
fractional part is truncated.
The % operator,
known as
remainder or
modulo operator,
yields the
remainder after
division.
7
Variable Comparison Operators
Logical & Boolean Operations
X = true Y=false??
13
Evaluating Expressions and Operator
Precedence
1. Exponentiation (**) is applied first.
2. Multiplication (*), float division (/), integer division (//) ,
and remainder operators (%) are applied next.
If an expression contains several multiplication, division, and
remainder operators, they are applied from left to right.
3. Addition (+) and subtraction (-) operators are applied last.
If an expression contains several addition and subtraction
operators, they are applied from left to right.
14
Operator Precedence
15
Tasks
16
Summary