Nearby lessons
35 of 108Python - 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
30
Example - Addition with Float Values
main.py
31.0
Addition with Strings
The + operator can also join (concatenate) two strings.
Example - String Concatenation
main.py
HelloPython
Important Note
For string concatenation, both operands must be strings.
Example - Invalid String Addition
main.py
TypeError
2. Subtraction Operator (-)
The subtraction operator is used to subtract one value from another.
Example - Subtraction
main.py
10
Example - Subtraction with Float Values
main.py
10.3
Important Note
The subtraction operator cannot be used with strings.
Example - Invalid String Subtraction
main.py
TypeError
3. Multiplication Operator (*)
The multiplication operator is used to multiply two values.
Example - Multiplication
main.py
50
Example - Multiplication with Float
main.py
21.0
String Repetition Using *
The * operator can repeat a string multiple times.
Example - String Repetition
main.py
PythonPythonPython
Important Note
For string repetition, one operand must be an integer.
Example - Invalid String Multiplication
main.py
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
5.0
Example - Division with Decimal Result
main.py
3.3333333333333335
Division by Zero
Dividing a number by zero causes an error.
Example - Division by Zero
main.py
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
5 3
Example - Floor Division with Float
main.py
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
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
True
7. Exponent Operator (**)
The exponent operator is used to calculate the power of a number.
Example - Exponent
main.py
8
Explanation
2 ** 3
means
2 × 2 × 2 = 8
Example - Another Exponent
main.py
25
Operator Precedence
Python follows operator precedence while evaluating arithmetic expressions.
Example - Operator Precedence
main.py
16
Explanation
First:
2 * 3 = 6
Then:
10 + 6 = 16
Using Parentheses
Parentheses change the order of execution.
Example - Parentheses
main.py
36
Arithmetic Operators with Variables
main.py
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) |