Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does these operators mean (** , ^ , %, //) ?
In Python, there are various types of operators used to perform specific functions, such as (**), (^), (%), and (//). The (**) operator represents exponentiation, (^) represents bitwise XOR, (%) represents the modulus operation, and (//) represents floor division. In this article, we will understand the workings of these operators.
Exponentiation Operator (**)
The exponentiation operator (**) is used to raise a number to a power. This operator works the same as the Python pow() method. In exponentiation, you need two numbers: the first is the base (the number you want to raise), and the second is the power (how many times to multiply the base by itself).
Example of Exponentiation Operator
In the example below, we calculate 2 raised to the power of 5, which results in 32 ?
# Calculate 2 raised to the power of 5
result = 2 ** 5
print("2 to the power of 5 is:", result)
# More examples
print("3 ** 4 =", 3 ** 4)
print("5 ** 2 =", 5 ** 2)
Following is the output of the above program ?
2 to the power of 5 is: 32 3 ** 4 = 81 5 ** 2 = 25
Bitwise XOR Operator (^)
The (^) represents the bitwise XOR operator in Python. The term XOR stands for exclusive OR. It compares each bit of two numbers and returns 1 only if the bits are different, otherwise returns 0.
Example of Bitwise XOR Operator
In the example below, the (^) operator performs a bitwise XOR, which compares each bit of a (60) and b (13) and returns 1 only if the bits are different ?
a = 60 # Binary: 111100
b = 13 # Binary: 001101
result = a ^ b
print("a:", a, "b:", b, "a^b:", result)
# More examples
print("8 ^ 3 =", 8 ^ 3)
print("15 ^ 7 =", 15 ^ 7)
Following is the output of the above program ?
a: 60 b: 13 a^b: 49 8 ^ 3 = 11 15 ^ 7 = 8
Modulo Operator (%)
The modulo operator is denoted by the "%" symbol in Python. It returns the remainder of the division of two numeric operands. This is particularly useful for checking if a number is even or odd, or for cycling through values.
Example of Modulo Operator
In the example below, the % operator gives the remainder after dividing the first number by the second ?
# Examples of modulo with positive integers
print("10 % 2 =", 10 % 2) # Even number check
print("34 % 5 =", 34 % 5)
print("43 % 6 =", 43 % 6)
# Checking if a number is even or odd
number = 17
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
Following is the output of the above program ?
10 % 2 = 0 34 % 5 = 4 43 % 6 = 1 17 is odd
Floor Division Operator (//)
The (//) symbol represents the floor division operator in Python. This operator divides two numbers and returns the largest whole number that is less than or equal to the result, effectively removing the decimal part.
Example of Floor Division Operator
In the example below, the (//) operator divides numbers and returns only the whole number part ?
# Basic floor division
a = 10
b = 4
result = a // b
print("10 // 4 =", result)
# More examples
print("17 // 3 =", 17 // 3)
print("22 // 7 =", 22 // 7)
# With negative numbers
print("-10 // 3 =", -10 // 3)
print("10 // -3 =", 10 // -3)
Following is the output of the above program ?
10 // 4 = 2 17 // 3 = 5 22 // 7 = 3 -10 // 3 = -4 10 // -3 = -4
Comparison Table
| Operator | Name | Function | Example | Result |
|---|---|---|---|---|
| ** | Exponentiation | Raises base to power | 2 ** 3 | 8 |
| ^ | Bitwise XOR | Exclusive OR operation | 5 ^ 3 | 6 |
| % | Modulo | Returns remainder | 7 % 3 | 1 |
| // | Floor Division | Returns whole quotient | 7 // 3 | 2 |
Conclusion
These four operators serve distinct purposes: ** for exponentiation, ^ for bitwise XOR operations, % for finding remainders, and // for floor division. Understanding these operators is essential for effective Python programming and mathematical operations.
