[Zero-Basic python] Four arithmetic operations in python and four arithmetic operations in python

Source: Internet
Author: User

[Zero-Basic python] Four arithmetic operations in python and four arithmetic operations in python
When it comes to computers, of course more people now call her computers. Both of them refer to computers. Whatever it is, as long as she is mentioned, it is common to think that she can quickly perform addition, subtraction, multiplication, division, and even the square. Even some people cannot separate computers and calculators in their oral English.

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


Review
Let's take a look at the great moment and print hello world.

Open your computer, run python idle, and enter:

>>> print 'Hello, World'Hello, World

Take a close look at the official website. Have you noticed that I used single quotes here, and double quotes last time. The two have the same effect, that is, in this case, single quotes and double quotes have the same effect. They must appear in pairs. The other half cannot be single quotes, and the other half is double quotes.

Arithmetic Operation


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

>>> 2+57>>> 5-23>>> 10/25>>> 5*210>>> 10/5+13>>> 2*3-42

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 as the need for learning now. They must not let future generations learn new computing rules, so they should 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. I still need to calculate it in ide and check whether the results are different? Observe it carefully.

>>> 4+26>>> 4.0+26.0>>> 4.0+2.06.0

The difference is that the first sub-result is 6, and the last two are 6.0.

Now we need to introduce two data types: integer and floating point.

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.

  • 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.
It is easy to understand. integers are the integers learned in elementary school. Floating-point numbers are decimals. If an integer is written as a decimal number, for example, 4 is written as 4.0, it becomes a floating point number.

Love learning requires a spirit of exploration. When you google the integer on the internet, you will find another word: long integer (type ). As the name suggests, it is a long integer. In some languages, we use this as a separate class to partition, but in python, we don't have to worry about this. As long as it is an integer, it is only an integer, and there is no need to distinguish the length (in previous versions), because the distinction is meaningless and does not coordinate with the math knowledge that has been learned in primary school.

There is another problem. I need to explain it to the viewer. It may not be available in the near future, but some people will use this to fool you. When he fooled you, the following knowledge will be used.

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 refer to the 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 int, integer >>> type (5.0) <type 'float'> #5.0 is float, floating point type (988776544222112233445566778899887766554433221133344455566677788998776543222344556678) <type 'long'> # It is a long integer and an integer.

Summary
  • In python, the addition, subtraction, multiplication, division are executed according to the rules of primary school mathematics.
  • You don't have to worry about big integers. python will automatically handle them.
  • Type (object) is a useful thing.
Statement

This article is a castrated version. To view the full version, go to BasicPython in my github: qiwsir's ITArticles.



Python arithmetic exercises

Print str ('"room area:" (int (a * B )')
Print str ('"room perimeter:" (int (a * 2 + B * 2 )')
The expression is embedded in a string and is printed directly as a string.
Change to (python2.7.5 ):
A = input ('length :')
B = input ('width :')
Print 'room size: % I '% (a * B)
Print 'room perimeter: % I '% (a * 2 + B * 2)

In python3, change the print usage.

Is it appropriate to study python without basic knowledge?

Of course you can. In fact, it's similar to reading tutorials online.
If programming really has no foundation, you can certainly start with python, but quick development doesn't matter how fast it feels or how much accumulation you have.

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.