Python的语法和(以及一些模块介绍)

版权声明:如需转载或引用,请注明出处。 https://blog.csdn.net/weixin_39278265/article/details/83006357

前言

本文旨在介绍python的语法 以及 python的一些模块。

1. python的math模块

参考 https://docs.python.org/3.6/library/math.html :(这里是python3.6版本)

1.1 Trigonometric functions

trigonometric

英 [ˌtrɪɡənə’metrɪk] 美 [ˌtrɪɡənə’metrɪk]
adj. 三角法的,据三角法的
adj. of or relating to or according to the principles of trigonometry
trigonometric function

math.tan(x)
Return the tangent of x radians.

radian

noun (geometry 几何) 弧度
a unit used to measure an angle, equal to the angle at the centre of a circle whose arc is the same length as the circle’s radius

math.cos(x)
Return the cosine of x radians.

math.acos(x)
Return the arc cosine of x, in radians.

math.asin(x)
Return the arc sine of x, in radians.

math.atan(x)
Return the arc tangent of x, in radians.

arc cosine: 反余弦 [1]

(arccosine,arccos,cos-1)是一种反三角函数,也是高等数学中的一种基本特殊函数。

1.2 Angular conversion 角度和弧度的转换

Angular

having angles or an angular shape
measured by an angle or by the rate of change of an angle
angular momentum 角动量

math.degrees(x)
Convert angle x from radians to degrees.

math.radians(x)
Convert angle x from degrees to radians.

1.3 Power and logarithmic functions

logarithmic

英 [ˌlɒɡə’rɪðmɪk] 美 [ˌlɒɡə’rɪðmɪk]
adj. 对数的
of or relating to or using logarithms
logarithmic function

math.exp(x)
Return e**x.

math.log10(x)
Return the base-10 logarithm of x. This is usually more accurate than log(x, 10). 注意,base是10

math.log1p(x)
Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.
注意,base是e,里面的参数是1+x

math.log2(x)
Return the base-2 logarithm of x. This is usually more accurate than log(x, 2).

math.sqrt(x)
Return the square root of x.

math.pow(x, y)

Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.


Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.

值得注意的是,pow中的参数全部转化成了float。
所以,pow(2,2)会输出4.0
2**2输出4。很神奇的

因为**(乘方)的优先级比*(乘法)高,所以乘方加不加括号都行。

比如:22**2 = 2(2**2) = 8 (不等于16)

1.4 Number-theoretic and representation functions

math.ceil(x)
Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.ceil(), which should return an Integral value.

math.fsum(iterable)
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums:

sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0

1.5 cmath — Mathematical functions for complex numbers

cmath是python专门对复数进行操作的模块。[3]

2. python的import

1)from math import tan, cos, pi

2)import math
适用于大项目,在代码中写math.xxx 能够清晰知道函数来自哪个模块。

3. python的格式化输出

参考 https://www.cnblogs.com/alfred0311/p/7735539.html

目前能用到的还不多,大概是%d %f %.2f 这样的。

4. python的版本

在看到[5]的时候,我发现各个版本的标识不一样,如下图左侧:

在这里插入图片描述

参考 https://baike.baidu.com/item/RC/7311964?fr=aladdin

RC=Release Candidate,含义是"发布候选版",它不是最终的版本,而是最终版(RTM=Release To Manufacture)之前的最后一个版本。广义上对测试有三个传统的称呼:alpha、beta、gamma,用来标识测试的阶段和范围。alpha 是指内测,即现在说的CB,指开发团队内部测试的版本或者有限用户体验测试版本。beta 是指公测,即针对所有用户公开的测试版本。然后做过一些修改,成为正式发布的候选版本时叫做gamma,现在叫做RC(Release Candidate)。

5. python循环之前需要初始化

这个还是需要记住的,如果没有初始化是很容易出错的。

参考文献

[1] 反余弦. https://baike.baidu.com/item/反余弦/10808622?fr=aladdin
[2] math — Mathematical functions. https://docs.python.org/3.6/library/math.html
[3] cmath — Mathematical functions for complex numbers. https://docs.python.org/3.6/library/cmath.html#module-cmath
[4] Python 3.6 模块学习math库常见函数. https://blog.csdn.net/qq_38092017/article/details/76216137
[5] Python 3.7.1rc1 documentation. https://docs.python.org/3.7/index.html
[6] RC. https://baike.baidu.com/item/RC/7311964?fr=aladdin
[7] Python3 格式化输出 %s & %d 等. https://www.cnblogs.com/alfred0311/p/7735539.html

猜你喜欢

转载自blog.csdn.net/weixin_39278265/article/details/83006357