Operators are used to manipulate data by performing a certain functionality on variables to get the results according to the user’s requirement. From the simplest programs to programs that require complex functionalities, operators are everywhere. Operators play a huge role in the right functioning of a program and enhancing its performance. This article discusses the operators **, ^, %, and // in detail with relevant examples.
What do these operators mean (**, ^, %, //)?
Arithmetic operators have the same precedence as in mathematics. The first operation to be performed is exponentiation then simple multiplication and division and the precedence of modulus and floor division comes after.
Let us discuss the functionality of the operators **,^,%, and // in detail.
** Operator
** operator performs the exponentiation of two numbers. It is a type of arithmetic operator. The second parameter acts as an exponent. Exponentiation involves two parameters. One is base and the other is power. The number which is the base is raised to a power that multiples the number the exponent times. The following visual representation shows how exponentiation can be done:
If the exponentiation needs to be done between two variables in Python, the first variable is the base and the second variable will be the exponent. The following code explains the usage of the ** operator in Python:
def exp(a,b):
return print(f"The result of exponentiation where the base is {a} and the exponent {b} is:", a**b)
exp(2,3)
exp(4,2)
exp(3,4)
In the above code,
- The function exp is defined that takes the parameters a and b.
- The function prints the result of the exponentiation of two numbers where the first number a is the base and the second number b is its exponent.
- The function is checked on different input values.
Output
The following output displays the exponentiation results of different inputs using the ** operator in Python:
^ Operator
^ operator in Python represents the XOR which can also be called the bitwise operator. In binary data, the XOR operators return 0 for similar values and if the values are distinct, the operator returns 1. XOR is used in hashing algorithms, to check errors by producing the parity bits and more. The following diagram represents the XOR operation performed on binary equivalents of 10 and 14:
The following code performs the XOR operation on two variables. The XOR operation is done on the binary equivalent of 10 which is 1010 and the binary equivalent of 14 which is 1110 to give the value 0100. The following code explains how the XOR operator can be applied between two variables in Python:
def XOR(a,b):
return print(f"The result of bitwise XOR operation of binary values {bin(a)} and {bin(b)} is:", bin(a^b))
XOR(10,14)
XOR(18,21)
In the above code,
- The function XOR is defined as performs bitwise XOR operation and prints the result.
- The XOR function takes the parameters a and b which will be the decimal inputs. To represent them in binary format bin() method is called on both variables.
- The result is converted to the binary format using bin()
Output
The following output displays the results of the XOR operation on different inputs using the ^ operator in Python:
% Operator
The % operator is called the modulo operator in Python which performs the modulus division on two variables. Modulus division means that the function returns the modulus which is also called the remainder after performing the division. Performing the % operator on two variables means that the first variable will be taken as a dividend and the second as a divisor. The following visual representation shows the result yielded by the modulus division of 23 with 5:
In the above diagram, we saw that 3 is the remainder after the division has been done. Now, we will illustrate it with the help of a Python code that takes 23 and 5 as the parameter values and performs the modulus division on them to get the modulus of 3. The following code shows the working of the % operator:
def mod(a,b):
return print(f"The result of modulus operation of values {a} and {b} is:" ,a%b)
mod(23,5)
mod(45,8)
In the above code,
- The mod function is defined that takes the two parameters a and b and performs the modulus division on them. The function prints the result of the modulus division.
- The function mod is called on different inputs.
Output
The following output displays the modulus operation being performed on different inputs using the % operator in Python:
// Operator
// operator performs the floor division between two variables. The division returns the result that is closest to the approximated whole number. The result is either equal to the result retrieved from the normal division method or is less than it. The quotient can be referred to as the result of the floor division. In the example, we have performed division on the numbers 455 and 8, and the quotient of the resulting division will be the result of the floor division between two variables. The following diagram shows the result of the floor division between 455 and 8:
// operator does the floor division on two variables. The first variable will be the dividend and the second variable is the divisor. The function returns the quotient which is the result of the floor division of two numbers. The code explains how to perform the floor division on two numbers in Python:
def floor_div(a,b):
return print(f"The result of floor division of values {a} and {b} is:" ,a//b)
floor_div(25,6)
floor_div(455,8)
In the above code,
- The floor_div function takes two parameters a and b performs floor division on them and prints the result.
- The function floor_div is called on different input values.
Output
The output displays the floor division performed on different inputs in Python using the// operator:
To sum up, here is a table that contains the information about each of the operators discussed above:
Operator | Function |
---|---|
** | Used for exponentiation |
^ | Performs the bitwise XOR operation on two binary numbers |
% | Returns the remainder after the division |
// | Performs floor division by returning the rounds and the result to the closest whole number |
Conclusion
The arithmetic operators **,^,%, and // performs exponentiation, XOR(bitwise operation), modulus, and division respectively in Python. Operators are an integral part of programming and are crucial for the operation of a program. No matter what the scope of a program is, it greatly relies on the right usage of the operators for its efficient working. The article discusses the usage of the operators **, ^, %, // and explains each operator and its working with an example.