# 功能¶

## 引言¶

```print('Hello, {name}!')
```

```greet('{name}')
```

```print('Hello, World!')
```

## 函数的组件¶

### 名字¶

```>>> print(print)
<built-in function print>
```

### 位置论元¶

`float()` 是一个函数，用于将表示数字的数字或字符串转换为浮点数。其接口定义为

`float`(x)

```>>> float(1)
1.0
>>> float(-2)
-2.0
```

```>>> float('1')
1.0
>>> float('-2')
-2.0
>>> float('1.500')
1.5
>>> float('1e-2')
0.01
>>> float('+1E6')
1000000.0
```

### 关键字参数¶

`int()` 是一个函数，用于将表示数字的数字或字符串转换为整数。其接口定义为

`int`(x, base=10)

```>>> int(1.0)
1
>>> int(-2.0)
-2
>>> int(1.3)
1
>>> int(1.8)
1
>>> int(-1.3)
-1
>>> int(-1.8)
-1
```

```>>> int('1')
1
>>> int('-2')
-2
>>> int('101010')
101010
>>> int('101010', base=2)
42
```

```>>> int('101010', 2)
42
```

```>>> int('1.0')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.0'
```

```>>> int(float('1.0'))
1
```

## 定义函数¶

```>>> def greet(name):
...     print('Hello', name)
...
>>> greet('World')
Hello World
>>> greet('you')
Hello you
```
```>>> def add_reciprocal(a, b):
...     return 1/a + 1/b
0.375
```

```>>> def name_and_favorite_food(name, favorite_food='pizza'):
...     return name + "'s favorite food is " + favorite_food + '.'
...
>>> name_and_favorite_food('Dominik')
"Dominik's favorite food is pizza."
>>> name_and_favorite_food('Stefan', 'kimchi')
"Stefan's favorite food is kimchi."
```

```>>> def is_positive_without_return(x):
...     if x >= 0:
...         print(True)
...     else:
...         print(False)
...
>>> is_positive_without_return(1)
True
>>> is_positive_without_return(-1)
False
>>> a = is_positive_without_return(1)
True
>>> print(a)
None
```

```>>> def is_positive_without_return(x):
...     if x >= 0:
...         return True
...     else:
...         return False
...
>>> is_positive_without_return(1)
True
>>> is_positive_without_return(-1)
False
>>> a = is_positive_without_return(1)
>>> print(a)
True
```

## 函数作为函数参数¶

```def format_heading(text):
return '\n' + text + '\n' + '='*len(text) + '\n'

# Assume that `sections` is a list of dictionaries with the keys
# ``heading`` for the heading text and ``content`` for the content
# of the section.
# Assume that the `header_formatter` is a function that takes a string
# as argument and formats it in a way that is befitting for a heading.
text = ''
for section in sections:
content = section['content']
text += format_heading(heading) + content + '\n'
# Before returning it we make shure that all surrounding whitespace is
# gone.
return text.strip()

secs = [
{
'content': 'In this section we introduce some smart method to teach Python.'
},
{
'content': 'More than 42 % of participants in this study learned Python.'
}
]

print(pretty_text)
```

```Introduction
============
In this section we introduce some smart method to teach Python.

Results
=======
More than 42 % of participants in this study learned Python.
```

## 摘要¶

• 通过简化重复任务或为复杂的编程逻辑命名，函数可以使您的工作变得更轻松。

• 函数可以有两种不同类型的参数， 位置论元 必须提供给函数的 关键字参数 它们提供默认值。

• 函数是使用 `def` 关键字。

• 函数可以通过使用 `return` 关键字。

## 练习¶

1

Lambdas 是规则的例外，因为它们定义匿名函数。