Python Operators
Operators in Python
Operators are special symbols in Python that perform operations on variables and values. Python supports various types of operators, each performing a different operation on data. These include arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
** | Exponentiation (power) | a ** b |
// | Floor Division (integer div) | a // b |
Example:
2. Comparison Operators
Comparison operators compare two values and return either True
or False
.
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both conditions are true | a and b |
or | Returns True if at least one condition is true | a or b |
not | Reverses the result, returns False if the condition is true | not a |
Example:
4. Assignment Operators
Assignment operators are used to assign values to variables. You can also combine assignment with arithmetic operations.
Operator | Description | Example |
---|---|---|
= | Assign value | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 3 |
*= | Multiply and assign | a *= 2 |
/= | Divide and assign | a /= 4 |
%= | Modulus and assign | a %= 2 |
**= | Exponent and assign | a **= 2 |
//= | Floor divide and assign | a //= 3 |
Example:
5. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Bitwise left shift | a << 2 |
>> | Bitwise right shift | a >> 2 |
Example:
6. Identity Operators
Identity operators are used to compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if both variables refer to the same object | a is b |
is not | Returns True if they do not refer to the same object | a is not b |
Example:
7. Membership Operators
Membership operators are used to test whether a value is present in a sequence (such as a string, list, or tuple).
Operator | Description | Example |
---|---|---|
in | Returns True if the value is present in the sequence | x in list |
not in | Returns True if the value is not present in the sequence | x not in list |
Example:
Operator Precedence
Python follows a specific order when evaluating expressions with multiple operators. The order (from highest to lowest) is:
- Parentheses:
()
- Exponentiation:
**
- Multiplication, Division, Floor Division, Modulus:
*
,/
,//
,%
- Addition, Subtraction:
+
,-
- Bitwise Shift:
<<
,>>
- Bitwise AND:
&
- Bitwise OR, XOR:
|
,^
- Comparisons:
==
,!=
,<
,>
,<=
,>=
- Identity, Membership:
is
,is not
,in
,not in
- Logical NOT:
not
- Logical AND:
and
- Logical OR:
or
Example:
Summary:
- Python provides several types of operators for performing arithmetic, comparison, logical, and bitwise operations, as well as assignment, identity, and membership tests.
- Operator precedence determines the order in which operations are evaluated.