[Zero basic learning python] coorse division, python
Division is not just python.
Integer divided by integer
Exercise the following operations after starting idle:
>>> 2/50>>> 2.0/50.4>>> 2/5.00.4>>> 2.0/5.00.4
No? The problem arises. If the division of primary school mathematics knowledge is involved, the calculation results of the above four operations should be 0.4. However, the first three matching results are 0. Why?
Because there is a rule in python, such as division in 2/5, to be rounded up. 2 divided by 5, the quotient is 0 (integer), and the remainder is 5 (integer ). If this form is used: 2/5, the calculation result is the quotient integer. It can also be understood:
An integer is divided by an integer. The result is an integer (quotient).
Continue the experiment and verify the conclusion:
>>> 5/22>>> 6/32>>> 5/22>>> 6/23>>> 7/23>>> 8/24>>> 9/24
Note:: Here we get the integer quotient, instead of "Rounding" The result containing decimal places ". For example, 5/2, the result is quotient 2, remainder 1, and final 5/2 = 2. Not rounding 2.5.
Float points and Integers
Note that the title is in different format from the title above. The title above is "integer divided by integer". If the title is in a consistent style, the title of this section should be "floating point number divided by integer", but no. Now it is "floating point number and integer division", because it contains the following three situations:
- The divisor is a floating-point number and the divisor is an integer.
- The divisor is an integer and the divisor is a floating point number.
- The divisor and divisor are both floating-point numbers.
Before coming to the conclusion, we should do the experiment first:
>>> 9.0/24.5>>> 9/2.04.5>>> 9.0/2.04.5>>> 8.0/24.0>>> 8/2.04.0>>> 8.0/2.04.0
Induction to obtain the rule:
Whether it is divisor or divisor, as long as one number is a floating point, the result is a floating point.. Therefore, if there is a remainder in the result of division, it will not be the same as before, but return a floating point number, which is the same as the result of learning mathematics.
>>> 10.0/33.3333333333333335
This is a bit strange. According to the mathematical knowledge, it should be 33333..., followed by a 3 loop. So your computer won't be able to stop. The full screen is 3. To avoid this, python ended the loop arbitrarily, but sadly it was not terminated in accordance with the principle of rounding.
I have learned about the infinite loop Decimals in elementary school, but this is not a simple question. Let's take a look at Wikipedia's entry: 0. 999... will you have any in-depth experience?
In short, to use python, you have to follow her rules. The first two rules have been clarified.
Add a reference for interested friends to read: Floating Point Number algorithms: disputes and restrictions. (For reasons you know, you cannot provide a link. Please go to my github to view the full version)
Note: The above division rules are for python2. In python3, 5/2 and 5.0/2 are equivalent. However, if you want to get the integer, you can use another method: Floor division.
>>> 9/24>>> 9//24
Python always provides a variety of solutions to the problem, which is her style.
Getting started with wheels
A very important reason why python is popular is that there are many wheels. This is a metaphor. What should I do if you want to run fast? It is impossible to practice running every day and use wheels. Finding a bicycle is much faster. It is not fast enough. You can change the battery car, the car, and the high-speed train... you can choose a lot. However, most of the things that let you run fast are not made by yourself. You can use them instead. Even two legs are a gift to my parents. It is precisely because of the large number of wheels that you can choose and enjoy at different speeds.
Python is like this. We only need to use it if there are various wheels created by others. But those wheels in python are not called bicycles, cars, or modules. Some people take the names of other languages, such as class libraries and classes ". No matter what the name is. It is something that is made by others. We can use it here.
How to use it? You can use the following two methods:
- Format 1: import module-name. Import is followed by a space, and then the module name, for example, import OS
- Form 2: from module1 import module11. Module1 is a large module with a sub-module module11 in it. I just want to use module11, so I wrote it. For example:
No worries. Experiment 1:
>>> from __future__ import division>>> 5/22.5>>> 9/24.5>>> 9.0/24.5>>> 9/2.04.5
Note: After a module is referenced and division is performed, the floating point number is obtained in no matter what the situation is.
This is the power of the wheel.
About Remainder
In the previous calculation of 5/2, the quotient is 2, and the remainder is 1.
How can I get the remainder?
Perform the following operations:
>>> 5%21>>> 9%21>>> 7%31>>> 6%42>>> 5.0%21.0
Symbol: %, which is the remainder of the division to get two numbers (which can be integers or floating-point numbers.
As mentioned above, python has many interesting wheels (modules), and many built-in functions will help us do a lot of things. For example, the divmod () function ()
>>> Divmod (5, 2) # indicates dividing 5 by 2, returns the quotient (2, 1) >>> divmod (9, 2) (4, 1) >>> divmod (5.0, 2) (2.0, 1.0)
Rounding
The last one must be insisted. Today it is indeed a bit cool. To implement rounding, it is very easy, that is, the built-in function: round ()
Try it:
>>> round(1.234567,2)1.23>>> round(1.234567,3)1.235>>> round(10.0/3,4)3.3333
Simple. The simpler the process is, the more careful you should be. When you encounter the following situations, you may be a bit skeptical:
>>> Round (1.2345, 3) 1.234 # It should be: 1.235 >>> round (2.235, 2) 2.23 # It should be: 2.24
Haha, I found a python bug, so excited.
Don't be so excited. If it's a bug, it's so obvious that it's not my turn. Why? For more information, see here. The following is an excerpt from the official document:
Note: The behavior of round () for floats can be surprising: for example, round (2.675, 2) gives 2.67 instead of the expected 2.68. this is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a float. see Floating Point Arithmetic: Issues and Limitations for more information.
It turns out that it really cannot reach me. (Dejected .)
It seems that the Division problem is coming to an end, but it is far from enough. As a beginner, this is now. There are still many topics left behind, such as how to deal with the problem of circular decimal places. I certainly won't disappoint friends with the spirit of exploration. There is such a wheel in my github. If you want to study it in depth, you can try it here. (The hyperlink does not exist. For the same reason, please go to my github: qiwsir's ITArticles for BasicPython)
Statement
This article is a castrated version. To view the full version, go to BasicPython in my github: qiwsir's ITArticles.
There is no basic knowledge of python. There are indeed no good training institutions in Beijing. I will consider the most authoritative
Yes. However, you can read books by yourself. As for training institutions, I have a buddy who is studying in zhonggu.
I am not advertising. Is it true. My buddy said yes. However, there seems to be few python training institutions.
If there is no comparison, the authority is not authoritative.
What should I do if I want to learn programming without basic knowledge?
Visual Basic (VB) is a Visual, object-oriented, and event-driven structured high-level programming language developed by Microsoft in 1991, it can be used to develop various applications in Windows. It is easy to learn and efficient, and has powerful functions comparable to the Windows Professional Development Tool SDK.
Easy to get started