Discover the intricacies of division in Python, including regular, floor, and modulus types, with Art, a Python instructor. Learn how these types of division can be applied in real-world situations to solve common problems.
Key Insights
- Python supports three types of division: regular, floor, and modulus.
- Regular division returns the exact quotient of a division operation.
- Floor division, often used when a whole number result is needed, returns how many times a number can fit into another, rounding down the result.
- Modulus division, or using a percentage sign, returns the remainder of a division operation.
- Python's divmod function returns a tuple with the results of floor division and modulus division.
- The instructor demonstrates these division types using practical examples, showing how to apply them in different scenarios.
In this video, we're going to look at how to use Division in Python
Video Transcription
Hi, My Name is Art, and I Teach Python at Noble Desktop. in This Video, I'm Going to Show You Division. by Now, You Probably Know That Math is Simple in Python. Two Plus Two Will Get You Four, Two Minus Two Will Get You Zero, and Two Times Two Will Get You Four. However, It's Slightly Different with Division.
There are three division types in Python. The first is regular division. Five divided by three will give you a result. The second type of division is floor division. Floor division is how many times three can fit into five. So, in this case, it's only once. Floor division is useful when you need to get a whole number, as it rounds down. An example of this is if you have seven people and you want to split them into two teams. Seven divided by two using floor division will give you a whole number.
The opposite of floor division is modulus. Modulus (or percentage sign) will give you the remainder. For example, if you feed three into five, you will get a remainder of two. Modulus is useful when you need to get the remainder.
Python also has the function divmod which returns two values in a tuple. The first value is the product of floor division, and the second value is the product of modulus division. You can unpack the tuple and assign the values to variable names.
In my next video, I'm going to show you how to do a very popular problem called checking if a number is even or odd. Please watch it. Thank you.