What Is an Arithmetic Operator in Python?
An arithmetic operator in Python is a symbol that performs basic math operations like addition,
subtraction, multiplication, and division on numbers or variables. These operators help you quickly
calculate values and work with numbers in your code.
We use Python arithmetic operators to make math tasks easier and faster when programming. They
are simple tools that allow us to perform everyday calculations in a clear and efficient manner.
Types of Arithmetic Operators in Python
We use Python arithmetic operators to perform different mathematical operations, including
subtraction, addition, multiplication, division, and more. Here are the arithmetic operators used in
Python:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)
6. Floor Division (//)
7. Exponentiation (**)
Operator Description Syntax
Addition: + adds the two operands. x+y
subtracts the second
Subtraction: – operand from the first x–y
operand.
Multiplication:
multiplies two operands. x*y
*
Division divides the first operand by
x/y
(float): / the second one.
divides the first operand by
Modulus: % the second one and returns x%y
the remainder.
Division divides the first operand by x // y
(floor): // the second and finds the
quotient without the
remainder.
returns the first operand
Power
raised to the power of the x ** y
(Exponent): **
second operand.
Precedence of Arithmetic Operators in Python
The following is the precedence of Python arithmetic operators, shown from highest to lowest:
Exponentiation (**)
Multiplication (*), Division (/), Modular (%), and Floor Division (//) (same precedence)
Addition (+) and Subtraction (-) (same precedence)
Here, we can see that the exponentiation operator has the highest precedence, so it will be
evaluated first, followed by multiplication, division, modular, and floor division operators, and then
addition and subtraction operators.
We can change the order of evaluation by using parentheses, just like in algebra. The expression
mentioned within parentheses is evaluated first, irrespective of operator precedence.
Operator Description Associativity
** Exponentiation Operator right-to-left
%, *, /, // Multiplication, Division, Modulus, and Floor Division left-to-right
+, – Addition and Subtraction operators left-to-right
Types of Python Comparison Operators
Below, we have listed and explained different types of comparison operators in Python with syntax to
help you understand them better.
Operator Name Description
Returns True when both operands have the
== Equal to a == b
same value.
Returns True when the operands have different
!= Not Equal to a != b
values.
Returns True if the left operand is larger than
> Greater than a>b
the right.
Returns True if the left operand is smaller than
< Less than a<b
the right.
Returns True if the left operand is either
>= Greater than or Equal to a >= b
greater or equal to the right.
Returns True if the left operand is either
<= Less than or Equal to a <= b
smaller or equal to the right.
Question ***How will python evaluate the following expressions?
1) 20 +30 *40
20 + 1200
1220
2) 20- 30 +40
-10+40
30
3) 15.0/4 + (8 +3.0)
15.0/ 4+11.0
3.75+11.0
14.75
LOGICAL OPERATOR
Logical AND operator
Logical OR operator
Logical NOT operator
Let’s discuss each of these operators in detail using examples.
Logical AND Operator in Python
We use the AND operator to check if both conditions are True. It returns True only if both operands
are True. If even one operand is False, it returns False. This operator combines multiple conditions,
requiring all to be True for the overall result to be True.
Here are the possible outcomes of the AND operator:
If both conditions are True, it returns True.
If one condition is False, it returns False.
If both conditions are False, it returns False.
x = 10
y=5
if x > 0 and y > 0:
print("Both conditions are True")
else:
print("At least one condition is False")
How the AND Operator Works?
We use it with two or more expressions.
It returns True only if all expressions are True.
It checks conditions from left to right.
It uses short-circuit evaluation; if we find a False condition, we stop and return False without
checking the rest.
Logical OR Operator in Python
The OR operator in Python verifies if at least one of the given conditions is True. If either of the
operands is True, it returns True. When both operands are False, it returns False. This operator is
used for conditions when at least one of the mentioned conditions must be True so that the overall
expression is True.
Here are the possible conditions that can occur in the OR operator:
If one condition is True and the second condition is False, it returns True.
If both conditions are True, it returns True.
If both conditions are False, it returns False.
Logical NOT Operator in Python
The NOT operator in Python works with a single Boolean value. It inverts the value of the
operand. We use it to invert the logic of an expression. So, if the operand is True, it returns
the Boolean value False; if the operand is False, it returns the Boolean value True.
Here are the possible conditions that can occur in the NOT operator:
If the condition is True, the resultant value is False.
If the condition is False, the resultant value is True.