Python arithmetic operators are used to perform basic mathematical operations on numbers. Here are the main arithmetic operators in Python:
- Addition (
+
): Adds two operands. - Subtraction (
-
): Subtracts the second operand from the first. - Multiplication (
*
): Multiplies two operands. - Division (
/
): Divides the first operand by the second. The result is a float. - Modulus (
%
): Returns the remainder of the division of the first operand by the second. - Exponentiation (
**
): Raises the first operand to the power of the second. - Floor Division (
//
): Divides the first operand by the second and rounds down to the nearest integer.
Examples:
# Variables for demonstration
a = 10
b = 3
# Addition
addition = a + b # 10 + 3 = 13
# Subtraction
subtraction = a - b # 10 - 3 = 7
# Multiplication
multiplication = a * b # 10 * 3 = 30
# Division
division = a / b # 10 / 3 = 3.333...
# Modulus
modulus = a % b # 10 % 3 = 1
# Exponentiation
exponentiation = a ** b # 10 ** 3 = 1000
# Floor Division
floor_division = a // b # 10 // 3 = 3
These operators are fundamental to performing calculations and processing numerical data in Python. The result type (integer or float) may vary depending on the operation and the operands involved. For instance, the division operator /
always returns a float, while the floor division operator //
returns an integer if both operands are integers.
0 Comments
Please do note create link post in comment section