Python Arithmetic Operators
Python Arithmetic Operators
Python Arithmetic operators are used to perform arithmetical operations like addition, subtraction, multiplication, division, etc, in Python programs. We use these operators in scientific calculations and mathematical expressions.
Python Arithmetic Operators
Python arithmetic operators are as follows:
Python Operator |
Name | Description |
+ | Addition Operator | The + operator adds two operands. |
– | Subtraction Operator | The – operator subtracts the right operand from the left operand.
z = x – y For example, the value of y is subtracted from the x and then assigned to the variable z.
|
* | Multiplication Operator | The * operator multiplies two operands. |
/ | True Division Operator | The / operator divides the left operand by the right one and returns the result into a float. |
% | Modulus Operator | The % operator returns the remainder of the division of the left operand by the right. |
// | Floor Division Operator | The // operator divides the left operand by the right one and returns results into the whole number adjusted to the left in the number line. |
** | Exponential Operator | The ** operator returns the value of the left operand raised to the power of the right. |
Example
The below program illustrates the use of the arithmetic operators:
# Python Arithmetic Operators program # Python Tutorials - www.TestingDocs.com number1 = input('Enter a number:') number1 = int(number1) number2 = input('Enter a number:') number2 = int(number2) result = number1 + number2 print('Addition =', result) diff = number1 - number2 print('Subtraction =', diff) product = number1 * number2 print('Multiplication =', product) truediv = number1 / number2 print('True Division =', truediv) floordiv = number1 // number2 print('Floor Division =', floordiv)
Output
Sample output of the program:
Enter a number:25
Enter a number:7
Addition = 32
Subtraction = 18
Multiplication = 175
True Division = 3.5714285714285716
Floor Division = 3
—
Python Tutorials
Python Tutorial on this website can be found at:
https://www.testingdocs.com/python-tutorials/
More information on Python is available at the official website: