Operators in Python

Operators are symbols that perform operations on variables and values. They are fundamental to any programming language, including Python. In this topic, we'll explore various types of operators in Python, from basic arithmetic operators to more advanced ones like bitwise and logical operators.

Arithmetic Operators

Addition Operator (+) Adds two operands.

				
					# Example of addition operator
result = 10 + 5
print(result)  # Output: 15
				
			

Subtraction Operator (-) Subtracts the second operand from the first.

				
					# Example of subtraction operator
result = 10 - 5
print(result)  # Output: 5
				
			

1.3 Multiplication Operator (*) Multiplies two operands.

				
					# Example of multiplication operator
result = 10 * 5
print(result)  # Output: 50
				
			

1.4 Division Operator (/) Divides the first operand by the second.

				
					# Example of division operator
result = 10 / 5
print(result)  # Output: 2.0 (result is a float)
				
			

1.5 Modulus Operator (%) Returns the remainder of the division of the first operand by the second.

				
					# Example of modulus operator
result = 10 % 3
print(result)  # Output: 1
				
			

1.6 Exponentiation Operator ()** Raises the first operand to the power of the second.

				
					# Example of exponentiation operator
result = 2 ** 3
print(result)  # Output: 8
				
			

Comparison Operators

Equal to (==) Checks if the values of two operands are equal.

				
					# Example of equal to operator
result = (10 == 5)
print(result)  # Output: False
				
			

Not equal to (!=) Checks if the values of two operands are not equal.

				
					# Example of not equal to operator
result = (10 != 5)
print(result)  # Output: True
				
			

Greater than (>) Checks if the first operand is greater than the second.

				
					# Example of greater than operator
result = (10 > 5)
print(result)  # Output: True
				
			

Less than (<) Checks if the first operand is less than the second.

				
					# Example of less than operator
result = (10 < 5)
print(result)  # Output: False
				
			

Greater than or equal to (>=) Checks if the first operand is greater than or equal to the second.

				
					# Example of greater than or equal to operator
result = (10 >= 5)
print(result)  # Output: True
				
			

Less than or equal to (<=) Checks if the first operand is less than or equal to the second.

				
					# Example of less than or equal to operator
result = (10 <= 5)
print(result)  # Output: False

				
			

Logical Operators

Logical AND (and)

Returns True if both operands are True.

				
					# Example of logical AND operator
result = True and False
print(result)  # Output: False
				
			

Logical OR (or)

Returns True if either of the operands is True.

				
					# Example of logical OR operator
result = True or False
print(result)  # Output: True
				
			

Logical NOT (not)

Returns True if the operand is False and vice versa.

				
					# Example of logical NOT operator
result = not True
print(result)  # Output: False
				
			

Bitwise Operators

Bitwise AND (&)

Performs bitwise AND operation on two operands.

				
					# Example of bitwise AND operator
result = 10 & 5
print(result)  # Output: 0

				
			

Bitwise OR (|)

Performs bitwise OR operation on two operands.

				
					# Example of bitwise OR operator
result = 10 | 5
print(result)  # Output: 15
				
			

Bitwise XOR (^)

Performs bitwise XOR operation on two operands.

				
					# Example of bitwise XOR operator
result = 10 ^ 5
print(result)  # Output: 15
				
			

Bitwise NOT (~)

Performs bitwise NOT operation on a single operand.

				
					# Example of bitwise NOT operator
result = ~10
print(result)  # Output: -11
				
			

Assignment Operators

Assignment (=)

Assigns a value to a variable.

				
					5.1 Assignment (=)
Assigns a value to a variable.
				
			

Addition Assignment (+=)

Adds the right operand to the left operand and assigns the result to the left operand.

				
					# Example of addition assignment operator
x += 5  # Equivalent to x = x + 5
				
			

Subtraction Assignment (-=)

Subtracts the right operand from the left operand and assigns the result to the left operand.

				
					# Example of subtraction assignment operator
x -= 5  # Equivalent to x = x - 5
				
			

Multiplication Assignment (*=)

Multiplies the right operand with the left operand and assigns the result to the left operand.

				
					# Example of multiplication assignment operator
x *= 5  # Equivalent to x = x * 5
				
			

Division Assignment (/=)

Divides the left operand by the right operand and assigns the result to the left operand.

				
					# Example of division assignment operator
x /= 5  # Equivalent to x = x / 5
				
			

Operators are indispensable tools in Python programming, allowing you to perform various operations on data efficiently. By mastering different types of operators, including arithmetic, comparison, logical, bitwise, and assignment operators, you gain the ability to manipulate data and control the flow of your program effectively. Remember to choose the appropriate operator for each task and practice using them in different scenarios to enhance your programming skills. Happy Coding!❤️

Table of Contents