Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Arithmetic operators in Python are used to perform mathematical operations on numerical values. In this tutorial, we will explore the various arithmetic operators and how to use them in Python.
+
):The addition operator is used to add two numeric values.
a = 10 b = 5 result = a + b print(result) # Output: 15
-
):The subtraction operator is used to subtract one numeric value from another.
a = 10 b = 5 result = a - b print(result) # Output: 5
*
):The multiplication operator is used to multiply two numeric values.
a = 10 b = 5 result = a * b print(result) # Output: 50
/
):The division operator is used to divide one numeric value by another. The result is a floating-point value.
a = 10 b = 5 result = a / b print(result) # Output: 2.0
//
):The floor division operator is used to divide one numeric value by another, but the result is rounded down to the nearest integer.
a = 10 b = 3 result = a // b print(result) # Output: 3
%
):The modulus operator is used to find the remainder when one numeric value is divided by another.
a = 10 b = 3 result = a % b print(result) # Output: 1
**
):The exponentiation operator is used to raise one numeric value to the power of another.
a = 2 b = 3 result = a ** b print(result) # Output: 8
Arithmetic operators can be used with different numeric data types, such as integers and floating-point numbers. Python will automatically convert the result to the appropriate data type.
a = 10.5 b = 3 result = a + b print(result) # Output: 13.5
In conclusion, arithmetic operators in Python are used to perform various mathematical operations on numeric values. They can be used with both integers and floating-point numbers and provide an easy way to manipulate numerical data in your Python code.
Using addition and subtraction in Python:
+
) and subtraction (-
) for numerical values. Addition combines two values, while subtraction subtracts the right operand from the left.a = 5 b = 3 addition_result = a + b # Result: 8 subtraction_result = a - b # Result: 2
Multiplication and division in Python arithmetic:
*
) combines two values by multiplying them, and division (/
) divides the left operand by the right.a = 5 b = 2 multiplication_result = a * b # Result: 10 division_result = a / b # Result: 2.5
Floor division and modulus in Python:
//
) returns the quotient of the division rounded down to the nearest integer. Modulus (%
) returns the remainder of the division.a = 7 b = 3 floor_division_result = a // b # Result: 2 modulus_result = a % b # Result: 1
Exponentiation with the ** operator in Python:
**
operator raises the left operand to the power of the right operand.base = 2 exponent = 3 exponentiation_result = base ** exponent # Result: 8
Order of operations in Python arithmetic expressions:
result = 2 + 3 * 4 # Result: 14 (Multiplication before addition)
Combining arithmetic operators in Python:
result = (3 + 5) * 2 - 4 / 2 # Result: 14 (Parentheses first, then multiplication, division, and subtraction)
Comparison operators vs. arithmetic operators in Python:
<
, >
, <=
, >=
, ==
, !=
) are used to compare values and return Boolean results. They differ from arithmetic operators that perform mathematical operations.a = 5 b = 3 comparison_result = a > b # Result: True
Overloading arithmetic operators in custom Python classes:
__add__
, __sub__
, etc., to overload arithmetic operators for instances of the class.class CustomNumber: def __init__(self, value): self.value = value def __add__(self, other): return CustomNumber(self.value + other.value) num1 = CustomNumber(3) num2 = CustomNumber(5) result = num1 + num2 # Custom addition using __add__