Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
WuShichao committed Oct 5, 2018
1 parent 374e6cd commit 1534057
Showing 1 changed file with 140 additions and 27 deletions.
167 changes: 140 additions & 27 deletions 08.functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@

案例(保存为 `function1.py`):

```text
{% include "./programs/function1.py" %}
```python
def say_hello():
# 该块属于这一函数
print('hello world')
# 函数结束

say_hello() # 调用函数
say_hello() # 再次调用函数
```

输出:

```text
{% include "./programs/function1.txt" %}
$ python function1.py
hello world
hello world
```

**它是如何工作的**
Expand All @@ -32,14 +40,31 @@

案例(保存为 `function_param.py`):

```text
{% include "./programs/function_param.py" %}
```python
def print_max(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')

# 直接传递字面值
print_max(3, 4)

x = 5
y = 7

# 以参数的形式传递变量
print_max(x, y)
```

输出:

```text
{% include "./programs/function_param.txt" %}
$ python function_param.py
4 is maximum
7 is maximum
```

**它是如何工作的**
Expand All @@ -54,14 +79,27 @@

案例(保存为 `function_local.py`):

```text
{% include "./programs/function_local.py" %}
```python
x = 50


def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)


func(x)
print('x is still', x)
```

输出:

```text
{% include "./programs/function_local.txt" %}
$ python function_local.py
x is 50
Changed local x to 2
x is still 50
```

**它是如何工作的**
Expand All @@ -80,14 +118,29 @@

案例(保存为 `function_global.py`):

```text
{% include "./programs/function_global.py" %}
```python
x = 50


def func():
global x

print('x is', x)
x = 2
print('Changed global x to', x)


func()
print('Value of x is', x)
```

输出:

```text
{% include "./programs/function_global.txt" %}
$ python function_global.py
x is 50
Changed global x to 2
Value of x is 2
```

**它是如何工作的**
Expand All @@ -104,14 +157,20 @@

案例(保存为 `function_default.py`):

```text
{% include "./programs/function_default.py" %}
```python
def say(message, times=1):
print(message * times)

say('Hello')
say('World', 5)
```

输出:

```text
{% include "./programs/function_default.txt" %}
$ python function_default.py
Hello
WorldWorldWorldWorldWorld
```

**它是如何工作的**
Expand All @@ -134,14 +193,22 @@

案例(保存为 `function_keyword.py`):

```text
{% include "./programs/function_keyword.py" %}
```python
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)

func(3, 7)
func(25, c=24)
func(c=50, a=100)
```

输出:

```text
{% include "./programs/function_keyword.txt" %}
$ python function_keyword.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
```

**它是如何工作的**
Expand All @@ -158,14 +225,33 @@

有时你可能想定义的函数里面能够有_任意_数量的变量,也就是参数数量是可变的,这可以通过使用星号来实现(将下方案例保存为 `function_varargs.py`):

```text
{% include "./programs/function_varargs.py" %}
```python
def total(a=5, *numbers, **phonebook):
print('a', a)

#遍历元组中的所有项目
for single_item in numbers:
print('single_item', single_item)

#遍历字典中的所有项目
for first_part, second_part in phonebook.items():
print(first_part,second_part)

print(total(10,1,2,3,Jack=1123,John=2231,Inge=1560))
```

输出:

```text
{% include "./programs/function_varargs.txt" %}
$ python function_varargs.py
a 10
single_item 1
single_item 2
single_item 3
Inge 1560
John 2231
Jack 1123
None
```

**它是如何工作的**
Expand All @@ -182,14 +268,23 @@

案例(保存为 `function_return.py`):

```text
{% include "./programs/function_return.py" %}
```python
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y

print(maximum(2, 3))
```

输出:

```text
{% include "./programs/function_return.txt" %}
$ python function_return.py
3
```

_它是如何工作的_
Expand All @@ -215,14 +310,32 @@ Python 有一个甚是优美的功能称作_文档字符串(Documentation Stri

案例(保存为 `function_docstring.py`):

```text
{% include "./programs/function_docstring.py" %}
```python
def print_max(x, y):
'''打印两个数值中的最大数。
这两个数都应该是整数'''
# 如果可能,将其转换至整数类型
x = int(x)
y = int(y)

if x > y:
print(x, 'is maximum')
else:
print(y, 'is maximum')

print_max(3, 5)
print(print_max.__doc__)
```

输出:

```text
{% include "./programs/function_docstring.txt" %}
$ python function_docstring.py
5 is maximum
打印两个数值中的最大数。
这两个数都应该是整数
```

**它是如何工作的**
Expand Down

0 comments on commit 1534057

Please sign in to comment.