By using operators you can perform operations on the numeric values (operands).
print(5 + 4)
9
In the above example, using the (+
) plus operator, we have added two numeric values. In this case, the (+
) plus is an operator, and numeric values (5
and 4
) are operands.
The arithmetic operators are used to perform mathematical operations in Python.
To perform addition on two or more numeric values, you can use the addition (+
) operator.
Python uses the mathematical plus (+
) sign to denote addition.
print(5 + 4)
9
In the above example, we have added two integer numbers (4
and 5
) by using the plus (+
) mathematical sign, and the addition of the two numbers is displayed in the output (9
).
To perform subtraction on two or more numeric values, you can use the subtraction (-
) operator.
Python uses the mathematical minus (-
) sign to denote subtraction.
print(5 - 3)
2
In the above example, we have subtracted two integer numbers (5
and 3
) by using the minus (-
) mathematical sign, and the subtraction of the two numbers is displayed in the output (2
).
To perform multiplication on two or more numeric values, you can use the multiplication (*
) operator.
The asterisk (*
) sign in Python denotes multiplication.
print(5 * 3)
15
In the above example, we have multiplied two integer numbers (5
and 3
) by using the multiplication operator (asterisk (*
) sign), and the multiplication of the two numbers is displayed in the output (15
).
To perform division on numeric values, you can use the division (/
) operator.
Python uses the forward slash (/
) sign to denote division.
print(6 / 3)
2
In the above example, we have performed division operations on integer numbers (6
and 3
) by using the division operator (forward slash (/
) sign), and the division of the numbers is displayed in the output (2
).
In Python, the modulus operator is used to get the remainder of a division.
Here, use the percentage (%
) sign for modulus operation.
print(10 % 2)
print(9 % 2)
0
1
In the above example, by using the modulus (%
) operator we perform a modulus operation on the numbers 10
modulus by 2
. Here in the output, the modulus result is 0
.
Similarly, 9
modulus by 2
, then the result is 1
.
In Python, you can use the double asterisk **
operator to perform exponential operations, which means raising a number to a certain power.
Here's how it works:
result = base ** exponent
Here, 'base' is the number you want to raise to a power, and 'exponent' is the power to which you want to raise it.
print(2 ** 3)
8
In this example, the exponentiation 'result' equals 8
, as 2^3
is 8
.
In Python, floor division is done using double forward slashes (//
).
It divides the left operand by the right operand and returns the largest integer less than or equal to the quotient.
You can use it when you want to obtain the whole number part of a division result without decimal point.
print(7 // 3)
2
In the above example, the result will be 2
, as it discards the decimal part of the division.