Python isn’t just the name of a super-strong snake, it’s also the name of the super semantic programming language.
Python is used to Create websites from software, automate tasks and conduct analysis.
It’s one of the most versatile computer languages we have so far comma and is considered an all-purpose full stop this means it’s used to create a range of different programs.
As such python has its own rules, codes, semantics, and language. // in Python is an operator, but what exactly does that mean?
Operators In Python
In Python, an operator is exactly what it sounds like, it is a symbol that can carry out a computation that is either arithmetic or logical. whatever the operator operates on is known as the operand.
For example, if you have something like 5 + 6, + is the operator. 5 and 6 are the operands and 11 becomes the output.
There are different kinds of operators in Python, one of the most used types is arithmetic operators. Here are some of the different types of arithmetic operators and their meanings.
|+||Addition of 2 operands or acts as a unary plus.|
|–||This will minus the right operand from the left operand, or act as a unary minus.|
|/||This will divide the left operand by the right operand|
|*||Multiples two operands together|
|//||This is for floor division. Floor division results in whole numbers that are adjusted left in a number line.|
|%||This represents modulus, which is the remainder of the division operands|
|**||This represents exponent, which is the left operand raised to the power of the right operand.|
Other Types Of Operators
Operators are key in python. They act as the instructions for computation and direct the computation correctly.
Different computations need to take place and therefore different types of operators. Here are the different types of operators in Python and what they are used for.
Comparison operators essentially compare values. They will return either a TRUE or FALSE statement based on the condition.
For example, if you have 9 > 6, > (more than symbol) is the comparison operator and it will return a TRUE statement because 9 is bigger than 6.
There are 3 different types of logical operators. They aren’t symbols, but words.
And– used if both of the operands are true
Or– Used if either one of the operands is true
Not– Used if the operands are false.
Bitwise operators act like binary digits. Binary digits are made up of 1s and 0s. Binary is the universal computer language.
A bitwise operator compares each part of the first operand to the corresponding parts of the second operand.
Assignment operators assign values to different variable. For example if you have the variable B and you assign in the value of 7, the assignment operator is B=7.
Special operators are used to increase or decrease values of a variable by one. There are two types of special operators. Identity operators, and membership operators.
There are two identity operators in python. They are IS and IS NOT. IS is used for operands that are identical, IS NOT is used for non-identical operands.
Membership operators test whether or not a variable or value is found in a sequence. The membership operators are IN and NOT IN.
IN is true when a variable is located in a sequence. NOT IN is true when a variable is not found in a sequence.
// is an arithmetic operator. It represents a floor division, which is also known as integer division. Floor division is basically a lot like normal division, only it will return the largest possible number.
It’s either less or equal to the answer that you’d get with normal division. Essentially, floor division will return the whole number in division.
So for example is you were to do 4//3, it would return the value 1, whereas normal division would give you 1.33333*. The whole point of floor division is to share out the quantities evenly.
Think about it like this, if you were to divide 8 brownies, between 3 people, how many whole brownies would a person get? You would use floor division to figure it out.
Each person would receive 2 whole brownies. Using standard division, 8÷3= 2.666666666667. And everyone knows that .666666666667 of a brownie just isn’t as tasty as a whole brownie.
// and Modulo
You may be wondering, but what happens to the .666666666667? You can just ignore it and pretend it doesn’t exist.
That’s where modulo (%) comes in. Modulo refers to the remainder of two numbers that have been divided.
We know that 8//3 will give you 2 brownies per person. If you were to reverse the equation, and multiply the number of brownies (2) by the number of people (3) you would get 6. 6 is the number of brownies eaten, which means (8-6) there are 2 brownies remaining and uneaten.
This is one of calculating the remainders, modulo is another way.
If you were to compute
It would return
Showing the remainder.
Floor Division (//) and Modulo (%) are related in python via this equation:
a=b * (a//b) + (a%b)
A represents the dividend
B represents the divisor
Floor Division And The Divmod() Function
Divmod() is a function in python that caulculates both modulo and floor division.
It uses the equation r= divmod (a,b).
Substituting using the brownie example we have;
Nbrownies, Nleftovers = divmod (8,3)
// In Python Final Thoughts…
So the symbol // represents floor division, and is called an operator. It’s an arithmetic operator and is used to find the whole number answer in division.
This is because it aims to spread/ divide the integers evenly.
Using the the // function will return the floor division value, and modulo will return the exact remainders. Using the divmod() function will return both.