Numbers and Basic Calculations 数字和基本计算

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

数字是Python中的一种对象类型。
Numbers are one type of object in Python.
实际上,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.
这意味着您的整数永远不会太长,无法适应Python的整数类型。
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.
所有Python数字都支持所有常用的算术运算。
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.
同样,Python对整数具有无限精度。
Again, Python has unlimited precision for integers.
让我们试试除法。
Let’s try out division.
除法是用斜杠来完成的。
Division is accomplished using the slash.
例如,如果我们说6除以7,Python会给出浮点答案。
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.
在本例中,让我们做一些类似于15除以7的事情,在浮点中是2.14。
In this case, let’s do something like 15 divided by 7, which in a floating point is 2.14.
如果我们使用整数除法,Python将给我们一个2的答案。
If we use integer division, Python is going to give us an answer of 2.
所以这里发生的事情如下:Python像往常一样执行除法,得到的数字是2.14。
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.
因此,浮点数2.14向下舍入为更接近的较小整数,即2。
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.
下划线运算符的值始终是Python返回给您的最后一个对象。
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.
让我们做一个简单的除法——比如说,15除以2.3。
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.
现在,如果我点击下划线,Python将返回最新操作的值。
Now, if I hit underscore, Python is returning the value of the latest operation.
假设我想将这个数字乘以2.3,这将返回15的数字。
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.
我只取下划线,乘以2.3,答案是15。
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.
让我们先从10乘以2开始。
Let’s first start with 10 times 2.
那是20。
That’s 20.
我们取这个数,再加上5,然后取25,再把它提高到二次方。
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.
在这种情况下,答案是625。
In this case, the answer is 625.
我们通常需要超越Python提供的内置函数和操作。
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.
所以n的阶乘,用感叹号表示,是n乘以n减去1乘以2乘以1。
So the factorial of n,indicated by exclamation mark, is n times n minus 1 all the way times 2 times 1.
例如,3阶乘等于3乘以2乘以1,也就是6。
And so for example, 3 factorial would be equal to 3 times 2 times 1, which is 6.
现在的问题是,我们如何用Python实现这个简单的计算?
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.
我们感兴趣的函数是math.factorial。
And the function we’re interested in is math.factorial.
本例中的参数是3,Python告诉我们3阶乘等于6。
So the argument in this case would be 3, and Python tells us 3 factorial is equal to 6.


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