...

/

Arithmetic Operators

Arithmetic Operators

Learn to perform calculations in Python using arithmetic operators, covering basic operations and operator precedence.

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.

Press + to interact
print(10 + 5)
float1 = 13.65
float2 = 3.40
print(float1 + float2)
num = 20
flt = 10.5
print(num + flt)

Explanation

Here is the code explanation:

  • Line 1: Adds two integers, 10 and 5, resulting in 15.

  • Line 5: Adds two floating-point numbers float1 and float2 having values 13.65 and 3.40, producing 17.05.

  • Line 9: Adds an integer 20 to a floating-point number 10.5, yielding 30.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.

Press + to interact
print(10 - 5)
float1 = -18.678
float2 = 3.55
print(float1 - float2)
num = 20
flt = 10.5
print(num - flt)

Explanation

Here is the code explanation:

  • Line 1: Subtracts two integers, 10 and 5, resulting in 5.

  • Line 5: Subtracts two floating-point numbers float1 and float2 having values -18.678 and 3.55, producing -22.228.

  • Line 9: Subtracts an integer 20 to a floating-point number 10.5, yielding 9.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.

Press + to interact
print(40 * 10)
float1 = 5.5
float2 = 4.5
print(float1 * float2)
print(3*10.2)

Explanation

Here is the code explanation:

  • Line 1: Multiplies two integers, 40 and 10, resulting in 400.

  • Line 5: Multiplies two floating-point numbers float1 and float2 having values 5.5 and 4.5 ...

Access this course and 1400+ top-rated courses and projects.