Python 3 – Arithmetic Operators Example
Python is a multi-purpose language that is used for various applications that range from developing web applications, scientific analysis, and machine learning to game programming. One of the most fundamental concepts in programming is arithmetic operations. In this article, we will look at the arithmetic operations in Python 3 and their examples.
Arithmetic Operations in Python 3
Arithmetic operations are mathematical operations that include addition, subtraction, multiplication, and division. In Python, we have several arithmetic operators that perform these operations.
Addition
The addition operator is represented by a plus sign (+). It is used to add two or more variables in Python. The following is an example of how to use the addition operator:
# addition example
a = 10
b = 5
c = a + b
print(c) # output: 15
Subtraction
The subtraction operator is represented by a minus sign (-). It is used to subtract two or more variables in Python. The following is an example of how to use the subtraction operator:
# subtraction example
a = 10
b = 5
c = a - b
print(c) # output: 5
Multiplication
The multiplication operator is represented by an asterisk sign (*). It is used to multiply two or more variables in Python. The following is an example of how to use the multiplication operator:
# multiplication example
a = 10
b = 5
c = a * b
print(c) # output: 50
Division
The division operator is represented by a forward-slash sign (/). It is used to divide two or more variables in Python. The following is an example of how to use the division operator:
# division example
a = 10
b = 5
c = a / b
print(c) # output: 2.0
In the example above, the division operator returned a floating-point number. If you want the result to be an integer, you can use the double forward-slash operator (//).
# integer division example
a = 10
b = 5
c = a // b
print(c) # output: 2
Modulo
The modulo operator is represented by a percent sign (%). It is used to get the remainder of a division operation. The following is an example of how to use the modulo operator:
# modulo example
a = 10
b = 3
c = a % b
print(c) # output: 1
In the example above, the modulo operator returned the remainder of the division operation 10/3, which is 1.
Exponentiation
The exponentiation operator is represented by two asterisks (**). It is used to raise a number to a power. The following is an example of how to use the exponentiation operator:
# exponentiation example
a = 2
b = 3
c = a ** b
print(c) # output: 8
In the example above, the exponentiation operator returned the result of 2 raised to the power of 3, which is 8.
Conclusion
Arithmetic operations are an essential part of programming, and Python provides us with several arithmetic operators to perform these operations. In this article, we looked at the addition, subtraction, multiplication, division, modulo, and exponentiation operators in Python 3, along with their examples. By using these operators, we can perform various mathematical calculations in Python.