python内置的数学函数_Python数字和内置数学函数

python内置的数学函数

In this section, we will be learning about Numbers and various Math functions available in python language. In Numbers, we will see some of the most commonly used math operators that we can use to perform various operations on the numbers in python. Under Math functions section, we will learn about some shortcuts (called functions), which are very helpful in calculating some of the complex mathematical expressions like power, sine/cosine, factorials etc. So, let's begin. We recommend keeping the IDLE open, while reading, so that you can practice and learn simultaneously.

在本节中,我们将学习数字和可用python语言提供的各种Math函数。 在数字中,我们将看到一些最常用的数学运算符,可用于对python中的数字执行各种运算。 在“数学函数”部分,我们将学习一些快捷方式(称为函数 ),这些快捷方式对于计算一些复杂的数学表达式(例如powersine / cosine阶乘等)非常有用。因此,让我们开始吧。 我们建议您在阅读时保持空闲状态,以便您可以同时练习和学习。

号码 (Numbers)

In Python we have 6 basic mathematical operators, they are:

在Python中,我们有6个基本的数学运算符 ,它们是:

  1. Addition

    加成

  2. Subtraction

    减法

  3. Multiplication

    乘法

  4. Division

  5. Modulo

    模数

  6. Power

    功率

Most of you must be familiar with all of the above operators except for the modulo operator. Don't worry we will explain it. Let's start from the beginning.

除了取运算符外,大多数人都必须熟悉上述所有运算符。 不用担心,我们会解释它。 让我们从头开始。

加成 (Addition)

As you might have guessed it's just simple addition of numbers. In order to test the operator, just go to IDLE and type a number, then addition sign +, and then another number to add to the first number. Press Enter. This must look like this.

您可能已经猜到了,这只是数字的简单加法。 为了测试操作员,只需转到IDLE并键入一个数字,然后输入加号+ ,然后再输入另一个数字即可添加到第一个数字。 按Enter键 。 这看起来必须像这样。

Example: Taking 8 and 19 as example,

范例:以8和19为例,

>>> 8+19
27
Adding Numbers in Python

On pressing return(or enter), the answer will appear just below the code line. And this is how the output will be displayed, all the time - just below your code line. As you'll hit the enter key, output will appear in the line below.

回车键 (或输入 )后,答案将出现在代码行的正下方。 这就是输出始终显示的方式- 刚好在代码行下方 。 当您按Enter键时,输出将显示在下面的行中。

Don't stop with this example, try using the addition operator with other numbers. Try number with decimal places, like 4.5 + 5.5 and so on.

不要仅仅停留在这个例子上,尝试将加法运算符与其他数字一起使用。 尝试使用带小数位的数字,例如4.5 + 5.5 ,依此类推。

减法 (Subtraction)

Just like addition, subtraction has the same syntax. Just change the operator to -. Again, pick some random numbers and try.

就像加法一样,减法具有相同的语法。 只需将运算符更改为- 。 再次,选择一些随机数并尝试。

Example: We took 89.33 and 23.67, which gave the output 65.55.

示例:我们采用89.3323.67 ,得出的输出为65.55

Subtracting Numbers in Python

乘法 (Multiplication)

Same again! Just change the operator to *, also known as an asterisk. You do know that it's used for multiplication, right? Go ahead and try it in you IDLE.

又是一样! 只需将运算符更改为* ,也称为星号 。 您知道它用于乘法,对吗? 继续并在您的IDLE中尝试它。

Example: Take any two numbers and multiply them using the * operator, just like we did below.

示例:取任意两个数字,并使用*运算符将它们相乘,就像我们在下面所做的那样。

Multiplying Numbers in Python

(Division)

Use / sign this time. And try with random numbers. Caution: If you're a beginner, you might find some difficulty in this one. How? Let's see. Let's take some integer numbers (numbers without decimal) like 16 and 2, and divide them.

这次使用/签名。 并尝试使用随机数。 警告:如果您是初学者,则可能会遇到一些困难。 怎么样? 让我们来看看。 让我们采用一些整数(没有小数的数字),例如162 ,并将它们相除。

>>> 16/2
8

Very well. Next, try with 15 and 2. What do you expect the answer would be? Well, according to proper mathematics the answer should obviously be 7.5, but if you actually try this in IDLE, the answer will turn out to be 7. This happened, because if we perform any mathematical operation on an integers then the answer would be an integer. In our case, 15 and 2 both are integers, hence, our answer is 7, as the answer has to be an integer.

很好。 接下来,尝试152 。 您期望答案是什么? 好吧,根据正确的数学,答案显然应该是7.5 ,但是如果您实际上在IDLE中尝试过,答案将是7 。 发生这种情况是因为,如果我们对整数执行任何数学运算,则答案将是整数。 在我们的例子中, 152都是整数,因此我们的答案是7 ,因为答案必须是整数。

You might be wondering if it had to be an integer, why it turned out to be 7 and why not any other integer number. Well, that is because the answer is determined as the closest, smaller integer to the original answer. In our case, the original answer is 7.5, thus the nearest integer to it is 7 and 8, and since we have to pick the smaller one; 7 is picked as the answer. In mathematics, it is also known as floor function (it's there in Python too).

您可能想知道它是否必须为整数,为什么它必须为7以及为什么没有其他整数。 好吧,这是因为答案被确定为与原始答案最接近的较小整数。 在我们的例子中,原始答案是7.5 ,因此最接近的整数是7和8,因为我们必须选择较小的整数; 选择7作为答案。 在数学中,它也被称为floor函数(在Python中也存在)。

Now talking about the solution to the above problem, all you have to do is, convert any one of the integers(that you want to divide) into decimal, i.e. write 15.0 instead of 15 and/or 2.0 instead of 2.

现在讨论上述问题的解决方案时,您要做的就是将任意一个(要除法的)整数转换为十进制,即写15.0而不是15和/或2.0而不是2。

Dividing Numbers in Python

功率 (Power)

This mathematical operator is not usually found in common programming languages. In fact, Python is the only language we know which does have an operator for this. In rest of the languages, they use some pre-defined functions (shortcut as we mentioned before) to calculate this. Getting to the point, just put two asterisks like ** between any two numbers. Example, to calculate 2 to the power 10, you have to write:

在常见的编程语言中通常找不到这种数学运算符。 实际上,Python是我们知道的唯一对此具有运算符的语言。 在其他语言中,它们使用一些预定义的函数(如我们前面提到的快捷方式)进行计算。 讲到重点,只需在任意两个数字之间加上两个星号(如** 。 例如,要计算2的幂10 ,您必须编写:

>>> 2**10
1024

With that, we now know about all the commonly used mathematical operators of python. Now you can try to combine multiple operators and use them to form one expression. We will recommend using brackets so that python can understand what you want as the answer, i.e. instead of writing 2-9.0/2, write 2-(9.0/2). Remember BODMAS, how a mathematical expression with multiple operators is solved in mathematics.

这样,我们现在了解了所有常用的python数学运算符。 现在,您可以尝试组合多个运算符并使用它们来形成一个表达式。 我们将建议使用方括号,以便python可以理解您想要的答案,即代替编写2-9.0/2 ,而编写2-(9.0/2) 。 记住BODMAS ,如何在数学中解决具有多个运算符的数学表达式。

Power of Numbers in Python

模数 (Modulo)

Modulo operator is denoted by % percentage sign. If you are familiar with the computer programming world, chances are you already know this function. In case you don't, no need to worry. You know division, right? Then you know what remainder is, correct? This Modulo operator, when used with two operands, returns the remainder as the answer. Here are some quick examples.

模运算符记%百分号。 如果您熟悉计算机编程领域,那么您很可能已经知道此功能。 如果您不这样做,则不必担心。 你知道分裂吧? 那你知道余数是对的吗? 与两个操作数一起使用时,此Modulo运算符将返回余数作为答案。 这里有一些简单的例子。

12%2 = 0, since 2 perfectly divides 12.

12%2 = 0 ,因为2完美除以12。

13%2 = 1, since dividing 13 with 2 leaves 1 as remainder.

13%2 = 1 ,因为用13除以2留下1作为余数。

19%5 = 4, because, again, 19/5 leaves 4 as the remainder.

19%5 = 4 ,因为,再次, 19/5叶片4作为剩余部分。

It is used in pretty much the same way as it has been explained here.

它的用法与此处已解释的用法几乎相同。

Number Modulo in Python

To see all the Math operators covered above, live in action, click on the Live Example button,

要实时查看上面介绍的所有数学运算符,请点击“实时示例”按钮,

Live Example →

现场示例→

And that concludes the numbers section. Let's dive into math's function now.

到此结束数字部分。 现在让我们深入研究数学函数。

Python中的数学函数 (Math Functions in Python)

As you learn more about python, you might decide to create a scientific calculator for a project or anything. For that, along with simple mathematical operations, you will have to evaluate some complex mathematical operations as well, like trigonometric operations, logarithmic operations etc. Forget about the calculator, there can be various situations where you might need these functions. Like a software for civil engineers to calculate various parameters of any structure that they are building, or any aerospace software - where they need various kinds of calculations about satellite trajectory, shuttle trajectory and what not. In a nutshell, the complex mathematical operations are used in various real life programs and softwares, hence you must know about them.

随着对python的更多了解,您可能会决定为项目或其他任何东西创建科学的计算器。 为此,除了简单的数学运算之外,您还必须评估一些复杂的数学运算,例如三角运算,对数运算等。忘掉计算器,在各种情况下可能需要这些函数。 例如,土木工程师可以使用软件来计算他们正在建造的任何结构的各种参数,或者可以使用任何航空航天软件-在该软件中,他们需要进行各种有关卫星轨迹,航天飞机轨迹以及其他方面的计算。 简而言之,复杂的数学运算用于各种现实生活的程序和软件中,因此您必须了解它们。

Now in Python, some nice guys have already created code pieces (libraries) for almost every mathematical function. We can use these codes without any hesitation and the plus point is, we won't have to re-write it again. Forget about rewriting, we don't even have to know what the complete code is. We only need a few key information to be able to use these readymade code pieces.

现在,在Python中,一些好人已经为几乎每个数学函数创建了代码段(库)。 我们可以毫不犹豫地使用这些代码,加号是,我们不必再次重写它。 不用担心重写,我们甚至不必知道完整的代码是什么。 我们只需要一些关键信息就可以使用这些现成的代码段。

Alright, so unofficially function part had already begun. We'll learn about functions in detail in later chapter, thus we'll keep this one short.

好吧,如此非正式的功能部分已经开始了。 我们将在后面的章节中详细了解函数,因此将其简短化。

Function can be described as a piece of code that may or may not take some value(s) as input, process it, and then finally may or may not return any value as output. 函数可以描述为一段代码,该代码可以或可以不将某些值作为输入,对其进行处理,然后最终可以或可以不将任何值作为输出。
Math Functions in Python

As you can see in the figure above, here input x is given to a function f and it is giving some value f(x) as the output. Although in general programming world, depending upon the purpose of the function, input and output are completely optional. But for a mathematical function, it's very important to have both.

如您在上图中所看到的,此处将输入x赋予函数f ,并给出一些值f(x)作为输出。 尽管在一般的编程世界中,根据功能的目的,输入和输出完全是可选的。 但是对于一个数学函数来说,同时拥有这两者是非常重要的。

For example, in the trignometric function sin(x), there must be some value of x in order to calculate and return the answer, and that pretty much establish why mathematical functions have both input and output.

例如,在三角函数sin(x) ,必须有x某个值才能计算并返回答案,并且这几乎可以确定为什么数学函数既具有输入又具有输出

In python, there are two types of pre-defined functions.

在python中,有两种类型的预定义函数。

  • Inbuilt functions: These are the functions which doesn't require any other(external) code file (known as, Modules or Library Files). These are a part of the python core and are just built within the Python compiler hence there is no hassle of importing these modules/libraries in our code.

    内置函数 :这些函数不需要任何其他(外部)代码文件(称为ModulesLibrary Files )。 这些是python核心的一部分,仅在Python编译器中构建,因此在我们的代码中导入这些模块/库没有麻烦。

  • The second type of functions require some external files(modules) in order to be used. The process of using these external files in our code is called importing. So all we have to do is import the file into our code and use the functions which are already written in that file.

    第二种功能需要使用一些外部文件(模块)才能使用。 在我们的代码中使用这些外部文件的过程称为导入 。 因此,我们要做的就是将文件导入到我们的代码中,并使用已经在该文件中编写的功能。

It's time to try some of the functions. Let's begin with power functions.

现在是时候尝试一些功能了。 让我们从幂函数开始。

功率pow(x,y) (Power - pow(x,y))

I know what you might be thinking. We just tried that, didn't we? Well, we did saw something that can calculate power, but it was an operator and this one is an inbuilt function (yes, the first type). So, with that just consider this one as an alternative way to calculate power.

我知道你在想什么。 我们只是尝试过,不是吗? 好吧,我们确实看到了可以计算功效的东西,但这是一个运算符,而这是一个内置函数(是,第一个类型)。 因此,将其视为计算功率的另一种方法。

Since this one is an inbuilt function, you don't need to import any other library files (or modules), hence it's pretty easy to implement.

由于这是一个内置函数,因此您无需导入任何其他库文件(或模块 ),因此实现起来非常容易。

Since power function will be needing two numbers(inputs) to perform the operation, i.e. base and exponent, hence we will have to provide two numbers to the function. Go ahead, open the IDLE and write:

由于幂函数将需要两个数字(输入)来执行操作,即baseexponent ,因此我们将必须为该函数提供两个数字。 继续,打开IDLE并输入:

>>> pow(3,2)

Now let's analyse what we did and what will happen. First, we wrote pow, which is simply the name of the function that we are trying to call. This will tell the python compiler to look out for an inbuilt function named pow and discover what it can do. Next, within the brackets we wrote two numbers separated with a comma, i.e. 3 and 2. Here the first number 3 is base and the second number 2 is an exponent, and we are trying to calculate 32.

现在,让我们分析一下我们做了什么以及将会发生什么。 首先,我们编写了pow ,它只是我们要调用的函数的名称。 这将告诉python编译器寻找一个名为pow的内置函数,并发现它可以做什么。 接下来,在方括号内,我们用逗号分隔两个数字,即32 。 在这里,第一个数字3是底数 ,第二个数字2是指数 ,我们正在尝试计算3 2

Once the python compiler has ensured that all the syntax (the grammar of programming) is correct, it will look for the implementation of the function pow and use it to find 32. So as you might have expected, the output would be:

一旦python编译器确保所有语法(编程语法)正确无误,它就会查找pow函数的实现,并使用它找到3 2 。 如您所料,输出将是:

Math Functions in Python

With that, we now know, how a function is called. Especially for math functions, we can generalise it as following:

这样,我们现在知道如何调用函数。 特别是对于数学函数,我们可以将其概括如下:

>>> functionName(input1, optionalInput2, optionalInput3, ...)

The values inside brackets that had been separated by commas, which we mentioned to you as input to functions, are called Arguments. As in pow(x, y) example given above, 3 and 2 were the arguments. There can be any number of arguments in a function. And as we discussed earlier, for a mathematical function there usually is at least one argument present. Let's see some another inbuilt mathematical functions.

方括号内用逗号分隔的值(称为函数的输入)称为Arguments 。 就像上面给出的pow(x, y)示例一样,3和2是自变量。 函数中可以有任意数量的参数。 正如我们前面所讨论的,对于一个数学函数,通常至少存在一个参数。 让我们看看另一个内置的数学函数。

绝对abs(x) (Absolute - abs(x))

Absolute function, also known as Modulus (not to be confused with Modulo), returns the non-negative value of the argument value. Therefore, absolute value of any non-negative number is the same, while for negative numbers, their positive value is returned.

绝对函数,也称为Modulus (不要与Modulo混淆),返回参数值的非负值 。 因此,任何非负数的绝对值都相同,而对于负数,将返回其正值。

Example: absolute value of -3 will be 3, absolute value of -8.74 will be 8.74 and so on.

示例: -3绝对值为3-8.74绝对值为8.74 ,依此类推。

Syntax:

句法:

>>> abs(-99.99)

Since -99.99 is a negative number, it's positive counterpart will be the output, i.e. 99.99.

由于-99.99是负数,因此它的正数将是输出,即99.99

Math Functions in Python

Now let's try some functions where we have to import some modules(or library files).

现在让我们尝试一些必须导入一些模块(或库文件)的功能。

正弦sin(x) (Sine - sin(x))

Since we know sine is a trigonometric function, hence it accepts only one value as an argument, i.e. x. Here x should be in radians, so you better not confuse it with a degree. As we mentioned before we won't be able to use this function directly. If you do, you might get an error, something like this, which will say name sin is not defined.

由于我们知道正弦是三角函数,因此它仅接受一个值作为参数,即x 。 x应该是弧度 ,因此最好不要将其与度数混淆。 如前所述,我们将无法直接使用此功能。 如果这样做,可能会收到类似这样的错误消息,说明未定义名称sin

Math Functions in Python

This is because the compiler doesn't know what it is supposed to do when it encounters sin() function, as we have not defined this function but we are trying to use it. So, in order to use it, we will have to import python's math module which consists the implementation of the sin() function, which will guide the python compiler to understand what to do when sin() is called.

这是因为编译器在遇到sin()函数时不知道应该做什么,因为我们尚未定义此函数,但我们正在尝试使用它。 因此,为了使用它,我们将必须导入python的math模块 ,该模块包含sin()函数的实现,该模块将引导python编译器了解调用sin()方法。

What we are about to do is called importing a module and it's most oftenly done to use already available ready-made functions. Importing a module takes just one extra line:

我们要做的就是导入模块 ,最常见的是使用已经可用的现成函数 。 导入模块只需要多一行:

>>> import math

Hit enter, and you're done. Now in order to use the sin() function, go to a new line and type:

按Enter键,就完成了。 现在,为了使用sin()函数,转到新行并键入:

>>> math.sin(3.14159)

Since 3.14159 is approximately the value of π hence the answer would be near to zero.

由于3.14159近似为π的值,因此答案将接近于零。

Math Functions in Python

As you can see after math.sin(3.14159) statement, the answer returned was something like 2.653589335273e-6, it might seem a little messy but it is an equivalent representation of 2.653589335273 × 10^-6, or 0.000002653589335273.

正如您在math.sin(3.14159)语句之后所看到的math.sin(3.14159) ,返回的答案类似于2.653589335273e-6 ,似乎有些混乱,但它等效于2.653589335273 × 10^-60.000002653589335273

Because of approximation in the value of π, the answer too just got deviated a little bit from 0, but you can see the value is almost zero. And at the same time, you can see how accurate the results are.

由于π值近似,答案也只是从0偏离了一点,但是您可以看到该值几乎为零。 同时,您可以看到结果的准确性。

Now of course there are several other functions available inside math module, like floor() (floor function; We mentioned this one in division operator), exp() (Exponential function), log() (Logarithmic function), sqrt() (Square root) and a lot more. You can check out the list, their syntax, number of arguments accepted and everything at Python's official website - Mathematical Functions. Python has a manual on their website where you can see all the functions listed with all the details. This manual is called Documentation.

当然,现在数学模块中还有其他几个可用的函数,例如floor() (地板函数;我们在除法运算符中提到了这一函数), exp() (指数函数), log() (对数函数), sqrt() (平方根)等等。 您可以在Python的官方网站Mathematical Functions上查看列表,语法,接受的参数数量以及所有内容。 Python在其网站上有手册,您可以在其中查看列出的所有功能以及所有详细信息。 本手册称为文档

翻译自: https://www.studytonight.com/python/numbers-and-math-functions

python内置的数学函数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值