With the old Ziko Python computing _python

Source: Internet
Author: User
Tags arithmetic in python

When it comes to computers, of course more people call her a computer now, and these two words refer to computer. No matter what, as long as mentioned her, it is common to think that she can do more quickly subtraction, even the powers and so on. Some people do not distinguish between computers and calculators in spoken English.

So, as a zero basis to learn this, but also from the calculation of elementary school math problem began. Because from here onwards, the basic knowledge of mathematics as a sure pass.

Review

Let's first revisit the great moment and print Hello world.

Turn on the computer, let the Python idle run, and then type:

>>> print ' Hello, world '
Hello, world

Careful reader, do you notice that I use single quotes here, and last time I used double quotes. The effect is the same, in this case, single and double quotes are the same effect, must be in pairs, not half is single quotes, the other half is double quotes.

Arithmetic

Run it in the IDE to see if the results are consistent with what you've done with the math in elementary school, according to the following requirements

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

In the above operation, four operational symbols are involved: plus (+), minus (-), multiply (*), except (/)

In addition, I believe reader has found an important axiom:

In computers, arithmetic and elementary school mathematics have learned the arithmetic rules are the same

Or to say that people are animals, the things they invented, must inherit the knowledge they have mastered, do not pass the history of their own. Great scientists, in the original design of the computer when the thought of as the need for learning now, must not let future generations to learn new rules of operation, in the elementary school mathematics inside the good. Thanks to the pioneers of the scientists, Ze was hereafter.

Here are three math questions to see what the results are.

4 + 2
4.0 + 2
4.0 + 2.0

Reader may be angry, such a simple topic, do not bother the computer, too wasteful.

Take it easy, do it in the IDE, and look at the results. Be careful to observe.

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

The difference is: the first formula results in 6, and the back two is 6.0.

Now we're going to introduce two data types: integers and floating-point numbers

The definition of these two, without rote, Google. Remember what Einstein said: "I don't remember anything in the book." It seems to be, probably mean, I don't remember it anyway. The second half of the sentence he did not say, I added: Forget about Google.

Definition 1: Like 4,-2, 129486655,-988654, 0, such as the number of forms, called integers
Definition 2: A number similar to 4.0,-2.0, 2344.123, 3.1415926, called floating-point numbers
Better understanding, integers, is the primary school learned the integer; floating point number, is a decimal. If the integer is written in decimal form, such as 4 written as 4.0, it becomes a floating-point number.

Love to learn, you must have the spirit of exploration. Reader on the internet to Google an integer, you will find another word: Long integer (Type). As the name suggests, is a relatively long integer. In some languages, this is done as a separate category, but in Python we don't have to worry about that. As long as integers, just integers, do not distinguish between length (the previous version of the distinction), because the distinction does not mean, and with the elementary school mathematics knowledge does not coordinate.

There is still a problem, need to explain to reader, the eyes may not use, but there will always be some people use this to cheat you, when he fooled you, the following knowledge is used.

Integer overflow problem

Here is an article devoted to this issue, recommended reading: integer overflow

For other languages, integer overflows must be addressed, but in Python, reader is not sad, because Python solves this problem for us, read the bad text: multiply the big integers

ok! reader can experiment with large integer multiplication in the IDE.

>>> 123456789870987654321122343445567678890098876*1233455667789990099876543332387665443345566
152278477193527562870044352587576277277562328362032444339019158937017801601677976183816L


Reader is lucky, Python is sad, so choosing to learn Python is a treasure of time.

The result of the above number has an L, which means that the number is a long integer, but reader do not care about this, anyway, python for us.

Before closing this section, there are two symbols that need to be reader remember (not to remember, it doesn't matter, you can always Google, just remember to use more convenient)

Integer, expressed in int, from Word: integer
Float, expressed in float, is the word: float
You can use a command: Type (object) to detect what a number is.

>>> type (4)
<type ' int ' >  #4是int, Integer
>>> type (5.0)
<type ' float ' > # 5.0 is float, floating-point number
type (988776544222112233445566778899887766554433221133344455566677788998776543222344556678)
<type ' long ' >  #是长整数, also an integer

Summarize

Subtraction in Python is executed in accordance with the elementary school math rules
Don't worry about big integer problems, Python will automatically handle
Type (object) is a useful thing

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.