In Python, operators are special symbols or keywords used to perform operations on operands.
Python supports various types of operators, including arithmetic operators, comparison operators, logical operators, assignment operators, bitwise operators, and more.
Operators performs some tasks, In other programming languages as well.
Here's an overview of the different types of operators in Python:
Arithmetic operators are used to perform mathematical operations on numeric operands.
+ Addition: The `+` operator is used to add two values together.
- Subtraction: The `-` operator is used to subtract one value from another.
* Multiplication: The `*` operator is used to multiply two values.
/ Division: The `/` operator divides one value by another.
// (Floor Division): It returns the integer part of the quotient.
% Modulus (Remainder): The `%` operator returns the remainder of the division of one value by another.
** Exponentiation: The `**` operator is used to raise the first operand to the power of the second operand.
quantity1 = 60 quantity2 = 20 # Addition sum = quantity1 + quantity2 print(sum) # Output: 80 # Subtraction diff = quantity1 - quantity2 print(diff) # Output: 40 # Subtract and Assign quantity1 -= quantity2 # same as quantity1 = quantity1 - quantity2 print(quantity1) # Output: 40 # Divide divideValue = quantity1 / quantity2 print(divideValue) # Output: 2.0 # Multiple multipleValue = quantity1 * quantity2 print(multipleValue) # Output: 800 # Modules modulesValue = quantity1 % quantity2 print(modulesValue) # Output: 0 # Exponentiation exponentiation = 4 ** 2 print(exponentiation) # Output: 16 # Floor Division: floorDivision = quantity1 // quantity2 print(floorDivision) # Output: 2
Comparison operators are used to compare values and return a Boolean result (True or False).
Equal to: ==
Not equal to: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
quantity1 = 40 # Initialize the quantity1 value quantity2 = 20 # Initialize the quantity2 value # Equal to print(quantity1 == quantity2) # Output: False # Not Equal to print(quantity1 != quantity2) # Output: True # Greater Than > print(quantity1 > quantity2) # Output: True # Less Than < print(quantity1 < quantity2) # Output: False # Greater than or equal to print(quantity1 >= quantity2) # Output: True # Less than and equal to print(quantity1 <= quantity2) # Output: False
Logical operators are used to combine conditional statements and return a Boolean result.
& (Logical AND): The logical AND operator `&` returns `TRUE` if both of its operands are `TRUE`, otherwise, it returns `FALSE`.
| (Logical OR): The logical OR operator `|` returns `TRUE` if at least one of its operands is `TRUE`, otherwise, it returns `False`.
isStudent = True isAdult = False # And Condition print(isStudent & isAdult) # Output: False # OR Condition print(isStudent | isAdult) # Output: True
Assignment operators are used to assign values to variables.
= (Assignment): The simple assignment operator `=` assigns the value on the right-hand side to the variable on the left-hand side.
+= (Add and Assign): The addition assignment operator `+=` adds the value on the right-hand side to the variable on the left-hand side.
-= (Subtract and Assign):The subtraction assignment operator `-=` subtracts the value on the right-hand side to the variable on the left-hand side.
*= (Multiply and Assign): The multiplication assignment operator `*=` multiples the value on the right-hand side to the variable on the left-hand side.
/= (Divide and Assign): The division assignment operator `/=` divides the value on the right-hand side to the variable on the left-hand side.
%= (Modulus and Assign): The modulus assignment operator `%=` performs the modulus operation on the variable on the left-hand side with the value on the right-hand side.
** (Exponentiation and Assign): The Exponentiation assignment operator `**` performs the Exponentiation operation on the variable on the left-hand side with the value on the right-hand side and raises the first operand to the power of the second operand.
Bitwise operators are used to perform bitwise operations on integers.
Bitwise AND: &
Bitwise OR: |
Bitwise XOR: ^
Bitwise NOT: ~
Left Shift: <<
Right Shift: >>
isStudent = True isAdult = False # And Condition print(isStudent & isAdult) # Output: False # OR Condition print(isStudent | isAdult) # Output: True # XOR Condition print(isStudent ^ isAdult) # Output: True # NOT print(~20) # Output: -21 print(~-20) # Output: 19 # Left Shift >> print(20>>10) # Output: 0 # Right Shift << print(10<<20) # Output: 10485760
Membership operators are used to test if a value is present in a sequence.
in: it returns True if a value is present in a sequence.
not in: it returns True if a value is not present in a sequence.
Identity operators are used to compare the memory addresses of two objects.
is: it returns True if both operands refer to the same object.
is not: it returns True if both operands refer to different objects.