Nearby lessons

35 of 108

Python - Arithmetic Operators

Introduction

Arithmetic operators are used to perform mathematical calculations in Python.

These operators work with numbers and, in some cases, with strings.

Arithmetic Operators List

Operator Meaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
//Floor Division
**Exponent

1. Addition Operator (+)

The addition operator is used to add two values.

Example - Addition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
30

Example - Addition with Float Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
31.0

Addition with Strings

The + operator can also join (concatenate) two strings.

Example - String Concatenation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
HelloPython

Important Note

For string concatenation, both operands must be strings.

Example - Invalid String Addition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

2. Subtraction Operator (-)

The subtraction operator is used to subtract one value from another.

Example - Subtraction

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10

Example - Subtraction with Float Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.3

Important Note

The subtraction operator cannot be used with strings.

Example - Invalid String Subtraction

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

3. Multiplication Operator (*)

The multiplication operator is used to multiply two values.

Example - Multiplication

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
50

Example - Multiplication with Float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
21.0

String Repetition Using *

The * operator can repeat a string multiple times.

Example - String Repetition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
PythonPythonPython

Important Note

For string repetition, one operand must be an integer.

Example - Invalid String Multiplication

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

4. Division Operator (/)

The division operator divides one value by another.

Important Rule: The division operator always returns a floating-point value.

Example - Division

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5.0

Example - Division with Decimal Result

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3.3333333333333335

Division by Zero

Dividing a number by zero causes an error.

Example - Division by Zero

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ZeroDivisionError

5. Floor Division Operator (//)

The floor division operator returns only the floor (quotient) value.

The decimal part is removed.

Example - Floor Division

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5
3

Example - Floor Division with Float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5.0

Difference Between / and //

/ //
Returns a floating-point result. Returns the floor value.
Example: 10 / 3 = 3.3333... Example: 10 // 3 = 3

6. Modulus Operator (%)

The modulus operator returns the remainder after division.

Example - Modulus

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
1

Explanation

Expression Result
10 % 2 0
10 % 3 1

Usage of Modulus Operator

The modulus operator is commonly used to:

  • Check even numbers.
  • Check odd numbers.
  • Check divisibility.

Example - Check Even Number

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

7. Exponent Operator (**)

The exponent operator is used to calculate the power of a number.

Example - Exponent

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
8

Explanation

2 ** 3

means

2 × 2 × 2 = 8
        

Example - Another Exponent

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
25

Operator Precedence

Python follows operator precedence while evaluating arithmetic expressions.

Example - Operator Precedence

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
16

Explanation

First:

2 * 3 = 6

Then:

10 + 6 = 16
        

Using Parentheses

Parentheses change the order of execution.

Example - Parentheses

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
36

Arithmetic Operators with Variables

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
30
10
200
2.0

Real World Usage

Arithmetic operators are commonly used in:

  • Billing systems
  • Banking applications
  • Marks calculation
  • Scientific calculations
  • E-commerce applications

Important Notes

  • Arithmetic operators perform mathematical calculations.
  • The division operator always returns a floating-point value.
  • Division by zero causes a ZeroDivisionError.
  • The + and * operators also work with strings in specific cases.
  • Operator precedence affects the final result of an expression.

Quick Summary

Operator Purpose
+Addition
-Subtraction
*Multiplication
/Division
//Floor Division
%Remainder (Modulus)
**Power (Exponent)

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Relational Operators