Operators in Python
In Python, an operator is a special symbol or keyword used to perform operations on one or more operands. Operators are fundamental to programming and are used to manipulate data and variables. They allow you to perform various types of operations, such as arithmetic calculations, comparisons, logical operations, and bitwise manipulations.
Types of Operators in Python
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, division, multiplication etc.
The following table shows all the arithmetic operators:
Operator | Operator Name | Description | Example |
---|---|---|---|
+ | Addition | Adds two operands. | 5 + 3 results in 8 |
- | Subtraction | Subtracts the second operand from the first operand. | 5 - 3 results in 2 |
* | Multiplication | Multiplies two operands. | 5 * 3 results in 15 |
/ | Division | Divides the first operand by the second operand (float result). | 5 / 2 results in 2.5 |
// | Floor Division | Divides and returns the largest integer less than or equal to the result. | 5 // 2 results in 2 |
% | Modulus | Returns the remainder of the division. | 5 % 2 results in 1 |
** | Exponentiation | Raises the first operand to the power of the second operand. | 5 ** 2 results in 25 |
Example:
Assignment Operators
Assignment operators are used to assign values to variables. Python provides several types of assignment operators: simple assignment and compound (or shorthand) assignment operators.
Simple Assignment Operator
The simple assignment operator =
assigns the value on the right-hand side to the variable on the left-hand side.
Syntax:
Example:
Compound Assignment Operators
Compound assignment operators combine an arithmetic operation with assignment. They modify the variable's value and then assign the result back to the variable. This can make the code more concise and easier to read.
Here are the common compound assignment operators:
Operator | Operator Name | Description | Example |
---|---|---|---|
+= | Addition Assignment | Adds the right operand to the left operand and assigns the result to the left operand. | x += 3 (equivalent to x = x + 3) |
-= | Subtraction Assignment | Subtracts the right operand from the left operand and assigns the result to the left operand. | x -= 3 (equivalent to x = x - 3) |
*= | Multiplication Assignment | Multiplies the left operand by the right operand and assigns the result to the left operand. | x *= 3 (equivalent to x = x * 3) |
/= | Division Assignment | Divides the left operand by the right operand and assigns the result to the left operand. | x /= 2 (equivalent to x = x / 2) |
//= | Floor Division Assignment | Divides the left operand by the right operand using floor division and assigns the result to the left operand. | x //= 2 (equivalent to x = x // 2) |
%= | Modulus Assignment | Applies modulus operation and assigns the result to the left operand. | x %= 2 (equivalent to x = x % 2) |
**= | Exponentiation Assignment | Raises the left operand to the power of the right operand and assigns the result to the left operand. | x **= 2 (equivalent to x = x ** 2) |
Example:
Python does not support the ++
(increment) and --
(decrement) operators
that are found in languages like C++, Java, or JavaScript.
Comparison Operators
Comparison operators in Python are used to compare values and return a Boolean result (True or False). These operators are essential for control flow and making decisions in your code. The operands can be numbers, strings, or boolean values. Strings are compared letter by letter using their ASCII values. For instance, 'E' is less than 'S' because the ASCII value of 'E' (69) is less than the ASCII value of 'S' (83).
The following table shows all the comparison operators:
Operator | Operator Name | Description | Example |
---|---|---|---|
== | Equal to | Checks if two operands are equal. | 5 == 5 results in True |
!= | Not equal to | Checks if two operands are not equal. | 5 != 3 results in True |
> | Greater than | Checks if the first operand is greater than the second operand. | 5 > 3 results in True |
< | Less than | Checks if the first operand is less than the second operand. | 5 < 3 results in False |
>= | Greater than or equal to | Checks if the first operand is greater than or equal to the second operand. | 5 >= 5 results in True |
<= | Less than or equal to | Checks if the first operand is less than or equal to the second operand. | 5 <= 3 results in False |
Example:
Don't confuse the equality operator (==
) with the assignment operator (=
).
-
The equality operator (
==
) is used to compare two values. It checks if the values are the same and returnsTrue
if they are, andFalse
otherwise.Example:
-
The assignment operator (
=
) is used to assign a value to a variable. It sets the value on the right side to the variable on the left side.Example:
In summary, x == y
checks if x
is equal to y
, while x = y
assigns the value of y
to x
.
Logical Operators
In Python, logical operators are used to combine multiple conditions together and evaluate them as a single boolean expression.
There are three types of logical operators in Python: and
, or
, and not
.
The and
operator returns True
if both conditions it is evaluating are true, otherwise it returns False
.
Here is list of logical operators:
Operator | Operator Name | Description | Example |
---|---|---|---|
and | Logical AND | Returns True if both operands are true. | True and False results in False |
or | Logical OR | Returns True if at least one of the operands is true. | True or False results in True |
not | Logical NOT | Returns True if the operand is false. | not True results in False |
Example:
and
Operator
The and
operator returns True
if both operands are true. If either operand is false, it returns False
.
Example:
or
Operator
The or
operator returns True
if at least one of the operands is true. It returns False
only if both operands are false.
Example:
not
Operator
The not
operator negates the boolean value of the operand. It returns True
if the operand is False
, and False
if the operand is True
.
Example:
Bitwise Operators
Bitwise operators in Python are used to perform operations at the bit level of integer values. These operators work on binary representations of numbers, performing bit-by-bit operations. A sequence consisting of ones and zeroes is known as binary. The smallest amount of information that you can store in a computer is known as a bit. A bit is represented as either 0 or 1.
Here is list of bitwise operators:
Operator | Operator Name | Description | Example |
---|---|---|---|
& | Bitwise AND | Performs bitwise AND operation between two integers. | 5 & 3 results in 1 |
| | Bitwise OR | Performs bitwise OR operation between two integers. | 5 | 3 results in 7 |
^ | Bitwise XOR | Performs bitwise XOR operation between two integers. | 5 ^ 3 results in 6 |
~ | Bitwise NOT | Performs bitwise NOT operation, inverts all bits. | ~5 results in -6 |
<< | Bitwise Left Shift | Shifts the bits of the first operand to the left by the number of positions specified by the second operand. | 5 << 1 results in 10 |
>> | Bitwise Right Shift | Shifts the bits of the first operand to the right by the number of positions specified by the second operand. | 5 >> 1 results in 2 |
Bitwise AND (&
)
The bitwise AND operator compares each bit of its two operands. If both bits are 1
, the corresponding result bit is set to 1
. Otherwise, it is set to 0
.
Example:
Bitwise OR (|
)
The bitwise OR operator compares each bit of its two operands. If at least one of the bits is 1
, the corresponding result bit is set to 1
. Otherwise, it is set to 0
.
Example:
Bitwise XOR (^
)
The bitwise XOR (exclusive OR) operator compares each bit of its two operands. If exactly one of the bits is 1
, the corresponding result bit is set to 1
. Otherwise, it is set to 0
.
Example:
Bitwise NOT (~
)
The bitwise NOT operator is a unary operator that inverts all the bits of its operand. It performs a bitwise negation. In Python, it returns the two's complement of the number.
Example:
Bitwise Left Shift (<<
)
The left shift operator shifts the bits of its operand to the left by the number of positions specified. The vacant positions on the right are filled with 0
.
Example:
Bitwise Right Shift (>>
)
The right shift operator shifts the bits of its operand to the right by the number of positions specified. The vacant positions on the left are filled based on the sign bit (0 for positive numbers, 1 for negative numbers in two's complement).
Example:
Precedence and Associativity
In Python, operators have a specific precedence and associativity that determine the order in which operations are performed in an expression.
Operator Precedence
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before those with lower precedence. Here's a list of Python operators, ordered by their precedence from highest to lowest:
Operator | Description |
---|---|
Parentheses () | Used to group expressions and override default precedence. |
Exponentiation ** | Example: 2 ** 3 results in 8 . |
Unary Plus and Minus, Bitwise NOT +x , -x , ~x | Unary plus (+ ) and minus (- ) are used for positive and negative values respectively. Bitwise NOT (~ ) inverts all the bits of its operand. |
Multiplication, Division, Floor Division, Modulus * , / , // , % | Multiplication (* ), Division (/ ), Floor Division (// ), and Modulus (% ) are evaluated left to right. |
Addition and Subtraction + , - | Addition (+ ) and subtraction (- ) are evaluated left to right. |
Bitwise Shift Operators << , >> | Left shift (<< ) and right shift (>> ) are evaluated left to right. |
Bitwise AND & | Bitwise AND is evaluated. |
Bitwise XOR ^ | Bitwise XOR is evaluated. |
Bitwise OR | | Bitwise OR is evaluated. |
Comparison Operators == , != , > , < , >= , <= | Comparison operators are evaluated. |
Logical NOT not | Logical NOT (not ) has higher precedence than the logical AND and OR operators. |
Logical AND and | Logical AND (and ) is evaluated. |
Logical OR or | Logical OR (or ) is evaluated. |
Conditional Expressions if-else | Conditional expressions have the lowest precedence. |
Assignment Operators = , += , -= , *= , /= , etc. | Assignment and other compound assignment operators are evaluated. |
Expressions separated by commas , | Used in tuples, function arguments, etc. |
Operator Associativity
Operator associativity determines the order in which operators of the same precedence are evaluated. Most operators in Python are left-associative, meaning they are evaluated from left to right. Here’s a summary of associativity for different types of operators:
-
Left-Associative: Most operators, including arithmetic (
+
,-
,*
,/
), bitwise operators (&
,|
), and comparison operators (==
,<
,>
). They are evaluated from left to right. -
Right-Associative: Exponentiation (
**
) and assignment operators (=
,+=
,-=
, etc.) are right-associative. This means expressions likea = b = c
are evaluated asa = (b = c)
. -
Non-Associative: Operators such as comparison operators (
==
,>
,<
) do not have associativity, meaning expressions involving these operators are evaluated based on precedence alone.
Examples:
Here's how precedence and associativity affect the evaluation of an expression:
Last updated on -