Python Basic Operators
Python Basic Operators
Section titled “Python Basic Operators”Operators are special symbols or keywords in Python that perform operations on values. These values are called operands.
For example, in the expression 4 + 5, the + is the operator, and 4 and 5 are the operands. The expression evaluates to 9.
Types of Operators
Section titled “Types of Operators”Python supports several types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let’s explore each type.
Python Arithmetic Operators
Section titled “Python Arithmetic Operators”These operators perform standard mathematical calculations. Assume a = 10 and b = 20.
| Operator | Description | Example (a=10, b=20) |
|---|---|---|
+ Addition | Adds the values of the operands. | a + b results in 30 |
- Subtraction | Subtracts the right operand from the left operand. | a - b results in -10 |
* Multiplication | Multiplies the values of the operands. | a * b results in 200 |
/ Division | Divides the left operand by the right operand. Always results in a float. | b / a results in 2.0 |
% Modulus | Returns the remainder of the division. | b % a results in 0. 13 % a results in 3. |
** Exponentiation | Raises the left operand to the power of the right operand. | a ** 2 results in 100 (10 to the power of 2) |
// Floor Division | Divides and rounds the result down to the nearest whole number (integer). | 9 // 2 results in 4. 9.0 // 2.0 results in 4.0. |
Python Comparison Operators
Section titled “Python Comparison Operators”These operators compare two values and return a Boolean result (True or False). They are also known as Relational Operators. Assume a = 10 and b = 20.
| Operator | Description | Example (a=10, b=20) |
|---|---|---|
== Equal to | Returns True if both operands are equal. | a == b is False. a == 10 is True. |
!= Not equal to | Returns True if operands are not equal. | a != b is True. a != 10 is False. |
> Greater than | Returns True if the left operand is greater than the right. | a > b is False. |
< Less than | Returns True if the left operand is less than the right. | a < b is True. |
>= Greater than or equal to | Returns True if the left operand is greater than or equal to the right. | a >= b is False. a >= 10 is True. |
<= Less than or equal to | Returns True if the left operand is less than or equal to the right. | a <= b is True. a <= 10 is True. |
Note: The <> operator is deprecated in Python 3; use != instead.
Python Assignment Operators
Section titled “Python Assignment Operators”These operators assign values to variables, often combining an arithmetic operation with assignment. Assume a = 10, b = 20, and c = 0 initially.
| Operator | Description | Example (a=10) |
|---|---|---|
= Assign | Assigns the value of the right operand to the left operand. | c = a + b assigns 30 to c. |
+= Add AND | Adds the right operand to the left operand and assigns the result back to the left operand. | c += a is equivalent to c = c + a. If c was 0, c becomes 10. |
-= Subtract AND | Subtracts the right operand from the left and assigns the result to the left operand. | c -= a is equivalent to c = c - a. |
*= Multiply AND | Multiplies the left operand by the right and assigns the result to the left operand. | c *= a is equivalent to c = c * a. |
/= Divide AND | Divides the left operand by the right and assigns the result (float) to the left operand. | c /= a is equivalent to c = c / a. |
%= Modulus AND | Takes the modulus and assigns the result to the left operand. | c %= a is equivalent to c = c % a. |
**= Exponent AND | Raises the left operand to the power of the right and assigns the result to the left operand. | c **= a is equivalent to c = c ** a. |
//= Floor Divide AND | Performs floor division and assigns the result to the left operand. | c //= a is equivalent to c = c // a. |
Python Bitwise Operators
Section titled “Python Bitwise Operators”Bitwise operators act on operands as sequences of binary digits (bits). They are often used for low-level manipulation or optimization. Assume a = 60 and b = 13.
Binary representations:
`a = 60 -> 0011 1100``b = 13 -> 0000 1101`| Operator | Description | Example (a=60, b=13) |
|---|---|---|
& Bitwise AND | Sets each bit to 1 if both corresponding bits are 1. | a & b results in 12 (0000 1100) |
| Bitwise OR | Sets each bit to 1 if at least one of the corresponding bits is 1. | a | b results in 61 (0011 1101) |
^ Bitwise XOR | Sets each bit to 1 if only one of the corresponding bits is 1. | a ^ b results in 49 (0011 0001) |
~ Bitwise NOT (Complement) | Inverts all the bits (0 becomes 1, 1 becomes 0). Applied to one operand. Result depends on the number representation (e.g., two’s complement for signed integers). | ~a results in -61. The result is -(a + 1) for positive integers in typical two’s complement systems. |
<< Left Shift | Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Fills with zeros from the right. | a << 2 results in 240 (1111 0000) |
>> Right Shift | Shifts the bits of the left operand to the right by the number of positions specified by the right operand. Fills with the sign bit from the left (arithmetic shift) or zeros (logical shift - Python does arithmetic). | a >> 2 results in 15 (0000 1111) |
Python Logical Operators
Section titled “Python Logical Operators”These operators combine conditional statements. Assume x = True and y = False.
| Operator | Description | Example (x=True, y=False) |
|---|---|---|
and Logical AND | Returns True if both operands are true. | x and y is False. |
or Logical OR | Returns True if at least one operand is true. | x or y is True. |
not Logical NOT | Returns True if the operand is false, and False if the operand is true. | not x is False. not y is True. |
Python Membership Operators
Section titled “Python Membership Operators”These operators test if a sequence (like string, list, tuple, set, dictionary keys) contains a specific item. Assume my_list = [1, 2, 3].
| Operator | Description | Example (my_list=[1, 2, 3]) |
|---|---|---|
in | Returns True if the value/variable is found in the sequence. | 2 in my_list is True. |
not in | Returns True if the value/variable is not found in the sequence. | 4 not in my_list is True. |
Python Identity Operators
Section titled “Python Identity Operators”These operators compare the memory locations of two objects, checking if they refer to the exact same object instance. Assume x = [1, 2], y = [1, 2], z = x.
| Operator | Description | Example (x=[1,2], y=[1,2], z=x) |
|---|---|---|
is | Returns True if both variables point to the same object in memory. | x is z is True (z references the same list as x). x is y is False (x and y are different list objects, even if their contents are identical). |
is not | Returns True if both variables point to different objects in memory. | x is not y is True. x is not z is False. |
Note: Use is for identity comparison (same object), and == for equality comparison (same value).
Python Operators Precedence
Section titled “Python Operators Precedence”When an expression contains multiple operators, Python evaluates them in a specific order based on precedence. Higher precedence operators are evaluated first. Parentheses () can be used to override the default precedence.
The following table lists operators from highest precedence (top) to lowest (bottom):
| Operator | Description |
|---|---|
() | Parentheses (grouping) |
f(args...), x[index], x[index:slice], x.attribute | Function calls, subscripting, slicing, attribute reference |
await x | Await expression |
** | Exponentiation (right-associative) |
+x, -x, ~x | Positive, negative, bitwise NOT (unary) |
*, /, //, % | Multiplication, division, floor division, modulus |
+, - | Addition and subtraction |
<<, >> | Bitwise left and right shifts |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, including membership and identity tests |
not x | Logical NOT |
and | Logical AND |
or | Logical OR |
if else | Conditional expression |
lambda | Lambda expression |
:= | Assignment expression (Walrus operator) |
Understanding operator precedence helps in writing predictable and correct code.