- if-elif-else
- for
- while
- function
- Function definition
- Empty function pass
- Return multiple values
- variable parameter *
- Keyword arguments **
Control statements
if - elif - else
For example, enter the user's age, print different content based on age, in a Python program, with the if
statement to achieve:
1 |
age = 20 |
Python indentation according to the rules, if if
the statement judge True
, put the two lines indented print statement is executed, otherwise, do nothing.
Also to if
add a else
sentence, meaning that if the if
judgment is False
, do not perform if
the content, to the else
implementation of:
1 |
age = 3 |
Be careful not to write less colon :
.
Of course, the above determination is very rough, you can use elif
to make a more detailed determination:
1 |
age = 3 |
elif
Is the else if
abbreviation can have more elif
, so if
the full form of the statement is:
1 |
if <condition judgment 1>: |
if
There is a characteristic statement is executed, it is down from the judge, if a judge is on True
, the corresponding statement after the judgment executed, ignore the rest elif
and else
, so, please explain why the test and the following program print It is teenager
:
1 |
age = 20 |
if
Judging condition can also be abbreviated, such as writing:
1 |
if x: |
As long as x
a non-zero value, non-empty string, other non-empty list, it is determined True
otherwise False
.
cycle
Python loop, there are two, one is for ... in loop, followed by the list or tuple each element iterative out, look at an example:
1 |
names = ['Michael', 'Bob', 'Tracy'] |
The implementation of this code, in turn prints names
of each element:
1 |
Michael |
So for x in ...
cycle each element is assigned to the variable x
, and then execute the statement block indentation.
The second loop is the while loop, as long as the conditions are met, continuous cycle, the loop exits when the condition is not satisfied. For example, we want to calculate the sum of all odd-numbered less than 100, can be achieved using a while loop:
1 |
sum = 0 |
The cycle variables n
continue to decrement, until it was -1
time, no longer satisfied while condition loop exits.
function
Function definition
In Python, to be used to define a function def
statement sequentially write the function name, parentheses, brackets and colon parameters :
, then, write the function block body is in the retracted, with the return value of the function return
return statement.
Empty function pass
If you want a definition of doing nothing is an empty function, you can use pass
the statement:
1 |
def nop(): |
pass
语句什么都不做,那有什么用?实际上pass
可以用来作为占位符,比如现在还没想好怎么写函数的代码,就可以先放一个pass
,让代码能运行起来。
pass
还可以用在其他语句里,比如:
1 |
if age >= 18: |
缺少了pass
,代码运行就会有语法错误。
返回多个值
函数可以返回多个值吗?答案是肯定的。
比如在游戏中经常需要从一个点移动到另一个点,给出坐标、位移和角度,就可以计算出新的新的坐标:
1 |
import math |
但其实这只是一种假象,Python函数返回的仍然是单一值:
1 |
>>> r = move(100, 100, 60, math.pi / 6) |
原 大专栏 Python-控制语句及函数来返回值是一个tuple!但是,在语法上,返回一个tuple可以省略括号,而多个变量可以同时接收一个tuple,按位置赋给对应的值,所以,Python的函数返回多值其实就是返回一个tuple,但写起来更方便。
默认参数
1 |
def power(x, n=2): |
则,我们调用power(5), 相当于调用 power(5, 2)。
1 |
>>> power(5) |
设置默认参数时,有几点要注意:
一是必选参数在前,默认参数在后,否则Python的解释器会报错(思考一下为什么默认参数不能放在必选参数前面)
二是如何设置默认参数。
当函数有多个参数时,把变化大的参数放前面,变化小的参数放后面。变化小的参数就可以作为默认参数。
可变参数 *
要定义出这个函数,我们必须确定输入的参数。由于参数个数不确定,我们首先想到可以把a,b,c……作为一个list或tuple传进来,这样,函数可以定义如下:
1 |
def calc(numbers): |
但是调用的时候,需要先组装出一个list或tuple:
1 |
>>> calc([1, 2, 3]) |
如果利用可变参数,调用函数的方式可以简化成这样:
1 |
>>> calc(1, 2, 3) |
所以,我们把函数的参数改为可变参数:
1 |
def calc(*numbers): // 函数定义修改为 *numbers |
定义可变参数和定义一个list或tuple参数相比,仅仅在参数前面加了一个*
号。在函数内部,参数numbers
接收到的是一个tuple,因此,函数代码完全不变。但是,调用该函数时,可以传入任意个参数,包括0个参数:
1 |
>>> calc(1, 2) |
如果已经有一个list或者tuple,要调用一个可变参数怎么办?可以这样做:
1 |
>>> nums = [1, 2, 3] |
这种写法当然是可行的,问题是太繁琐,所以Python允许你在list或tuple前面加一个*
号,把list或tuple的元素变成可变参数传进去:
1 |
>>> nums = [1, 2, 3] |
*nums
表示把nums
这个list的所有元素作为可变参数传进去。这种写法相当有用,而且很常见。
关键字参数 **
可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。请看示例:
1 |
def person(name, age, **kw): |
函数person
除了必选参数name
和age
外,还接受关键字参数kw
。在调用该函数时,可以只传入必选参数:
1 |
>>> person('Michael', 30) |
也可以传入任意个数的关键字参数:
1 |
>>> person('Bob', 35, city='Beijing') |
应用:
1 |
def test(name, age, **kw): |
或者:
1 |
def person(name, age, **kw): |