Powered by AI
Copilot
Operators in Python
Python supports various types of operators, which are used to perform operations on variables and values. Here
are the main categories:
1. Arithmetic Operators:
+ (Addition): Adds two operands.
- (Subtraction): Subtracts the second operand from the first.
* (Multiplication): Multiplies two operands.
/ (Division): Divides the first operand by the second.
% (Modulus): Returns the remainder of the division.
** (Exponentiation): Raises the first operand to the power of the second.
// (Floor Division): Divides the first operand by the second and returns the largest integer less than or
equal to the result.
2. Comparison Operators:
== (Equal): Checks if two operands are equal.
!= (Not Equal): Checks if two operands are not equal.
> (Greater than): Checks if the first operand is greater than the second.
< (Less than): Checks if the first operand is less than the second.
>= (Greater than or equal to): Checks if the first operand is greater than or equal to the second.
<= (Less than or equal to): Checks if the first operand is less than or equal to the second.
3. Logical Operators:
and: Returns True if both operands are true.
or: Returns True if at least one of the operands is true.
not: Returns True if the operand is false.
4. Bitwise Operators:
& (AND): Performs a bitwise AND operation.
| (OR): Performs a bitwise OR operation.
^ (XOR): Performs a bitwise XOR operation.
~ (NOT): Performs a bitwise NOT operation.
<< (Left Shift): Shifts the bits of the first operandleft by the number of positions specified by the
second operand.
>> (Right Shift): Shifts the bits of the first operand right by the number of positions specified by the
second operand.
5. Assignment Operators:
=: Assigns a value to a variable.
+=: Adds and assigns the result to the variable.
-=: Subtracts and assigns the result to the variable.
*=: Multiplies and assigns the result to the variable.
/=: Divides and assigns the result to the variable.
%=: Takes modulus and assigns the result to the variable.
**=: Raises to the power and assigns the result to the variable.
//=: Performs floor division and assigns the result to the variable.
6. Identity Operators:
is: Checks if two variables refer to the same object.
is not: Checks if two variables do not refer to the same object.
7. Membership Operators:
in: Checks if a value is present in a sequence.
not in: Checks if a value is not present in a sequence.
Operator Precedence
Operator precedence determines the order in which operations are performed in an expression. Operators with
higher precedence are evaluated before operators with lower precedence. Here is a summary of operator
precedence in Python, from highest to lowest:
1. () (Parentheses)
2. ** (Exponentiation)
3. +x, -x, ~x (Unary plus, Unary minus, Bitwise NOT)
4. *, /, //, % (Multiplication, Division, Floor division, Modulus)
5. +, - (Addition, Subtraction)
6. <<, >> (Bitwise shift operators)
7. & (Bitwise AND)
8. ^ (Bitwise XOR)
9. | (Bitwise OR)
10. in, not in, is, is not, <, <=, >, >=, !=, == (Comparisons, Identity, Membership operators)
11. not (Logical NOT)
12. and (Logical AND)
13. or (Logical OR)
Associativity
When operators of the same precedence appear in an expression, their associativity determines the order of
evaluation:
Left-to-right: Most operators, including +, -, *, /, //, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=, is, is not, in, not
in, and, or.
Right-to-left: Exponentiation (**), Unary operators (+x, -x, ~x), and assignment operators (=, +=, -=, *=, /=,
%=, **=, //=).
Examples
# Example of operator precedence
result = 10 + 2 * 3 # Multiplication has higher precedence than addition
print(result) # Output: 16
# Example of using parentheses to change precedence
result = (10 + 2) * 3 # Parentheses have the highest precedence
print(result) # Output: 36
# Example of associativity
result = 2 ** 3 ** 2 # Exponentiation is right-to-left associative
print(result) # Output: 512 (2 ** (3 ** 2))
If you have any specific questions or need further examples, feel free to ask!
: PythonGeeks : Programiz