Page Contents
Python Arithmetic Operators are used to perform various mathematical processes (such as addition, division, multiplication, subtraction, etc.). All the arithmetic operators and their application are shown below in the form of a table taking the value of x and y as 9 and 2 –
Operator(name) | Example | Description | Result |
---|---|---|---|
+ (Addition) | x + y | Add two operands | 11 |
- (Subtraction) | x – y | Subtract right operand from left operand | 7 |
* (Multiplication) | x * y | Multiply two operands | 18 |
/ (Division) | x / y | Divide left operand by right operand (returns a float) | 4.5 |
// (Floor division) | x // y | Divide left operand by right operand (returns a whole number less than the float number return from division) | 4 |
% (Modulus) | x % y | Remainder of the division of left operand by the right operand | 1 |
** (Exponent) | x ** y | Left operand raised to the power of right operand | 81 |
Now we will see about all the arithmetic operators in details with code –
Python Addition operator
Python Addition(“+
”) operator is used to add two operands .
Example 1:
x=9
y=2
# using integer as operand
print("x+y =",x+y)
# using string as operand
print("apple+mango =","apple"+"mango")
# using list as operand
print("[1,2]+[4] =",[1,2]+[4])
x+y = 11 apple+mango = applemango [1,2]+[4] = [1, 2, 4]
In the above code we use +
operator with various types of operands –
- When we use
+
operator with integer operands it just add those operands .Here+
operator added9
and2
and returned11
. - When we use
+
operator with string operands it just concatenate those operands . Here+
operator concatenated“apple”
and“mango”
and returned“applemango”
. - when we use
+
operator with list operands it just merge those operands . Here+
operator merged[1,2]
and[4]
and returned[1,2,4]
.
Python Subtraction operator
Python Subtraction(“-
”) operator is used to subtract right operand from left operand –
Example 1:
# using integer as operand
print("x-y =",x-y)
x-y = 7
In the above code we use -
operator with integer operands .Here -
operator subtracted right operand(2
) from left operand(9
)and returned 7
.
Python Multiplication operator
Python Multiplication(“*
”) operator is used to multiply two operands –
Example 1:
# using integer as operand
print("x*y =",x*y)
# using string as operand
print("apple*3=","apple"*3)
# using list as operand
print("[1,2]*3 =",[1,2]*3)
x*y = 18 apple*3= appleappleapple [1,2]*3 = [1, 2, 1, 2, 1, 2]
In the above code we use *
operator with various types of operands –
- When we use
*
operator with integer operands it just multiply those operands .Here*
operator multiplied9
and2
and returned18
. - When we use
*
operator with string in left and integer in right it just return the same string value of integers time . Here*
operator returned"apple"
3 times as the integer in right was 3. - when we use
*
operator with list in left and integer in right it just merge the same list value of integers time . Here*
operator merged[1,2]
3 times as the integer in right was 3.
Python Division operator
Python Division(“/
”) operator is used to divide left operand with the right operand and returns a float number
Example 1:
# using integer as operand
print("x/y =",x/y)
x/y = 4.5
In the above code we use “/” operator with integer operands .Here “/” operator divide left operand(9) with right operand(2)and returned 4.5
Python Floor Division operator
Python Floor Division(“/
”) operator is used to divide left operand with the right operand and returns a immediate less integer number than the float number return from division.
Example 1:
# using positive integer as operand
print("9/2 =",9/2)
print("9//2 =",9//2)
# using negative integer as operand
print("-9/2 =",-9/2)
print("-9//2 =",-9//2)
9/2 = 4.5 9//2 = 4 -9/2 = -4.5 -9//2 = -5
In the above code we use //
operator with various types of operands –
- In the first case
“9//2”
returned4
which is immediate less integer number than the “9/2″(4.5) . - In the second case
“-9//2”
returned-5
which is also immediate less integer number than the “-9/2″(-4.5) .
Python Modulus operator
Python Modulus(“%
”) operator is used to divide the left operand with the right operand and returns the remainder .
Example 1:
# using integer as operand
print("x%y =",x%y)
x%y = 1
In the above code we use %
operator with integer operands .Here %
operator divided left operand(9
) with right operand(2
)and returned 1
as remainder .
Python Exponent operator
Python Exponent(“**
”) operator is used to raise the left operand to the power of right operand.
Example 1:
# using integer as operand
print("x**y =",x**y)
x**y = 81
In the above code we use **
operator with integer operands .Here **
operator raised left operand(9
) to the power of right operand(2
)and returned 81
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .