GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — math function · GiTechLab/StarterLearningPython@57c4321

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 104.md
  • index.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎104.md‎

Copy file name to clipboard
+165Lines changed: 165 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
>He has told you, O mortal, what is good;
2+
>and what does the LORD require of you
3+
>but to do justice, and to love kindness,
4+
>and to walk humbly with your God?(MICAH 6:8)
5+
>世人哪,耶和华已指示你何为善,
6+
>他向你所要的是什么呢?
7+
>只要你行公义,好怜悯,
8+
>存谦卑的心,与你的神同行。
9+
10+
#常用数学函数和运算优先级
11+
12+
在数学之中,除了加减乘除四则运算之外——这是小学数学——还有其它更多的运算,比如乘方、开方、对数运算等等,要实现这些运算,需要用到python中的一个模块:Math
13+
14+
>模块(module)是python中非常重要的东西,你可以把它理解为python的扩展工具。换言之,python默认情况下提供了一些可用的东西,但是这些默认情况下提供的还远远不能满足编程实践的需要,于是就有人专门制作了另外一些工具。这些工具被称之为“模块”
15+
>任何一个pythoner都可以编写模块,并且把这些模块放到网上供他人来使用。
16+
>当安装好python之后,就有一些模块默认安装了,这个称之为“标准库”,“标准库”中的模块不需要安装,就可以直接使用。
17+
>如果没有纳入标准库的模块,需要安装之后才能使用。模块的安装方法,我特别推荐使用pip来安装。这里仅仅提一下,后面会专门进行讲述,性急的看官可以自己google。
18+
19+
##使用math模块
20+
21+
math模块是标准库中的,所以不用安装,可以直接使用。使用方法是:
22+
23+
>>> import math
24+
25+
用import就将math模块引用过来了,下面就可以使用这个模块提供的工具了。比如,要得到圆周率:
26+
27+
>>> math.pi
28+
3.141592653589793
29+
30+
这个模块都能做哪些事情呢?可以用下面的方法看到:
31+
32+
>>> dir(math)
33+
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
34+
35+
`dir(module)`是一个非常有用的指令,可以通过它查看任何模块中所包含的工具。从上面的列表中就可以看出,在math模块中,可以计算正sin(a),cos(a),sqrt(a)......
36+
37+
这些我们称之为函数,也就是在模块math中提供了各类计算的函数,比如计算乘方,可以使用pow函数。但是,怎么用呢?
38+
39+
python是一个非常周到的姑娘,她早就提供了一个命令,让我们来查看每个函数的使用方法。
40+
41+
>>> help(math.pow)
42+
43+
在交互模式下输入上面的指令,然后回车,看到下面的信息:
44+
45+
Help on built-in function pow in module math:
46+
47+
pow(...)
48+
pow(x, y)
49+
50+
Return x**y (x to the power of y).
51+
52+
这里展示了math模块中的pow函数的使用方法和相关说明。
53+
54+
1. 第一行意思是说这里是math模块的内建函数pow帮助信息(所谓built-in,称之为内建函数,是说这个函数是python默认就有的)
55+
2. 第三行,表示这个函数的参数,有两个,也是函数的调用方式
56+
3. 第四行,是对函数的说明,返回`x**y`的结果,并且在后面解释了`x**y`的含义。
57+
4. 最后,按q键返回到python交互模式
58+
59+
从上面看到了一个额外的信息,就是pow函数和`x**y`是等效的,都是计算x的y次方。
60+
61+
>>> 4**2
62+
16
63+
>>> math.pow(4,2)
64+
16.0
65+
>>> 4*2
66+
8
67+
68+
特别注意,`4**2`和`4*2`是有很大区别的。
69+
70+
用类似的方法,可以查看math模块中的任何一个函数的使用方法。
71+
72+
>关于“函数”的问题,在这里不做深入阐述,看管姑且按照自己在数学中所学到去理解。后面会有专门研究函数的章节。
73+
74+
下面是几个常用的math模块中函数举例,看官可以结合自己调试的进行比照。
75+
76+
>>> math.sqrt(9)
77+
3.0
78+
>>> math.floor(3.14)
79+
3.0
80+
>>> math.floor(3.92)
81+
3.0
82+
>>> math.fabs(-2) #等价于abs(-2)
83+
2.0
84+
>>> abs(-2)
85+
2
86+
>>> math.fmod(5,3) #等价于5%3
87+
2.0
88+
>>> 5%3
89+
2
90+
91+
##几个常见函数
92+
93+
有几个常用的函数,列一下,如果记不住也不要紧,知道有这些就好了,用的时候就google。
94+
95+
**求绝对值**
96+
97+
>>> abs(10)
98+
10
99+
>>> abs(-10)
100+
10
101+
>>> abs(-1.2)
102+
1.2
103+
104+
**四舍五入**
105+
106+
>>> round(1.234)
107+
1.0
108+
>>> round(1.234,2)
109+
1.23
110+
111+
>>> #如果不清楚这个函数的用法,可以使用下面方法看帮助信息
112+
>>> help(round)
113+
114+
Help on built-in function round in module __builtin__:
115+
116+
round(...)
117+
round(number[, ndigits]) -> floating point number
118+
119+
Round a number to a given precision in decimal digits (default 0 digits).
120+
This always returns a floating point number. Precision may be negative.
121+
122+
123+
##运算优先级
124+
125+
从小学数学开始,就研究运算优先级的问题,比如四则运算中“先乘除,后加减”,说明乘法、除法的优先级要高于加减。
126+
127+
对于同一级别的,就按照“从左到右”的顺序进行计算。
128+
129+
下面的表格中列出了python中的各种运算的优先级顺序。不过,就一般情况而言,不需要记忆,完全可以按照数学中的去理解,因为人类既然已经发明了数学,在计算机中进行的运算就不需要从新编写一套新规范了,只需要符合数学中的即可。
130+
131+
|运算符|描述|
132+
|------|----|
133+
|lambda|Lambda表达式|
134+
|or|布尔“或”|
135+
|and|布尔“与”|
136+
|not x|布尔“非”|
137+
|in,not in|成员测试|
138+
|is,is not|同一性测试|
139+
|<,<=,>,>=,!=,==|比较|
140+
|\||按位或|
141+
|^|按位异或|
142+
|&|按位与|
143+
|<<,>>|移位|
144+
|+,-|加法与减法|
145+
|*,/,%|乘法、除法与取余|
146+
|+x,-x|正负号|
147+
|~x|按位翻转|
148+
|**|指数|
149+
|x.attribute|属性参考|
150+
|x[index]|下标|
151+
|x[index:index]|寻址段|
152+
|f(arguments...)|函数调用|
153+
|(experession,...)|绑定或元组显示|
154+
|[expression,...]|列表显示|
155+
|{key:datum,...}|字典显示|
156+
|'expression,...'|字符串转换|
157+
158+
上面的表格将python中用到的与运算符有关的都列出来了,是按照**从低到高**的顺序列出的。虽然有很多还不知道是怎么回事,不过先列出来,等以后用到了,还可以回来查看。
159+
160+
最后,要提及的是运算中的绝杀:括号。只要有括号,就先计算括号里面的。这是数学中的共识,无需解释。
161+
162+
------
163+
164+
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:除法](./103.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:写一个简单的程序](./105.md)
165+
Collapse file

‎index.md‎

Copy file name to clipboardExpand all lines: index.md
+3-1Lines changed: 3 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
1. [集成开发环境](./101.md)
1616
2. [数和四则运算](./102.md)
17-
2. [除法](./103.md)
17+
3. [除法](./103.md)
18+
4. [常用数学函数和运算优先级](./104.md)
19+
5. [写一个简单程序]()
1820
3. 与运算有关的函数
1921
3. 字符串
2022
4. 输出格式

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.