Operators are symbols that perform specific operations on values (operands) and produce a result. There are several types of operators in Python, including:
Arithmetic operators: perform arithmetic operations like addition, subtraction, multiplication, division and exponentiation.
+(addition)
-(subtraction)
*(multiplication)
/ (division)
% (modulus)
** (exponentiation)
Comparison operators: compare two values and return a Boolean value indicating if the comparison is true or false.
== (equal to)
!= (not equal to)
>(greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Assignment operators: assign a value to a variable.
= (assign)
+= (add and assign)
-= (subtract and assign)
*= (multiply and assign)
/= (divide and assign)
%= (modulus and assign)
**= (exponentiation and assign)
Logical operators: perform logical operations like and, or, not and return a Boolean value.
and
or
not
Bitwise operators: perform bitwise operations on binary representations of integers.
& (bitwise AND)
| (bitwise OR)
^ (bitwise XOR)
~ (bitwise NOT)
<<(left shift)
>>(Right Shift)
Membership operators: test if an element is present in a sequence (list, tuple, string, etc.).
in
not in
Identity operators: compare the memory addresses of two objects.
is
is not
Arithmetic operators
Arithmetic operators are used to perform basic mathematical operations in Python, such as addition, subtraction, multiplication, division, etc.
The following are the arithmetic operators in Python:
Addition: + operator is used to add two numbers.
Subtraction: – operator is used to subtract two numbers
Multiplication: * operator is used to multiply two numbers.
Division: / operator is used to divide two numbers
Modulus: % operator is used to find the remainder of a division operation.
Exponentiation: ** operator is used to calculate the exponent of a number.
Comparison Operators
Comparison operators in Python are used to compare two values and determine if they are equal, unequal, greater than, less than, etc.
The comparison operators in Python return a Boolean value of either True or False.
The following are the comparison operators in Python:
Equal to: == operator is used to check if two values are equal.
Not equal to: != operator is used to check if two values are not equal.
Greater than: > operator is used to check if one value is greater than another
Less than: < operator is used to check if one value is less than another
Greater than or equal to: >= operator is used to check if one value is greater than or equal to another.
Less than or equal to: <= operator is used to check if one value is less than or equal to another.
Logical Operators
Logical operators in Python are used to perform Boolean operations on values, such as AND, OR, NOT. The result of a logical operation is either True or False.
The following are the logical operators in Python:
Logical AND: and operator returns True if both operands are True, otherwise it returns False.
Logical OR: or operator returns True if at least one of the operands is True, otherwise it returns False
Logical NOT: not operator returns True if the operand is False, and False if the operand is True.
These logical operators can be used with any data type that supports Boolean operations, including integers, floating-point numbers, and strings. However, in most cases, they are used with Boolean values (True or False)
Assignment Operators
Assignment operators in Python are used to assign a value to a variable. The following are the assignment operators in Python
Simple Assignment: = operator is used to assign a value to a variable
Compound Assignment (Addition): += operator is used to add a value to a variable and assign the result to the same
variable.
Compound Assignment (Subtraction): -= operator is used to subtract a value from a variable and assign the result to the same variable
Compound Assignment (Multiplication): *= operator is used to multiply a variable by a value and assign the result to the same variable.
Compound Assignment (Division): /= operator is used to divide a variable by a value and assign the result to the same variable.
Compound Assignment (Modulus): %= operator is used to find the remainder of dividing a variable by a value and assign the result to the same variable.
Compound Assignment (Exponentiation): **= operator is used to raise a variable to the power of a value and assign the result to the same variable.
Compound Assignment (Floor Division): //= operator is used to perform floor division on a variable by a value and assign the result to the same variable.
These assignment operators can be used with variables of any data type, including integers, floating-point numbers, strings, and others.
Bitwise Operators
Bitwise operators in Python are used to perform bit-level operations on integers. The following are the bitwise operators in Python:
Bitwise AND: & operator is used to perform a bitwise AND operation on two integers
This operator compares each bit of two numbers and performs a bitwise AND operation on corresponding bits. If both bits are 1
, the result is 1
; otherwise, the result is 0
.
Bitwise Operation Breakdown:
Convert a
and b
to their binary representations:a = 10
→ 1010
(in binary)b = 5
→ 0101
(in binary)
Perform a bitwise AND on corresponding bits:
1010
& 0101
-----
0000 (Result in binary)
The result of 0000
in binary is 0
in decimal.
Explanation:
The AND operation checks each bit:
- 1 AND 0 = 0
- 0 AND 1 = 0
- 1 AND 0 = 0
- 0 AND 1 = 0
Since no pair of bits from a
and b
are both 1
, the result is 0
. This demonstrates how the bitwise AND operator works in Python.
Bitwise OR: | operator is used to perform a bitwise OR operation on two integers.
The bitwise OR (|
) operator is used to compare each bit of two numbers and perform a OR operation. If either of the bits in a position is 1
, the result is 1
; otherwise, it is 0
.
Bitwise Operation Breakdown:
Convert a
and b
to their binary representations:a = 10
→ 1010
(in binary)b = 5
→ 0101
(in binary)
Perform a bitwise OR on corresponding bits:
1010
| 0101
-----
1111 (Result in binary)
The result of 1111
in binary is 15
in decimal.
Explanation:
The OR operation checks each bit:
- 1 OR 0 = 1
- 0 OR 1 = 1
- 1 OR 0 = 1
- 0 OR 1 = 1
Thus, every bit in the result is 1
, making the final binary result 1111
, which equals 15
in decimal. This demonstrates how the bitwise OR operator works in Python.
Bitwise XOR: ^ operator is used to perform a bitwise XOR operation on two integers.
The bitwise XOR (^
) operator performs an exclusive OR operation on the bits of two numbers. It compares each bit of the two numbers and sets the result bit to 1
if the bits are different (i.e., one is 1
and the other is 0
), and 0
if the bits are the same (i.e., both 1
or both 0
).
Convert a
and b
to their binary representations:a = 10
→ 1010
(in binary)b = 5
→ 0101
(in binary)
Perform a bitwise XOR on corresponding bits:
1010
^ 0101
-----
1111 (Result in binary)
The result of 1111
in binary is 15
in decimal.
Explanation:
The XOR operation checks each bit:
- 1 XOR 0 = 1
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- 0 XOR 1 = 1
Since all corresponding bits are different, the result is 1111
in binary, which equals 15
in decimal.
Bitwise NOT: ~ operator is used to perform a bitwise NOT operation on an integer
The bitwise NOT (~
) operator inverts all the bits of a number. It changes each 1
to 0
and each 0
to 1
. Additionally, Python represents integers using a two’s complement system for signed numbers. As a result, the bitwise NOT operation effectively computes the result as -(n + 1)
.
Here’s the explanation for the given example:
Binary Representation of a
:
a = 10
→00000000 00000000 00000000 00001010
(in a 32-bit binary representation, for clarity).
Inverting the Bits: The NOT operator flips all bits:
00000000 00000000 00000000 00001010 (original)
11111111 11111111 11111111 11110101 (inverted)
Two’s Complement Representation: In Python, integers are signed, and the result of ~a
corresponds to -(a + 1)
.
For a = 10
, the calculation is:
~10 = -(10 + 1) = -11
The bitwise NOT flips the bits of 10
and represents it in the two’s complement form, which results in -11
.
This behavior reflects how Python handles signed integers in a binary system.
Left Shift: << operator is used to shift the bits of an integer to the left by a specified number of positions
How the Left Shift Operator Works:
- Binary Representation of
a
:10
in binary is0000 1010
(in an 8-bit format for clarity). - Left Shift by 2 Positions:
The bits are shifted 2 positions to the left, and0
s are added to the right:
Original: 0000 1010
Left Shift: 0010 1000
The new binary value 0010 1000
corresponds to the decimal value 40
.
Formula for Left Shift:
The left shift operation effectively multiplies the number by 2
raised to the power of the number of shifts:
a << n = a × (2^n)
For this example:
10 << 2 = 10 × (2^2) = 10 × 4 = 40
The value of b
will be 40
.
Right Shift: >>
operator is used to shift the bits of an integer to the right by a specified number of positions.
How the Right Shift Operator Works:
- Binary Representation of
a
:10
in binary is0000 1010
(in an 8-bit format for clarity). - Right Shift by 2 Positions:
The bits are shifted 2 positions to the right, and0
s are added to the left
Original: 0000 1010
Right Shift: 0000 0010
The new binary value 0000 0010
corresponds to the decimal value 2
.
Formula for Right Shift:
The right shift operation effectively divides the number by 2
raised to the power of the number of shifts (truncating the result to an integer):
a >> n = a // (2^n)
For this example:
10 >> 2 = 10 // (2^2) = 10 // 4 = 2
The value of b
will be 2
These bitwise operators can be used to perform various lowlevel operations on integers, such as testing for the presence of a particular bit, setting or clearing bits, and more.
Membership Operators
Membership operators in Python are used to test if a value or an object is a member of a sequence, such as a list, tuple, or string. The following are the membership operators in Python:
in operator: returns True if a value is found in a sequence and False otherwise.
not in operator: returns True if a value is not found in a sequence and False otherwise
Membership operators are often used in conditional statements to make decisions based on the presence of a value in a sequence.
Identity Operators
Identity operators in Python are used to compare the memory addresses of two objects to determine if they are the same object or not. The following are the identity operators in Python:
is operator: returns True if both objects are the same object in memory and False otherwise
is not operator: returns True if both objects are not the same object in memory and False otherwise
The is and is not operators are used to check if two objects are the same object in memory or not. They are useful in situations where you want to compare the memory addresses of objects rather than their values.
Operator Precedence in Python
Operator precedence in Python refers to the order in which operations are performed in an expression. Python follows the mathematical rules of operator precedence, which determine the order in which operations are executed.
Operator Association
Operator association in Python refers to the direction in which operations are performed in an expression. Operations can be associated from either left-to-right or right-to-left.
Precedence Level | Operator(s) | Description | Associativity |
---|---|---|---|
1 (Highest) | () | Parentheses (used for grouping) | N/A |
2 | ** | Exponentiation | Right-to-left |
3 | +x, -x, ~x | Unary plus, unary minus, bitwise NOT | Right-to-left |
4 | *, /, //, % | Multiplication, division, floor division, modulus | Left-to-right |
5 | +, – | Addition, subtraction | Left-to-right |
6 | <<, >> | Bitwise left and right shift | Left-to-right |
7 | & | Bitwise AND | Left-to-right |
8 | ^ | Bitwise XOR | Left-to-right |
9 | | | Bitwise OR | Left-to-right |
10 | ==, !=, >, <, >=, <=, is, is not, in, not in | Comparison and membership operators | Left-to-right |
11 | not | Logical NOT | Right-to-left |
12 | and | Logical AND | Left-to-right |
13 (Lowest) | or | Logical OR | Left-to-right |
To buy Python books click the below links
Python Programming
Python Programming 2E
Handbook of Python Programming