Numbers and Basic Calculations 数字和基本计算

1.1.4: Numbers and Basic Calculations 数字和基本计算

Numbers are one type of object in Python.

And Python, in fact, provides three different numeric types.

These are called integers, floating point numbers, and complex numbers.
Python整数的一个有趣之处是它们具有无限的精度。
One of the interesting things about Python integers is that they have unlimited precision.

That means your integer will never be too long to fit into Python’s integer type.

Another important aspect to realize about numbers is that you can freely mix different numeric types.

All Python numbers support all the usual arithmetic operations.

And let’s try a few of them out right now.

The basics, like addition and multiplication are, of course, easy.

We can also raise a number to a power.

And you’ll see that if we increase the exponent a little bit,the result is going to be a very large number,but that’s not going to be a problem.

Again, Python has unlimited precision for integers.

Let’s try out division.

Division is accomplished using the slash.

So for example, if we say 6 divided by 7,Python gives us the floating point answer.

Sometimes, however, we might want to do what’s called floor division, or integer division.

This is accomplished by using two slash signs.

In this case, let’s do something like 15 divided by 7, which in a floating point is 2.14.

If we use integer division, Python is going to give us an answer of 2.

So what happened here is the following: Python carries out the division as usual and that gives you the number 2.14.

It then rounds that number to the closest integer,which is less than the actual floating point answer.

So 2.14, a floating point number, gets rounded down to the closer, smaller integer, which is 2.
Python中的交互模式提供了一个非常有用的操作，即下划线运算符。
The interactive mode in Python provides a very useful operation which is the underscore operator.

And the value of the underscore operator is always the last object that Python has returned to you.

So let’s see what that means.

Let’s do a simple division– 15 divided by 2.3, say.
Python告诉我们它是6.521，以此类推。
Python tells us that it’s 6.521 and so on.

Now, if I hit underscore, Python is returning the value of the latest operation.

Say I wanted to multiply this number with 2.3, which would give me back the number of 15.

So I can do that in the following way.

I just take the underscore,I multiply that by 2.3, and the answer is 15.

This is especially handy in the interactive mode if you’d like to change a few calculations.

So let’s try one example.

That’s 20.

Let’s take that number,let’s add 5 to it,and let’s then take that number 25 raise that to the second power.

In this case, the answer is 625.

Very commonly we need to go beyond the built-in functions and operations that Python provides.

And one way to do this for numbers is to use the math module, which contains some basic mathematical operations,like the factorial.

Let’s just quickly remind ourselves, what is the factorial operation.

So the factorial of n,indicated by exclamation mark, is n times n minus 1 all the way times 2 times 1.

And so for example, 3 factorial would be equal to 3 times 2 times 1, which is 6.

Now, the question is, how could we implement this simple calculation in Python?

Let’s start by importing the math module.

So we type import math.

And the function we’re interested in is math.factorial.

So the argument in this case would be 3, and Python tells us 3 factorial is equal to 6.

上一篇 Random Choice 随机选择
1.1.5: Random Choice 随机选择通常，当我们使用数字时，偶尔也会使用其他类型的对象，我们希望使用某种类型的随机性。Often when we’re using numbers, but also,occasionally,
2021-10-18 Expressions and Booleans 表达式和布尔值
1.1.6: Expressions and Booleans 表达式和布尔值表达式是计算值的对象和运算符的组合。Expression is a combination of objects and operators that compu
2021-10-18
目录