Operators are used to performing operations on values.
Operators in Python are divided into the following categories :
- Arithmetic operators
- Assignment operators
- Logical operators
- Comparison operators
- Identity operators
- Membership operators
- Bitwise operators
- Arithmetic operators:
These operators are used to perform mathematical operations (addition, subtraction, multiplication, division) on numerical values.
"+": This operator is used to add two given numbers. "-": This operator is used to subtract a number from the other. "*": This operator is used to multiply two numbers. "/": This operator is used to divide a number by the other. "**": The first number raised to the power second number. "//": This operator is used to Divides the first operand by the second. "%": Finds remainder when the first number is divided by the second number.
Example:
# Assign the operand values x = 9 y = 6 # Addition add = x + y # Subtraction sub = x - y # Multiplication mul = x * y # Division div = x / y # Power exp = x ** y # Floor division floor = x // y # Modulus mod = x % y # Printing outputs print("Addition:",add) print("Subtraction:",sub) print("Multiplication:",mul) print("Division:",div) print("Power:",exp) print("Floor division:",floor) print("Modulus:",mod)
Output:
Addition: 15 Subtraction: 3 Multiplication: 54 Division: 1.5 Power: 531441 Floor division: 1 Modulus: 3
This operator are used to assign values to variables.
"=": Assign value of right side of expression to left side operand. Some of it's uses with different operators are : "+=": This operator is used to Add right side operand with left side operand and then assign to left operand. (x+=y => x=x+y) "-=": This operator is used to Subtract right operand from left operand and then assign to left operand. (x-=y => x=x-y) "*=": This operator is used to Multiply right operand with left operand and then assign to left operand. (x*=y => x=x*y) "/=": This operator is used to Divide left operand with right operand and then assign to left operand. (x/=y => x=x/y) "%=": This operator is used to Takes modulus using left and right operands and assign result to left operand. (x%=y => x=x%y) "//=": This operator is used to Divide left operand with right operand and then assign the value(floor) to left operand. (x//=y => x=x//y) "**=": This operator is used to Calculate exponent(raise power) value using operands and assign value to left operand. (x**=y => x=x**y)
Example:
x = 10 print("value of x is:", x) x += 5 print("x += 5, The updated value of x is:", x) x -= 5 print("x -= 5, The updated value of x is:", x) x *= 5 print("x *= 5, The updated value of x is:", x) x /= 5 print("x /= 5, The updated value of x is:", x) x %= 3 print("x %= 3, The updated value of x is:", x) y = 10 print("\nvalue of y is:", y) y //= 5 print("y //= 5, The updated value of y is:", y) y **= 2 print("y **= 2, The updated value of y is:", y)
Output:
value of x is: 10 x += 5, The updated value of x is: 15 x -= 5, The updated value of x is: 10 x *= 5, The updated value of x is: 50 x /= 5, The updated value of x is: 10.0 x %= 3, The updated value of x is: 1.0 value of y is: 10 y //= 5, The updated value of y is: 2 y **= 2, The updated value of y is: 4
These operators are used to perform logically AND, logical OR and logical NOT operations.
"and": This operator returns True if both operands are True. "or": This operator returns True if either of the operands is True. "not": This operator returns true if operand is False value or 0 value (reverses the result).
Example:
# Assign the operand values x = True y = False # Logical And print("{} AND {}:".format(x, y), x and y) # Logical or print("{} OR {}:".format(x, y), x or y) # Logical Not print("NOT {}:".format(x), not x)
Output:
True AND False: False True OR False: True NOT True: False
These operators are used to compare two values.
"==": This operator returns True if both the operands are equal. "!=": This operator returns True if both the operands are not equal. ">": This operator returns True if the first operand is greater than second. "<": This operator returns True if the first operand is less than second. ">=": This operator returns True if the first operand is either greater than or equal to second. "<=": This operator returns True if the first operand is either less than or equal to second.
Example:
# Assign the operand values x = 13 y = 6 # Equal to print("{} Equal to {}:".format(x,y), x == y) # Not equal to print("{} Not equal to {}:".format(x,y), x != y) # Greater than print("{} Greater than {}:".format(x,y), x > y) # Less than print("{} Less than {}:".format(x,y), x < y) # Greater than or equal to print("{} Greater than or equal to {}".format(x,y), x >= y) # Less than or equal to print("{} Less than or equal to {}".format(x,y), x <= y)
Output:
13 Equal to 6: False 13 Not equal to 6: True 13 Greater than 6: True 13 Less than 6: False 13 Greater than or equal to 6 True 13Less than or equal to 6 False
These operators are used to check if two objects are the same or not. It does not check if they are equal but it checks if they are at the same memory locations.
"is": This operator returns Returns true if both objects are same x is y "is not": This operator returns Returns true if both objects are not same x is not y
Note: In Python, if immuatble variables have assigning same values then they stored at a same memory location but that is not true in the case of mutable variables.
Example:
# In case of immutable data types a = 10 b = 10 print("a is b:", a is b) print("a is not b:", a is not b) c = "python" d = "python" print("\nc is d:", c is d) print("c is not d:", c is not d) # In case of mutable data types x = [10, 20, 30] y = [10, 20, 30] print("\nx is y:", x is y) print("x is not y:", x is not y)
Output:
a is b: True a is not b: False c is d: True c is not d: False x is y: False x is not y: True
These operators are used to check if a variable is in the sequence.
"in": This operator returns True if the value is found in the sequence. "not in": This operator returns True if the value is not found in the sequence.
Example:
# assigning a list x = [10, 20, 30, 40, 50] print("Whether 10 present in x or not:", 10 in x) print("Whether 10 not present in x or not:", 10 not in x) # assigning a string y = "Python" print("\nWhether 'p' present in y or not:", 'p' in y) print("Whether 'p' not present in y or not:", 'p' not in y)
Output:
Whether 10 present in x or not: True Whether 10 not present in x or not: False Whether 'p' present in y or not: False Whether 'p' not present in y or not: True
These operators are used to compare binary values.
"&": Bitwise AND operation is performed. "|": Bitwise OR operation is performed. "^": Bitwise XOR operation is performed. "~": Bitwise NOT operation is performed. ">>": Bitwise Right Shift operation is performed. "<<": Bitwise Left Shift operation is performed.
Example:
# Assign the operand values x = 7 y = 4 # bitwise AND print("{} & {}:".format(x,y), x & y) # bitwise OR print("{} | {}:".format(x,y), x | y) # bitwise NOT print("{} ~ {}:".format(x,y), ~x) # bitwise XOR print("{} ^ {}:".format(x,y), x ^ y) # bitwise right shift print("{} >> {}:".format(x,y), x >> 2) # bitwise left shift print("{} << {}:".format(x,y), x << 2)
Output:
7 & 4: 4 7 | 4: 7 7 ~ 4: -8 7 ^ 4: 3 7 >> 4: 1 7 << 4: 28