Home > Backend Development > Python Tutorial > Basic mathematical calculation methods in Python programming

Basic mathematical calculation methods in Python programming

高洛峰
Release: 2017-03-13 18:08:05
Original
2056 people have browsed it

This article mainly introduces the use of basic mathematical calculations in Pythonprogramming, focusing on the use of division operations and related pision modules. Friends in need can refer to the following

Number
In Python, the rules for logarithms are relatively simple and can be understood at the elementary school mathematics level.

So, as a zero-based learning, let’s start by calculating elementary school mathematics problems. Because from here on, you will definitely pass the basic knowledge of mathematics.


>>> 3
3
>>> 3333333333333333333333333333333333333333
3333333333333333333333333333333333333333L
>>> 3.222222
3.222222
Copy after login


The above display is in the interactive mode. If you enter 3, 3 will be displayed, so Numbers are called integers, and this name is the same as primary school mathematics.

If you enter a relatively large number, the second one, then an integer composed of multiple 3s, is called a long integer in Python. To indicate that a number is a long, Python displays an L at the end. In fact, Python is now able to automatically treat input large integers as long integers. You don't have to differentiate here.

The third one is called a decimal in mathematics. You can still call it that here, but like many programming languages, it is customary to call it a "floating point number". As for the origin of this name, there is some explanation. If you are interested, you can google.

In the above examples, it can be said that they are all unsigned (or non-negative numbers). If you want to express negative numbers, follow the expression method in mathematics. Just put a negative sign in front of it.

It is worth noting that what we are talking about here are all decimal numbers.

In addition to decimal, there are also binary, octal, and hexadecimal systems that may be used in programming. Of course, sexagesimal systems are rarely used (in fact, the time recording method is the typical sexagesimal system) .

Specifically, each number is an object in Python. For example, the 3 entered earlier is an object. Each object has its own address in memory, which is its identity.


>>> id(3)
140574872
>>> id(3.222222)
140612356
>>> id(3.0)
140612356
>>>
Copy after login


Use the built-in function id() to view the memory address, that is, the identity of each object.

Built-in function is called built-in Function in English. Readers can guess pretty much what it is based on its name. Yes, it is the internal function that has been defined in Python.
The above three different numbers are three different objects with three different memory addresses. Note in particular that mathematically, 3 and 3.0 are equal, but here, they are different objects.

The memory address obtained using id() is read-only and cannot be modified.

After understanding "identity", let's look at "type". There is also a built-in function for using type().


>>> type(3)
<type &#39;int&#39;>
>>> type(3.0)
<type &#39;float&#39;>
>>> type(3.222222)
<type &#39;float&#39;>
Copy after login


The type of object can be viewed using built-in functions. , indicating that 3 is an integer type (Interger); tells us that the object is a floating point type (Floating point real number). Similar to the result of id(), the result of type() is also read-only.

As for the value of the object, here it is the object itself.

It seems that the object is not difficult to understand. Please stay confident and continue.

Variable
Just writing 3, 4, 5 is not enough. In programming languages, "variables" and "variables" are often used. Numbers" (strictly speaking objects in Python) establish a corresponding relationship. For example:


>>> x = 5
>>> x
5
>>> x = 6
>>> x
6
Copy after login


In this example, x = 5 is established between the variable (x) and the number (5) Correspondence, and then establish a correspondence between x and 6. We can see that x first "is" 5, and then "is" 6.

In Python, it is very important to have this sentence: objects have types, variables have no types. How to understand it?

First of all, 5 and 6 are both integers. They are named in Python and are called "integer" type data, or data type is an integer, represented by int.

When we write 5 and 6 in Python, the computer girl will automatically create these two objects for us somewhere in her memory (the definition of the objects will be discussed later, you can use them here first) , the meaning will gradually become clear), it is like building two sculptures, one is shaped like 5, and the other is shaped like 6. These are two objects, and the types of these two objects are int.

that x Woolen cloth? It's like a label. When x = 5, the label x is tied to 5. Through this x, you can see 5 in succession, so in interactive mode, >>> The result output by x It is 5. It seems to people that x is 5, but the fact is that the label x is attached to 5. In the same way, when x = 6, the label changes position and is attached to 6.

所以,这个标签 x 没有类型之说,它不仅可以贴在整数类型的对象上,还能贴在其它类型的对象上,比如后面会介绍到的 str(字符串)类型的对象等等。

这是 Python 区别于一些语言非常重要的地方。

四则运算
按照下面要求,在交互模式中运行,看看得到的结果和用小学数学知识运算之后得到的结果是否一致


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


上面的运算中,分别涉及到了四个运算符号:加(+)、减(-)、乘(*)、除(/)

另外,我相信看官已经发现了一个重要的公理:

在计算机中,四则运算和小学数学中学习过的四则运算规则是一样的

要不说人是高等动物呢,自己发明的东西,一定要继承自己已经掌握的知识,别跟自己的历史过不去。伟大的科学家们,在当初设计计算机的时候就想到列位现在学习的需要了,一定不能让后世子孙再学新的运算规则,就用小学数学里面的好了。感谢那些科学家先驱者,泽被后世。

下面计算三个算术题,看看结果是什么


4 + 2
4.0 + 2
4.0 + 2.0
Copy after login


看官可能愤怒了,这么简单的题目,就不要劳驾计算机了,太浪费了。

别着急,还是要运算一下,然后看看结果,有没有不一样?要仔细观察哦。


>>> 4+2
6
>>> 4.0+2
6.0
>>> 4.0+2.0
6.0
Copy after login


不一样的地方是:第一个式子结果是 6,这是一个整数;后面两个是 6.0,这是浮点数。

定义 1:类似 4、-2、129486655、-988654、0 这样形式的数,称之为整数
定义 2:类似 4.0、-2.0、2344.123、3.1415926 这样形式的数,称之为浮点数
对这两个的定义,不用死记硬背,google 一下。记住爱因斯坦说的那句话:书上有的我都不记忆(是这么的说?好像是,大概意思,反正我也不记忆)。后半句他没说,我补充一下:忘了就 google。

似乎计算机做一些四则运算是不在话下的,但是,有一个问题请你务必注意:在数学中,整数是可以无限大的,但是在计算机中,整数不能无限大。为什么呢?(我推荐你去 google,其实计算机的基本知识中肯定学习过了。)因此,就会有某种情况出现,就是参与运算的数或者运算结果超过了计算机中最大的数了,这种问题称之为“整数溢出问题”。

整数溢出问题
这里有一篇专门讨论这个问题的文章,推荐阅读:整数溢出

对于其它语言,整数溢出是必须正视的,但是,在 Python 里面,看官就无忧愁了,原因就是 Python 为我们解决了这个问题,请阅读下面的拙文:大整数相乘

ok!看官可以在 IDE 中实验一下大整数相乘。


>>> 123456789870987654321122343445567678890098876*1233455667789990099876543332387665443345566
152278477193527562870044352587576277277562328362032444339019158937017801601677976183816L
Copy after login


看官是幸运的,Python 解忧愁,所以,选择学习 Python 就是珍惜光阴了。

上面计算结果的数字最后有一个 L,就表示这个数是一个长整数,不过,看官不用管这点,反正是 Python 为我们搞定了。

在结束本节之前,有两个符号需要看官牢记(不记住也没关系,可以随时 google,只不过记住后使用更方便)

整数,用 int 表示,来自单词:integer
浮点数,用 float 表示,就是单词:float
可以用一个命令:type(object)来检测一个数是什么类型。


>>> type(4)
<type &#39;int&#39;>  #4 是 int,整数
>>> type(5.0)
<type &#39;float&#39;> #5.0 是 float,浮点数
type(988776544222112233445566778899887766554433221133344455566677788998776543222344556678)
<type &#39;long&#39;>  # 是长整数,也是一个整数
Copy after login


除法
除法啰嗦,不仅是 Python。

整数除以整数
进入 Python 交互模式之后(以后在本教程中,可能不再重复这类的叙述,只要看到>>>,就说明是在交互模式下),练习下面的运算:

>>> 2 / 5
0
>>> 2.0 / 5
0.4
>>> 2 / 5.0
0.4
>>> 2.0 / 5.0
0.4
Copy after login


看到没有?麻烦出来了(这是在 Python2.x 中),按照数学运算,以上四个运算结果都应该是 0.4。但我们看到的后三个符合,第一个居然结果是 0。why?

因为,在 Python(严格说是 Python2.x 中,Python3 会有所变化)里面有一个规定,像 2/5 中的除法这样,是要取整(就是去掉小数,但不是四舍五入)。2 除以 5,商是 0(整数),余数是 2(整数)。那么如果用这种形式:2/5,计算结果就是商那个整数。或者可以理解为:整数除以整数,结果是整数(商)。

比如:

>>> 5 / 2
2
>>> 7 / 2
3
>>> 8 / 2
4
Copy after login


注意:得到是商(整数),而不是得到含有小数位的结果再通过“四舍五入”取整。例如:5/2,得到的是商 2,余数 1,最终5 / 2 = 2。并不是对 2.5 进行四舍五入。

浮点数与整数相除
这个标题和上面的标题格式不一样,上面的标题是“整数除以整数”,如果按照风格一贯制的要求,本节标题应该是“浮点数除以整数”,但没有,现在是“浮点数与整数相除”,其含义是:

假设:x 除以 y。其中 x 可能是整数,也可能是浮点数;y 可能是整数,也可能是浮点数。
出结论之前,还是先做实验:

>>> 9.0 / 2
4.5
>>> 9 / 2.0
4.5
>>> 9.0 / 2.0
4.5

>>> 8.0 / 2
4.0
>>> 8 / 2.0
4.0
>>> 8.0 / 2.0
4.0
Copy after login


归纳,得到规律:不管是被除数还是除数,只要有一个数是浮点数,结果就是浮点数。所以,如果相除的结果有余数,也不会像前面一样了,而是要返回一个浮点数,这就跟在数学上学习的结果一样了。


>>> 10.0 / 3
3.3333333333333335
Copy after login


这个是不是就有点搞怪了,按照数学知识,应该是 3.33333...,后面是 3 的循环了。那么你的计算机就停不下来了,满屏都是 3。为了避免这个,Python 武断终结了循环,但是,可悲的是没有按照“四舍五入”的原则终止。当然,还会有更奇葩的出现:


>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1 + 0.1 - 0.2
0.0
>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17
>>> 0.1 + 0.1 + 0.1 - 0.2
0.10000000000000003
Copy after login


越来越糊涂了,为什么 computer 姑娘在计算这么简单的问题上,如此糊涂了呢?不是 computer 姑娘糊涂,她依然冰雪聪明。原因在于十进制和二进制的转换上,computer 姑娘用的是二进制进行计算,上面的例子中,我们输入的是十进制,她就要把十进制的数转化为二进制,然后再计算。但是,在转化中,浮点数转化为二进制,就出问题了。

例如十进制的 0.1,转化为二进制是:0.0001100110011001100110011001100110011001100110011...

也就是说,转化为二进制后,不会精确等于十进制的 0.1。同时,计算机存储的位数是有限制的,所以,就出现上述现象了。

这种问题不仅仅是 Python 中有,所有支持浮点数运算的编程语言都会遇到,它不是 Python 的 bug。

明白了问题原因,怎么解决呢?就 Python 的浮点数运算而言,大多数机器上每次计算误差不超过 2**53 分之一。对于大多数任务这已经足够了,但是要在心中记住这不是十进制算法,每个浮点数计算可能会带来一个新的舍入错误。

一般情况下,只要简单地将最终显示的结果用“四舍五入”到所期望的十进制位数,就会得到期望的最终结果。

对于需要非常精确的情况,可以使用 decimal 模块,它实现的十进制运算适合会计方面的应用和高精度要求的应用。另外 fractions 模块支持另外一种形式的运算,它实现的运算基于有理数(因此像 1/3 这样的数字可以精确地表示)。最高要求则可是使用由 SciPy 提供的 Numerical Python 包和其它用于数学和统计学的包。列出这些东西,仅仅是让看官能明白,解决问题的方式很多,后面会用这些中的某些方式解决上述问题。

关于无限循环小数问题,我有一个链接推荐给诸位,它不是想象的那么简单呀。请阅读:维基百科的词条:0.999...,会不会有深入体会呢?

补充一个资料,供有兴趣的朋友阅读:浮点数算法:争议和限制
Python 总会要提供多种解决问题的方案的,这是她的风格。

引用模块解决除法--启用轮子
Python 之所以受人欢迎,一个很重重要的原因,就是轮子多。这是比喻啦。就好比你要跑的快,怎么办?光天天练习跑步是不行滴,要用轮子。找辆自行车,就快了很多。还嫌不够快,再换电瓶车,再换汽车,再换高铁...反正你可以选择的很多。但是,这些让你跑的快的东西,多数不是你自己造的,是别人造好了,你来用。甚至两条腿也是感谢父母恩赐。正是因为轮子多,可以选择的多,就可以以各种不同速度享受了。

轮子是人类伟大的发明。

Python 就是这样,有各种轮子,我们只需要用。只不过那些轮子在 Python 里面的名字不叫自行车、汽车,叫做“模块”,有人承接别的语言的名称,叫做“类库”、“类”。不管叫什么名字吧。就是别人造好的东西我们拿过来使用。

怎么用?可以通过两种形式用:

形式 1:import module-name。import 后面跟空格,然后是模块名称,例如:import os
形式 2:from module1 import module11。module1 是一个大模块,里面还有子模块 module11,只想用 module11,就这么写了。
不啰嗦了,实验一个:


>>> from future import pision
>>> 5 / 2
2.5
>>> 9 / 2
4.5
>>> 9.0 / 2
4.5
>>> 9 / 2.0
4.5
Copy after login


注意了,引用了一个模块之后,再做除法,就不管什么情况,都是得到浮点数的结果了。

这就是轮子的力量。

余数
前面计算 5/2 的时候,商是 2,余数是 1

余数怎么得到?在 Python 中(其实大多数语言也都是),用%符号来取得两个数相除的余数.

实验下面的操作:


>>> 5 % 2
1
>>> 6%4
2
>>> 5.0%2
1.0
Copy after login


符号:%,就是要得到两个数(可以是整数,也可以是浮点数)相除的余数。

前面说 Python 有很多人见人爱的轮子(模块),她还有丰富的内建函数,也会帮我们做不少事情。例如函数 pmod()


>>> pmod(5,2) # 表示 5 除以 2,返回了商和余数
(2, 1)
>>> pmod(9,2)
(4, 1)
>>> pmod(5.0,2)
(2.0, 1.0)
Copy after login


四舍五入
最后一个了,一定要坚持,今天的确有点啰嗦了。要实现四舍五入,很简单,就是内建函数:round()

动手试试:


>>> round(1.234567,2)
1.23
>>> round(1.234567,3)
1.235
>>> round(10.0/3,4)
3.3333
Copy after login



The above is the detailed content of Basic mathematical calculation methods in Python programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template