A very important lesson in Python to learn from the beginning is “Division in Python”. What it means to divide in Python, and the different uses of Python arguments or operators. Many people do not learn these subtle differences. When they do not learn, it costs them hours and days of debugging programs.
One might ask: how you divide in Python?
This is done in a couple of different ways. There are two kinds of division; there is integer division and a float division.
- Integer division takes two numbers and divides them to give a result of a whole number.
- Float division takes two numbers and divides them and results in a decimal value.
Many people get confused about the acceptable use of operators when dividing two numbers. Especially at times when people need to use division in their code to give an integer value when they truly need a float value between zero and one to perform their program needs and parameters. For example, JAVA uses integer division which will return a value that skips the remainder, (i.e. no decimal values), so it will always return a whole number. This cannot be used when a Float number between zero and one is needed. So, when a decimal value is needed, we can turn to the use of the float operator in Python.
Python division depends on the operators that are used. The use of regular division uses the single front-slash
/ operator. This operator will result in a decimal value. Floor division uses the double front-slash
// operator. This operator will result in a whole number, or integer value being given. Then we can go a step further and use the Modulo Operator (percent symbol)
% which gives you a remainder value or a zero.
Floor Division //
An example of floor division is noted in the Python code listed below:
>>> x = 60 // 12 >>> print(x) 5
So we see that when we need a whole number generated in our code to accomplish particular tasks we will use this floor division operator
Integer Division /
A person also needs to be careful to use the most updated type of Python available. For example, if someone uses Python 2.0, integer divisions will return an integer value instead of a float value needed. Also when we perform division in Python we want to be careful what value we divide by. We find that if we divide by a whole number, it will round to an integer.
>>> 10 / 90 0
However, when we divide by a float (or decimal) we find we get a float value as a result.
>>> 10 / 90.0 0.111111
One can also use arguments in Python to give them the answers they are looking for. These arguments are “Float” and “Int” parameters. So we find when we want to get a result with a decimal value we can write our code as noted below
>>> float(10/90) 0.11111 >>> int(10/90) 0
Modulo Operator %
The Modulo Operator ” % ” is a special division operator that calculates the remainder of an integer division.
Example: It can be used to find out leap years. We can set up a calendar program. A leap year happens every fourth year. It has 29 days in February instead of 28 days and has 366 days in the year. It occurs in 1996, 2000, 2004, 2008… 2020 and so on every 4 years. So we can find out if a year is a leap year by simply dividing it by four. If the answer for the remainder is zero, it is a leap year.
The Modulo operator is referred to as the remainder from the division of the first argument to the second argument. Working with this operator we find that the numbers are first converted into their common type. A zero division error is caused if the right argument is zero. These arguments can be floating-point numbers. We find that the absolute value for the result is smaller than the second operand. And the result of using the Modulo operator will always yield the same sign as the second operand or zero. To see the results of using the Modulo operators you can follow the code written below.
year_input = int(input("enter a year: ")) if year_input % 4 == 0: print("Remainder is zero!", year_input, "is a leap year!") else: print(year_input, "is not a leap year!")
IF we enter 2004 in the code above we will get a response that says the remainder is zero. So 2004 will be a leap year. However, if we were to put 2021 into the equation, the answer to the input would be “2021 is not a leap year!!!.”
NumPy Division Further Reading
One degree of difficulty further with the use of division in Python is the use of Numpy. Numpy is a general-purpose tool that allows for the processing of many numbers arranged in arrays. So when you have multiple division processes going on the process can be accelerated to a great degree. Ultimately, you will be able to speed up your workflow and work with and interface with multiple other applications in the Python stratosphere. Numpy is not only used to evaluate numbers but also strings, words, and other phrases. Ultimately it allows you to analyze many sets of data quickly and efficiently. This allows you to study big data, machine learning, and read large data files. Also, it can allow you to do multiple arithmetic functions on these arrays including Python division quickly and efficiently.
Related Article: The Ultimate Guide to NumPy
So to sum up, we have taken a journey on our look at the use of division in Python from the simplest formula with simple division by using the operator
/ to the more complex use of division with Python by using the operator
// to give an integer value. We could have also accomplished this with the two different arguments. We can use the
int() functions. We will use
float() to give us a decimal value and
int() to give us a whole number.
Then we moved on to the use of the Modulo Operator. This allowed us to use the remainders of the division process. These are incorporated into our code to perform specific functions. Then these functions were used to figure out when Leap Years will occur for our calendar program. And finally, we arrive at the culmination of the use of division in Python.
We implement Numpy to perform many computations fast. This allows us to use big data so multiple computations in the division of many numbers can be done quickly and efficiently.