DEV Community

Sajin Joe J
Sajin Joe J

Posted on

Operators in Python

In Python, operators are symbols used to perform operations on variables and values.
1. Arithmetic operators
Used for mathematical calculations.

+   Addition             10 + 5      15
-   Subtraction          10 - 5      5
*   Multiplication       10 * 5      50
/   Division             10 / 5      2.0
//  Floor division       10 // 3     3
%   Modulus (remainder)  10 % 3      1
**  Power                2 ** 3      8
Enter fullscreen mode Exit fullscreen mode

2. Comparison operators
Used to compare two values. The result is True or False.

==  Equal to                    5 == 5  True
!=  Not equal to                5 != 3  True
>   Greater than                5 > 3   True
<   Less than                   5 < 3   False
>=  Greater than or equal to    5 >= 5  True
<=  Less than or equal to       3 <= 5  True
Enter fullscreen mode Exit fullscreen mode

Important: Python uses == for equality. It does not have JavaScript's strict equality operator ===.

3. Assignment operators
Used to assign or update values.

=     x = 10        Assign 10 to x
+=    x += 5        x = x + 5
-=    x -= 5        x = x - 5
*=    x *= 2        x = x * 2
/=    x /= 2        x = x / 2
%=    x %= 3        x = x % 3
//=  x //= 2        x = x // 2
**=  x **= 2        x = x ** 2
Enter fullscreen mode Exit fullscreen mode

4. Logical operators
Used to combine conditions.

and -Both conditions must be true          5 > 2 and 8 > 3  True
or  -At least one condition must be true   5 < 2 or 8 > 3   True
not -Reverses the result                   not True         False
Enter fullscreen mode Exit fullscreen mode

Example:

age = 25

if age >= 18 and age <= 60:
    print("Eligible")
Enter fullscreen mode Exit fullscreen mode

Output:

Eligible
Enter fullscreen mode Exit fullscreen mode

5. Membership operators
Check whether a value exists inside a sequence, such as a string or list.

in           "a" in "apple"         True
not in       "z" not in "apple"     True
Enter fullscreen mode Exit fullscreen mode

Example:

fruits = ["apple", "banana", "orange"]

print("apple" in fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

True
Enter fullscreen mode Exit fullscreen mode

**

  1. Identity operators** Check whether two variables refer to the same object.
is              Same object
is not          Not the same object
Enter fullscreen mode Exit fullscreen mode
a = None

print(a is None)  # True
Enter fullscreen mode Exit fullscreen mode

Use is None to check whether a variable is None. Do not use is as a replacement for == when comparing ordinary values.

7. Bitwise operators
Bitwise operators in Python work on the binary representation (0s and 1s) of integers.

&   AND
\|  OR
^   XOR
~   NOT
<<  Left shift
>>  Right shift
Enter fullscreen mode Exit fullscreen mode

1. Examples of bitwise operators
& — AND
Returns 1 only when both bits are 1.

a = 5   # Binary: 0101
b = 3   # Binary: 0011

print(a & b)
Enter fullscreen mode Exit fullscreen mode

Output: 1

| — OR
Returns 1 when at least one bit is 1.

print(5 | 3)
Enter fullscreen mode Exit fullscreen mode

Output: 7
Explanation:

  0101   (5)
| 0011   (3)
------
  0111   (7)
Enter fullscreen mode Exit fullscreen mode

^ — XOR
Returns 1 when the two bits are different.

print(5 ^ 3)
Enter fullscreen mode Exit fullscreen mode

Output: 6

Explanation:

  0101   (5)
^ 0011   (3)
------
  0110   (6)
Enter fullscreen mode Exit fullscreen mode

~ — NOT
Reverses the bits. In Python, ~x equals -x - 1.

print(~5)
Enter fullscreen mode Exit fullscreen mode

Output: -6

<< — Left shift
Shifts bits to the left. For non-negative integers, shifting left by one position multiplies the value by 2.

print(5 << 1)
print(5 << 2)
Enter fullscreen mode Exit fullscreen mode

Output:

10
20
Enter fullscreen mode Exit fullscreen mode

Explanation:

5  = 0101
5 << 1 = 1010  (10)
5 << 2 = 10100 (20)
Enter fullscreen mode Exit fullscreen mode

>> — Right shift
Shifts bits to the right. For non-negative integers, shifting right by one position is equivalent to floor-dividing by 2.

print(5 >> 1)
print(5 >> 2)
Enter fullscreen mode Exit fullscreen mode

Output:

2
1
Enter fullscreen mode Exit fullscreen mode

Explanation:

5 = 0101
5 >> 1 = 0010 (2)
5 >> 2 = 0001 (1)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)