导图社区 python 第一课
obs.python.org Success Stories More Python and its broad variety of libraries are very well suited to develop customized machine learning tools which tackle the complex challenges p...
编辑于2022-11-28 19:54:47 重庆python 1.2
expression(表达)
>>> 42 42
>>> -1 - -1 0
>>> 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128 0.9921875
Call Expressions(调用表达式)
>>> max(7.5, 9.5)
9.5
The order of the arguments in a call expression matters(在调用表达式中参数的顺序很重要)
>>> pow(100, 2)
10000
>>> pow(2, 100)
1267650600228229401496703205376
functions may take an arbitrary number of arguments:(函数可以采用任意数量的参数)
>>> max(1, -2, 3, -4)
3
可以轻松的进行嵌套不会产生歧义
>>> max(min(1, -2), min(pow(3, 5), -4))
-2
Importing Library Functions(调用库函数)
python中定义了大量的函数但在默认环境下都不可用,他们共同组成了python 库 ,在使用时需要从python库中去调用他们
>>> from math import sqrt
>>> sqrt(256)
16.0
the operator module provides access to functions corresponding to infix operators(){operater 模块提供了一个对于中缀运算符的操作方法}
中缀表达式就是常见的运算表达式,如(3+4)×5-6
>>> from operator import add, sub, mul
>>> add(14, 28)
42
>>> sub(100, mul(7, add(8, 4)))
16
Names and the Environment
we can establish new bindings using the assignment statement, which contains a name to the left of = and a value to the right
>>> radius = 10
>>> radius
10
>>> 2 * radius
20
Names are also bound via import statements.
>>> from math import pi
>>> pi * 71 / 223
1.0002380197528042
We can use assignment statements to give new names to existing functions.(我们可以通过赋值语句去使一个已经存在的函数绑定到一个名称)
>>> f = max
>>> f
<built-in function max>
>>> f(2, 3, 4)
4
successive assignment statements can rebind a name to a new value. (连续的赋值语句可以将名称绑定到一个新值)
>>> f = 2
>>> f
2
One can even bind built-in names to new values. (甚至可以将内置名称绑定到一个新值)
After assigning max to 5, the name max is no longer bound to a function, and so attempting to call max(2, 3, 4) will cause an error. (在绑定max到5后,max不再绑定到函数,因此尝试调用max函数会导致错误)
>>> max = 5
>>> max
5
When executing an assignment statement, Python evaluates the expression to the right of = before changing the binding to the name on the left. Therefore, one can refer to a name in right-side expression, even if it is the name to be bound by the assignment statement. (当执行赋值语句时,Python在将绑定更改为左侧的名称之前,会对=右侧的表达式求值。因此,可以引用右侧表达式中的名称,即使它是要由赋值语句绑定的名称。)
>>> x = 2
>>> x = x + 1
>>> x
3
We can also assign multiple values to multiple names in a single statement, where names on the left of = and expressions on the right of = are separated by commas. (我们也可以在一条语句中分配多个值给多个名称,在等号的左边和右边用逗号进行分割)
>>> area, circumference = pi * radius * radius, 2 * pi * radius
>>> area
314.1592653589793
>>> circumference
62.83185307179586
Changing the value of one name does not affect other names (更改一个名称的值不会影响到其他名称)
>>> radius = 11
>>> area
314.1592653589793
>>> area = pi * radius * radius
380.132711084365
With multiple assignment, all expressions to the right of = are evaluated before any names to the left are bound to those values. (对于多重赋值,在左侧的任何名称绑定到这些值之前将会对等式右边的所有表达式进行求值)
As a result of this rule, swapping the values bound to two names can be performed in a single statement. (通过这个规则,在一个语句中交换两个名称的值是可以执行的)
>>> x, y = 3, 4.5
>>> y, x = x, y
>>> x
4.5
>>> y
3
Evaluating Nested Expressions(计算嵌套表达式)
The first step dictates that in order to accomplish the evaluation process for a call expression we must first evaluate other expressions. (第一步规定,为了完成调用表达式的求值过程,我们必须首先对其他表达式求值)
Thus, the evaluation procedure is recursive in nature (因此,评估程序本质上是递归的)
标注
>>> sub(pow(2, add(1, 10)), pow(2, 5))
2016
The Non-Pure Print Function
We can see in the non-pure function example above that print does not return a useful result when used in an operand expression. On the other hand, we have seen that functions such as max, pow and sqrt can be used effectively in nested expressions. 我们能看到当用于操作数表达式中非纯函数的上述例子中print不能返回有效的结果,另一方面,我们能看到诸如max,pow,and sqrt 函数能够有效的用于循环嵌套表达式中
pure functions tend to be simpler to test. A list of arguments will always lead to the same return value, which can be compared to the expected return value. 纯函数往往更容易测试,参数列表往往更容易导出相同的值,这将会使得我们能够更好的和预期的返回值进行比较
Pure functions(纯函数)
Functions have some input (their arguments) and return some output (the result of applying them). 函数有一些输入(对应的参数)然后返回一些输出(对应输入的结果)
>>> abs(-2)
2
The built-in function can be depicted as a small machine that takes input and produces output. (内置函数可以描述为一种接受输入产出输出的小型机器)
Non-pure functions(非纯函数)
In addition to returning a value, applying a non-pure function can generate side effects, which make some change to the state of the interpreter or computer (除了返回值之外,应用非纯函数还会产生副作用,从而对解释器或计算机的状态进行一些更改。)
A common side effect is to generate additional output beyond the return value, using the print function. (一个常见的副作用是使用print函数生成超出返回值的附加输出。)
>>> print(1, 2, 3)
1 2 3
The value that print returns is always None, a special Python value that represents nothing. The interactive Python interpreter does not automatically print the value None. In the case of print, the function itself is printing output as a side effect of being called. (打印返回的值始终为None,这是一个不表示任何内容的特殊Python值。交互式Python解释器不会自动打印值None。在打印的情况下,函数本身是打印输出,作为调用的副作用。)
>>> print(print(1), print(2))
1
2
None None
>>> two = print(2)
2
>>> print(two)
None