Operators: Assignment, Unary, Binary, Arithmetic, Relational, Logical, Bitwise Operator and Membership Operator

Operators are symbols used to perform operations on variables and values. Python provides several types of operators, including Assignment, arithmetic, Relational, Logical, Bitwise, and Membership operators.

1. Assignment Operators (=)

Assignment operators are used to assign values to variables. Python also provides shorthand assignment operators for performing operations and assignment simultaneously.

Operator Example Equivalent To Description
= a = 10 a = 10 Assigns value to variable
+= a += 5 a = a + 5 Adds and assigns
-= a -= 5 a = a - 5 Subtracts and assigns
*= a *= 2 a = a * 2 Multiplies and assigns
/= a /= 2 a = a / 2 Divides and assigns
//= a //= 3 a = a // 3 Floor division and assigns
%= a %= 3 a = a % 3 Modulus and assigns
**= a **= 2 a = a ** 2 Exponentiation and assigns

2. Unary Operators

Unary operators work on a single operand.

Operator Example Description
+ +a Represents positive value (default)
- -a Negates the value
~ ~a Bitwise NOT (inverts bits)

Example:

a = 5
print(-a) # Output: -5
print(~a) # Output: -6 (inverts bits)

3. Binary Operators

Binary operators work on two operands. They include arithmetic, relational, logical, and bitwise operators.

Example:

x = 10
y = 5
print(x + y) # Addition (Binary Operator)
print(x > y) # Relational (Binary Operator)

4. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

Operator Example Result
+ a + b Addition
- a - b Subtraction
* a * b Multiplication
/ a / b Division (float)
// a // b Floor division (removes decimal part)
% a % b Modulus (remainder)
** a ** b Exponentiation (power)

Example:

x = 10
y = 3
print(x / y) # Output: 3.3333
print(x // y) # Output: 3
print(x ** y) # Output: 1000 (10^3)

5. Relational (Comparison) Operators

Relational operators compare values and return True or False.

Operator Example Description
== a == b 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

Example:

a = 10
b = 20
print(a == b) # False
print(a < b) # True

6. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Example Description
and a > 5 and b < 10 Returns True if both conditions are True
or a > 5 or b < 10 Returns True if at least one condition is True
not not(a > b) Reverses the condition (TrueFalse and vice versa)

Example:

a = 5
b = 10
print(a > 2 and b < 15) # True
print(a > 10 or b < 15) # True
print(not(a > 10)) # True

7. Bitwise Operators

Bitwise operators work on binary numbers (bitwise operations).

Operator Example Description
& a & b Bitwise AND
` ` `a
^ a ^ b Bitwise XOR
~ ~a Bitwise NOT
<< a << 2 Left shift (multiply by 2^n)
>> a >> 2 Right shift (divide by 2^n)

Example:

a = 5 # 101 in binary
b = 3 # 011 in binary
print(a & b) # Output: 1 (001 in binary)
print(a | b) # Output: 7 (111 in binary)
print(a ^ b) # Output: 6 (110 in binary)
print(a << 1) # Output: 10 (Shifts left by 1 bit)
print(a >> 1) # Output: 2 (Shifts right by 1 bit)

8. Membership Operators

Membership operators check whether a value exists in a sequence (like a list, tuple, or string).

Operator Example Description
in x in list Returns True if x exists in the list
not in x not in list Returns True if x does not exist in the list

Example:

fruits = [“apple”, “banana”, “cherry”]
print(“apple” in fruits) # True
print(“grape” not in fruits) # True

Leave a Reply

error: Content is protected !!