Operators are special symbols or keywords that perform operations on variables and values.
Python supports various types of operators, including
Arithmetic
Bitwise
Assignment
Comparison
Membership
Identity
logical
Arithmetic Operators :-
These are used to perform mathematical operations like
- Addition
( + )
:- Returns addition of two values. a = 5 b = 3 result = a + b print(result) # Output: 8
- Subtraction
( - )
:- Returns subtraction of two values. a = 10 b = 7 result = a - b print(result) # Output: 3
- Multiplication
( * )
:- Returns multiplication of two values. a = 4 b = 6 result = a * b print(result) # Output: 24
- Division
( / )
:- Returns division of two values. a = 20 b = 4 result = a / b print(result) # Output: 5.0
- Modulo
( % )
:- Returns reminder of numbers. a = 17 b = 5 result = a % b print(result) # Output: 2
- Floor Division
( // )
:- Returns output of floor division a = 17 b = 5 result = a // b print(result) # Output: 3
- Exponentiation
( ** )
:- Return value of ab where a and are values a = 2 b = 3 result = a ** b print(result) # Output: 8
Bitwise Operators :-
These operators perform bitwise operations on integers. They treat operands as binary numbers and operate bit by bit.
- AND
( & )
: It returns a new integer where each bit is set to 1 only if the corresponding bits of both operands are 1. a = 0b0000000000000101 # 16-bit binary representation of 5 b = 0b0000000000000011 # 16-bit binary representation of 3 result = a & b # 0b0000000000000001 in binary, which is 1 in decimal print(result) # Output: 1
- OR
( | )
: It returns a new integer where each bit is set to 1 if at least one of the corresponding bits of the operands is 1. a = 0b0000000000000101 # 16-bit binary representation of 5 b = 0b0000000000000011 # 16-bit binary representation of 3 result = a | b # 0000000000000111 in binary, which is 7 in decimal print(result) # Output: 7
- XOR
( ^ )
: It returns a new integer where each bit is set to 1 if the corresponding bits of the operands are different. a = 0b0000000000000101 # 16-bit binary representation of 5 b = 0b0000000000000011 # 16-bit binary representation of 3 result = a ^ b # 0000000000000110 in binary, which is 6 in decimal print(result) # Output: 6
- NOT
( ~ )
: It returns the complement of the integer. a = 0b0000000000000101 # 16-bit binary representation of 5 result = ~a # -0000000000000110 (complement of 0101 in binary), NOT operation inverts each bit print(result) # Output: -6
- Left Shift
( << )
: Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are shifted in from the right. a = 0b0000000000000101 # 16-bit binary representation of 5 result = a << 1 # 0000000000001010 in binary, which is 10 in decimal print(result) # Output: 10
- Right Shift
( >> )
: Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Zeros are shifted in from the left. a = 0b0000000000000101 # 16-bit binary representation of 5 result = a >> 1 # 0000000000000010 in binary, which is 2 in decimal print(result) # Output: 2
Assignment Operators :-
These are used to assign values to variables.
+=
Addition Assignment Operator :-# += (Addition Assignment Operator) Example x = 5 x += 3 # Equivalent to x = x + 3 print("x after addition:", x) # Output: 8
-=
Subtraction Assignment Operator :-# -= (Subtraction Assignment Operator) Example x = 10 x -= 4 # Equivalent to x = x - 4 print("x after subtraction:", x) # Output: 6
*=
Multiplication Assignment Operator :-# *= (Multiplication Assignment Operator) Example x = 3 x *= 2 # Equivalent to x = x * 2 print("x after multiplication:", x) # Output: 6
/=
Division Assignment Operator :-# /= (Division Assignment Operator) Example x = 10 x /= 5 # Equivalent to x = x / 5 print("x after division:", x) # Output: 2.0 (result is a float)
%=
Modulus Assignment Operator :-# %= (Modulus Assignment Operator) Example x = 17 x %= 5 # Equivalent to x = x % 5 print("x after modulus operation:", x) # Output: 2
//=
Floor Division Assignment Operator :-# //= (Floor Division Assignment Operator) Example x = 17 x //= 5 # Equivalent to x = x // 5 print("x after floor division:", x) # Output: 3
**=
Exponentiation Assignment Operator :-# **= (Exponentiation Assignment Operator) Example x = 2 x **= 3 # Equivalent to x = x ** 3 print("x after exponentiation:", x) # Output: 8
Comparison Operators :-
These operators are used to compare values.
- Equal to
( == )
: - Checks if two values are equal. x = 5 y = 5 if x == y: print("x is equal to y") else: print("x is not equal to y")
- Not equal to
( != )
:- Checks if two values are not equal. x = 5 y = 10 if x != y: print("x is not equal to y") else: print("x is equal to y")
- Less than
( < )
:- Checks if the left operand is less than the right operand. x = 5 y = 10 if x < y: print("x is less than y") else: print("x is not less than y")
- Greater than
( > )
:- Checks if the left operand is greater than the right operand. x = 10 y = 5 if x > y: print("x is greater than y") else: print("x is not greater than y")
- Less than or equal to
( <= )
:- Checks if the left operand is less than or equal to the right operand. x = 5 y = 5 if x <= y: print("x is less than or equal to y") else: print("x is greater than y")
- Greater than or equal to
( >= )
:- Checks if the left operand is greater than or equal to the right operand. x = 10 y = 5 if x >= y: print("x is greater than or equal to y") else: print("x is less than y")
Membership Operators :-
These operators are used to test whether a value is a member of a sequence (like a string, list, or tuple).
in
Operator :-# Checking if a character is in a string string = "Hello, world!" if 'o' in string: print("'o' is present in the string.") else: print("'o' is not present in the string.")
not in
Operator :-# Checking if a number is not in a list numbers = [1, 2, 3, 4, 5] if 6 not in numbers: print("6 is not present in the list.") else: print("6 is present in the list.")
Identity Operator :-
These operators are used to compare the memory locations of two objects.
is
Operator :-# Using the is operator to compare the identity of two objects x = [1, 2, 3] y = [1, 2, 3] print(x is y) # False, because x and y are two different objects in memory
is not
Operator :-# Using the is not operator to compare the identity of two objects a = "hello" b = "world" print(a is not b) # True, because a and b are different objects
Logical Operators :-
These operators are used to combine conditional statements.
and
Operator :-# Using 'and' operator to check if two conditions are true x = 5 y = 10 if x > 0 and y < 15: print("Both conditions are true.") else: print("At least one condition is false.")
or
Operator :-# Using 'or' operator to check if at least one condition is true name = "Alice" age = 25 if name == "Alice" or age == 30: print("At least one condition is true.") else: print("Both conditions are false.")
not
Operator :-# Using 'not' operator to negate a condition is_raining = False if not is_raining: print("It's not raining.") else: print("It's raining.")