Lessons
Python Basics
Python Variables
Operators in Python
Conditional Statements in Python
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Loops in Python
Python Arrays and Functions
Conclusion
Arithmetic Operators in Python
Python Arithmetic Operators
Python provides several arithmetic operators to perform common mathematical operations on numeric values such as addition, subtraction, multiplication, division, and more. These operators can be applied to integers, floats, and even complex numbers.
Basic Arithmetic Operators
1. Addition (+)
The +
operator adds two values together.
Example:
python
1 2 3 4
x = 10 y = 5 result = x + y print(result) # Output: 15
2. Subtraction (-)
The -
operator subtracts one value from another.
Example:
python
1 2 3 4
x = 10 y = 5 result = x - y print(result) # Output: 5
3. Multiplication (*)
The *
operator multiplies two values.
Example:
python
1 2 3 4
x = 10 y = 5 result = x * y print(result) # Output: 50
4. Division (/)
The /
operator divides one value by another. The result is always a floating-point number.
Example:
python
1 2 3 4
x = 10 y = 5 result = x / y print(result) # Output: 2.0
Python Advanced Arithmetic Operators
5. Modulus (%)
The %
operator returns the remainder of a division.
Example:
python
1 2 3 4
x = 10 y = 3 result = x % y print(result) # Output: 1
The modulus operator is useful when you need to determine whether one number is divisible by another.
6. Exponentiation (**)
The **
operator raises one value to the power of another.
Example:
python
1 2 3 4
x = 2 y = 3 result = x ** y print(result) # Output: 8
In this case, 2 ** 3
is equivalent to 2 raised to the power of 3.
7. Floor Division (//)
The //
operator divides one value by another but returns only the whole number (integer) part of the division, discarding the remainder.
Example:
python
1 2 3 4
x = 10 y = 3 result = x // y print(result) # Output: 3
Floor division is useful when you only need the integer result without the fractional part.