Arithmetic Operators
Learn to perform calculations in Python using arithmetic operators, covering basic operations and operator precedence.
We'll cover the following...
Below, we can find the basic arithmetic operators in order of precedence. The operator listed higher will be computed first. These operators allow us to perform arithmetic operations in Python.
Symbol | Operator | Precedence |
| Parentheses | Higher precedence |
| Exponent | |
| Modulo, Multiplication, Division, Floor Division | |
| Addition, Subtraction | Lower precedence |
Addition
We can add two numbers using the +
operator.
print(10 + 5)float1 = 13.65float2 = 3.40print(float1 + float2)num = 20flt = 10.5print(num + flt)
Explanation
Here is the code explanation:
Line 1: Adds two integers,
10
and5
, resulting in15
.Line 5: Adds two floating-point numbers
float1
andfloat2
having values13.65
and3.40
, producing17.05
.Line 9: Adds an integer
20
to a floating-point number10.5
, yielding30.5
. This illustrates that Python's addition operator can handle both integers and floating-point numbers, performing the necessary type conversions to produce the correct results.
Subtraction
We can subtract one number from the other using the -
operator.
print(10 - 5)float1 = -18.678float2 = 3.55print(float1 - float2)num = 20flt = 10.5print(num - flt)
Explanation
Here is the code explanation:
Line 1: Subtracts two integers,
10
and5
, resulting in5
.Line 5: Subtracts two floating-point numbers
float1
andfloat2
having values-18.678
and3.55
, producing-22.228
.Line 9: Subtracts an integer
20
to a floating-point number10.5
, yielding9.5
. This illustrates that Python's subtraction operator can handle both integers and floating-point numbers, performing the necessary type conversions to produce the correct results.
Multiplication
We can multiply two numbers using the *
operator.
print(40 * 10)float1 = 5.5float2 = 4.5print(float1 * float2)print(3*10.2)
Explanation
Here is the code explanation:
Line 1: Multiplies two integers,
40
and10
, resulting in400
.Line 5: Multiplies two floating-point numbers
float1
andfloat2
having values5.5
and4.5
...