Example of Arithmetic Operators in Python

Division Operators

In Python programming language Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. 

There are two types of division operators: 

  1. Float division
  2. Floor division

Float division

The quotient returned by this operator is always a float number, no matter if two numbers are integers. For example:

Example: The code performs division operations and prints the results. It demonstrates that both integer and floating-point divisions return accurate results. For example, ’10/2β€² results in β€˜5.0’, and β€˜-10/2’ results in β€˜-5.0’.

Python

print(5/5)
print(10/2)
print(-10/2)
print(20.0/2)

Output:

1.0
5.0
-5.0
10.0

Integer division( Floor division)

The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:

Example: The code demonstrates integer (floor) division operations using the // in Python operators. It provides results as follows: ’10//3β€² equals β€˜3’, β€˜-5//2’ equals β€˜-3’, β€˜5.0//2β€² equals β€˜2.0’, and β€˜-5.0//2’ equals β€˜-3.0’. Integer division returns the largest integer less than or equal to the division result.

Pythons

print(10//3)
print (-5//2)
print (5.0//2)
print (-5.0//2)

Output:

3
-3
2.0
-3.0

Precedence of Arithmetic Operators in Python

The precedence of Arithmetic Operators in Python is as follows:

  1. P – Parentheses
  2. E – Exponentiation
  3. M – Multiplication (Multiplication and division have the same precedence)
  4. D – Division
  5. A – Addition (Addition and subtraction have the same precedence)
  6. S – Subtraction

The modulus of Python operators helps us extract the last digit/s of a number. For example:

  • x % 10 -> yields the last digit
  • x % 100 -> yield last two digits

Arithmetic Operators With Addition, Subtraction, Multiplication, Modulo and Power

Here is an example showing how different Arithmetic Operators in Python work:

Example: The code performs basic arithmetic operations with the values of β€˜a’ and β€˜b’. It adds (β€˜+’), subtracts (β€˜-β€˜), multiplies (β€˜*’), computes the remainder (β€˜%’), and raises a to the power of β€˜b (**)’. The results of these operations are printed.

Python

a = 9
b = 4
add = a + b

sub = a - b

mul = a * b

mod = a % b

p = a ** b
print(add)
print(sub)
print(mul)
print(mod)
print(p)

Output:

13
5
36
1
6561

Note: Refer to Differences between / and // for some interesting facts about these two Python operators.

Python Operators

In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. 

  • OPERATORS: These are the special symbols. Eg- + , * , /, etc.
  • OPERAND: It is the value on which the operator is applied.

Similar Reads

Types of Operators in Python

Arithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsIdentity Operators and Membership Operators...

Arithmetic Operators in Python

Python Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division....

Example of Arithmetic Operators in Python

Division Operators...

Comparison of Python Operators

In Python Comparison of Relational operators compares the values. It either returns True or False according to the condition....

Logical Operators in Python

Python Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements....

Bitwise Operators in Python

Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers....

Assignment Operators in Python

Python Assignment operators are used to assign values to the variables....

Identity Operators in Python

In Python, is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical....

Membership Operators in Python

In Python, in and not in are the membership operators that are used to test whether a value or variable is in a sequence....

Ternary Operator in Python

in Python, Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5....

Precedence and Associativity of Operators in Python

In Python, Operator precedence and associativity determine the priorities of the operator....

Python Operator Exercise Questions

Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below....

Contact Us