Detailed explanation of the basic mathematical computing usage in Python programming, detailed explanation of python programming mathematics

Source: Internet
Author: User
Tags integer division

Detailed explanation of the basic mathematical computing usage in Python programming, detailed explanation of python programming mathematics

Quantity
In Python, the provisions on logarithm are relatively simple and can be understood basically at the level of elementary school mathematics.

So, as a zero-basic learning, we should start with the calculation of Primary School Mathematics questions. From here on, the basic knowledge of mathematics must pass through.

>>> 3
3
>>> 3333333333333333333333333333333333333333
3333333333333333333333333333333333333333L
>>> 3.222222
3.222222

In interactive mode, if 3 is input, 3 is displayed. Such a number is called an integer, which is the same as that in elementary school mathematics.

If you enter a relatively large number and the second one, then an integer consisting of multiple three is called a long integer in Python. To indicate that a number is a long integer, Python displays an L at the end of it. In fact, Python can now automatically regard a large integer as a long integer. You do not have to make any difference in this regard.

Third, it is called decimal in mathematics. You can still call it here, but like many programming languages, it is often called a "floating point number ". The origin of this name is also a bit mentioned. If you are interested, google.

In the preceding example, all values are unsigned (or non-negative). To represent a negative number, just like the Representation Method in mathematics, add a negative number.

It is worth noting that we are talking about the decimal number here.

In addition to decimal, binary, octal, and hexadecimal are all used in programming. Of course, there is less time to use the 60-digit notation (in fact, the time record method is a typical 60-digit Notation ).

Each number is an object in Python. For example, 3 entered above is an object. Each object has its own address in the memory. This is its identity.

>>> id(3)
140574872
>>> id(3.222222)
140612356
>>> id(3.0)
140612356
>>>

You can use the built-in function id () to view the memory address of each object, that is, the identity.

The built-in Function is a built-in Function in English. You can guess the number of built-in functions by name. Good, it is an internal function that has been defined in Python.
The preceding three numbers are three different objects with three different memory addresses. In mathematics, 3 and 3.0 are equal, but here they are different objects.

The memory address obtained by using id () is read-only and cannot be modified.

After learning about "Identity", let's look at "type". There is also a built-in function for using type ().

>>> type(3)
<type 'int'>
>>> type(3.0)
<type 'float'>
>>> type(3.222222)
<type 'float'>

You can use built-in functions to view object types. <Type 'int'>, indicating that 3 is an integer type (Interger); <type 'float'> indicates that the object is a Floating point real number ). Similar to the result of id (), the result of type () is read-only.

As for the object value, here it is the object itself.

It seems that the object is not hard to understand. Please be confident and continue.

Variable
Writing 3, 4, and 5 is far from enough. In programming languages, "variables" and "Numbers" are often used (strictly speaking, objects in Python) create a ing. For example:

>>> x = 5
>>> x
5
>>> x = 6
>>> x
6

In this example, x = 5 establishes a ing between the variable (x) and the number (5), and then establishes a ing between x and 6. We can see that x first "yes" 5, and then "yes" 6.

In Python, such a statement is very important: the object has a type and the variable has no type. How can this problem be solved?

First, values 5 and 6 are all integers. in Python, they are named "integer" type data, or the data type is an integer, which is represented by int.

When we write 5 and 6 in Python, the computer girl will automatically create these two objects for us somewhere in her memory (the definition of the object will be described later, here you will first use, gradually clarify the meaning). It is like building two sculptures. One is 5 and the other is 6. These two objects are of the int type.

What about x? It is like a tag. When x = 5, the tag x is tied to 5. The tag x is extended to 5, in the interactive mode, >>> the output result of x is 5. It seems that x is 5, and the fact is that x is attached to 5. In the same way, when x = 6, the tag is changed and pasted to 6.

Therefore, the tag x has no type. It can be attached not only to integer objects, but also to other types of objects, such as the str (string) type object.

This is a very important difference between Python and some languages.

Arithmetic Operation
Run in interactive mode according to the following requirements to check whether the obtained results are consistent with those obtained after the basic mathematics knowledge calculation.

>>> 2+5
7
>>> 5-2
3
>>> 10/2
5
>>> 5*2
10
>>> 10/5+1
3
>>> 2*3-4
2

The preceding operations involve four operators: Plus (+), minus (-), multiplication (*), and Division (/)

In addition, I believe that the audience has discovered an important principle:

In a computer, the four arithmetic operations are the same as those learned in elementary school mathematics.

If a person is a high-tech animal, he must inherit the knowledge he has already learned from his own history. The great scientists, when designing a computer, thought of the needs of the column space for learning now. They must not let the future generations learn new computing rules and use the primary school mathematics. I would like to thank those pioneers of scientists, who were later on.

The following three arithmetic questions are calculated to see what the result is.

4 + 2
4.0 + 2
4.0 + 2.0

An official may be angry. It is a waste of time to drive a computer for such a simple question.

Don't worry. You still need to calculate the result and check whether the result is different? Observe carefully.

>>> 4+2
6
>>> 4.0+2
6.0
>>> 4.0+2.0
6.0

The difference is that the first sub-result is 6, which is an integer; the last two are 6.0, which is a floating point.

Definition 1: a number in the form of 4,-2, 129486655,-988654, and 0. It is called an integer.
Definition 2: a number in the format of 4.0,-2.0, 2344.123, and 3.1415926. It is called a floating point number.
There is no need to memorize these two definitions. google it. Remember the sentence that Einstein said: I don't remember anything in the book? I don't remember it anyway ). He didn't say anything in the last half. I added: google is the only thing you forget.

It seems that the computer does some arithmetic operations, but you must note that in mathematics, integers can be infinitely large, but in computers, an integer cannot be infinitely large. Why? (I recommend that you go to google. Actually, you must have learned some basic computer knowledge .) Therefore, it may occur that the number involved in the operation or the calculation result exceeds the maximum number in the computer. This problem is called an integer overflow problem ".

Integer Overflow
Here is an article devoted to this issue. We recommend that you read this article: integer overflow.

For other languages, integer overflow must be taken into consideration. However, in Python, you will not be worried about it. The reason is that Python solves this problem for us. Please read the following article: multiply large Integers

OK! You can experiment with the IDE to multiply large integers.

>>> 123456789870987654321122343445567678890098876*1233455667789990099876543332387665443345566152278477193527562870044352587576277277562328362032444339019158937017801601677976183816L

It's lucky to look at Python, so it's time to learn Python.

The number calculated above has an L at the end, which indicates that this number is a long integer. However, you don't have to worry about this. It's done by Python.

Before the end of this section, there are two symbols that need to be remembered by the reader (You can google at any time, but remember to use it more conveniently)

Integer, represented by int, from the word: integer
Floating Point Number, represented by float, is the word: float
You can use a command: type (object) to check the type of a number.

> > > type (4)
<type 'int'=""> #4 is an int, an integer</type>
> > > type (5.0)
<type 'float'="">#5.0 is a float</type>
Type (988776544222112233445566778899887766554433221133344455566677788998776543222344556678).
<type 'long'=""> # is a long integer and also an integer</type>  

Division
Division: not only Python.

Integer divided by integer
After entering the Python interaction mode (later in this tutorial, you may not repeat this type of description, as long as you see >>>, it means that it is in the interaction mode), and practice the following operations:

>>> 2 / 5
0
>>> 2.0 / 5
0.4
>>> 2 / 5.0
0.4
>>> 2.0 / 5.0
0.4

No? This is troublesome (in Python2.x). According to the mathematical calculation, the above four calculation results should be 0.4. However, the first three matching results are 0. Why?

Because, in Python (strictly speaking, Python2.x, Python3 will change), there is a rule that, like division in 2/5, is to take an integer (that is, remove decimal places, but not rounding them down ). 2 divided by 5, the quotient is 0 (integer), and the remainder is 2 (integer ). If this form is used: 2/5, the calculation result is the quotient integer. It can be understood as follows: an integer is divided by an integer, and the result is an integer (quotient ).

For example:

>>> 5 / 2
2
>>> 7 / 2
3
>>> 8 / 2
4

Note: The result is a quotient (integer), instead of an integer containing decimal places, and then rounded up by rounding. For example: 5/2, the result is quotient 2, remainder 1, and final 5/2 = 2. Not rounding 2.5.

Float points and Integers
The title format is different from the title format above. The title above is "integer divided by integer". If the style is consistent, the title of this section should be "floating point number divided by integer", but no, it is now a "floating point number and integer division", which means:

Assume that x is divided by y. X may be an integer or floating point number. y may be an integer or floating point number.
Before coming to the conclusion, we should do the experiment first:

>>> 9.0 / 2
4.5
>>> 9 / 2.0
4.5
>>> 9.0 / 2.0
4.5

>>> 8.0 / 2
4.0
>>> 8 / 2.0
4.0
>>> 8.0 / 2.0
4.0

Induction to obtain the rule: whether it is a divisor or a divisor, if 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 / 3
3.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. Of course, there will be even more amazing appearances:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1 + 0.1 - 0.2
0.0
>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17
>>> 0.1 + 0.1 + 0.1 - 0.2
0.10000000000000003

Why is the computer girl so confused about the simplicity of computing? Not a computer girl, she is still ice and snow smart. The reason is that in decimal and binary conversion, the computer girl uses binary for calculation. In the above example, we input decimal, and she will convert the decimal number to binary, then calculate. However, in conversion, if a floating point number is converted into a binary number, a problem occurs.

For example, convert decimal 0.1 to binary: 0. 0001100110011001100110011001100110011001100110011...

That is to say, after being converted to binary, it is not exactly equal to 0.1 in decimal format. At the same time, there are limits on the number of digits stored by computers.

This problem is not only encountered in Python, but is not a Python bug in all programming languages that support floating-point calculation.

After understanding the cause of the problem, how can this problem be solved? For Python floating-point operations, the error of each calculation on most machines cannot exceed 2 ** 53 points. This is enough for most tasks, but remember that this is not a decimal algorithm, and each floating point calculation may bring a new round-off error.

Generally, as long as the final displayed result is simply rounded to the expected decimal digit, the expected final result is obtained.

You can use the decimal module to perform decimal operations that require high accuracy. In addition, the fractions module supports another form of operation, which implements operations based on rational numbers (so numbers such as 1/3 can be accurately expressed ). The highest requirement is that the Numerical Python package provided by SciPy and other packages for mathematics and statistics are used. Listing these things is just to make the viewer understand that there are many ways to solve the problem, and some of these methods will be used later to solve the problem.

For the infinite loop decimal problem, I have a link to recommend to you. It is not as simple as you think. Please read: Wikipedia entry: 0. 999... will you have any in-depth experience?

Additional information for interested friends to read: floating-point number algorithms: disputes and restrictions
Python always provides a variety of solutions to the problem, which is her style.

Reference module solves division-enable Wheel
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.

The wheel is a great invention of mankind.

Python is like this, there are various wheels, we only need to use. 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.
No worries. Experiment 1:

>>> from __future__ import division
>>> 5 / 2
2.5
>>> 9 / 2
4.5
>>> 9.0 / 2
4.5
>>> 9 / 2.0
4.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.

Remainder
In the previous calculation of 5/2, the quotient is 2, and the remainder is 1.

How can I get the remainder? In Python (in most languages, too), use the % symbol to obtain the remainder of the two numbers division.

Perform the following operations:

>>> 5 % 2
1
>>> 6%4
2
>>> 5.0%2
1.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) # means 5 divided by 2, returns the quotient and remainder
(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


Articles you may be interested in:
  • Python-based Tkinter implements a simple calculator
  • Python uses the datetime module to calculate the time difference
  • How many weekends have been computed in Python?
  • Python calculates the word Distance Based on the Dynamic Planning Algorithm
  • Python simple calculation of folder size Method
  • Python countdown
  • Python Method for Calculating the circumference pi
  • How to calculate the average value of a sequence in python
  • How to calculate the number of lines of text files in python
  • How to calculate the number of words in a file using Python


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.