Python
Arithmetic Operators in Python
Arithmetic Operators
15
#description
Arithmetic operators are used to perform mathematical operations on numerical values in Python.
Python supports several arithmetic operators which include:
- Addition (+): This operator is used to add two numbers together.
Example:
a = 5 b = 10 c = a + b print(c) # Output: 15
- Subtraction (-): This operator is used to subtract one number from another.
Example:
a = 10 b = 2 c = a - b print(c) # Output: 8
- Multiplication (*): This operator is used to multiply two numbers together.
Example:
a = 3 b = 6 c = a * b print(c) # Output: 18
- Division (/): This operator is used to divide a number by another number.
Example:
a = 9 b = 3 c = a / b print(c) # Output: 3.0
- Modulus (%): This operator is used to find the remainder of dividing one number by another number.
Example:
a = 11 b = 3 c = a % b print(c) # Output: 2
- Floor Division (//): Similar to division, but rounds down the result to the nearest integer.
Example:
a = 15 b = 2 c = a // b print(c) # Output: 7
- Exponentiation (**): This operator raises a number to the power of another number.
Example:
a = 2 b = 3 c = a ** b print(c) # Output: 8
These arithmetic operators can be used with variables or directly with numerical values. It is important to note that some operations may result in errors, such as division by zero, and should be avoided.
March 25, 2023