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.
Types of Operators in Python
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Identity Operators and Membership Operators
Python Operators
Arithmetic Operators in Python
Python Arithmetic operators are used to perform basic mathematical
operations like addition, subtraction, multiplication, and division.
In Python 3.x the result of division is a floating-point while in Python 2.x
division of 2 integers was an integer. To obtain an integer result in Python
3.x floored (// integer) is used.
Operator Description Syntax
+ Addition: adds two operands x + y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
% Modulus: returns the remainder when the first operand is divided by
the second x % y
** Power: Returns first raised to power second x ** y
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:
Float division
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’.
[GFGTABS] Python
print(5/5)
print(10/2)
print(-10/2)
print(20.0/2)
[/GFGTABS]
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.
[GFGTABS] Pythons
print(10//3)
print (-5//2)
print (5.0//2)
print (-5.0//2)
[/GFGTABS]
Output:
-3
2.0
-3.0
Precedence of Arithmetic Operators in Python
The precedence of Arithmetic Operators in Python is as follows:
P – Parentheses
E – Exponentiation
M – Multiplication (Multiplication and division have the same precedence)
D – Division
A – Addition (Addition and subtraction have the same precedence)
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.
[GFGTABS] 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)
[/GFGTABS]
Output:
13
36
6561
Note: Refer to Differences between / and // for some interesting facts
about these two Python operators.
Comparison of Python Operators
In Python Comparison of Relational operators compares the values. It
either returns True or False according to the condition.
Operator Description Syntax
> Greater than: True if the left operand is greater than the right x
>y
< Less than: True if the left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
>= Greater than or equal to True if the left operand is greater than or
equal to the right x >= y
<= Less than or equal to True if the left operand is less than or equal to
the right x <= y
= is an assignment operator and == comparison operator.
Precedence of Comparison Operators in Python
In Python, the comparison operators have lower precedence than the
arithmetic operators. All the operators within comparison operators have
the same precedence order.
Example of Comparison Operators in Python
Let’s see an example of Comparison Operators in Python.
Example: The code compares the values of ‘a’ and ‘b’ using various
comparison Python operators and prints the results. It checks if ‘a’ is
greater than, less than, equal to, not equal to, greater than, or equal to,
and less than or equal to ‘b’.
[GFGTABS] Python
a = 13
b = 33
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
[/GFGTABS]
Output
False
True
False
True
False
True
Logical Operators in Python
Python Logical operators perform Logical AND, Logical OR, and Logical
NOT operations. It is used to combine conditional statements.
Operator Description Syntax
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if the operand is false not x
Precedence of Logical Operators in Python
The precedence of Logical Operators in Python is as follows:
Logical not
logical and