Files
chunyu_project/media/learn/courses/12/chapters/70.md
T
2026-08-05 23:59:15 +08:00

46 lines
711 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 函数与模块
## 学习目标
- 理解函数的作用
- 掌握函数的定义和调用
- 了解模块的使用
## 定义函数
```python
def greet(name):
'''打招呼函数'''
return f"你好,{name}!"
# 调用函数
message = greet("小明")
print(message) # 输出:你好,小明!
```
## 参数类型
```python
# 默认参数
def power(base, exponent=2):
return base ** exponent
print(power(3)) # 9
print(power(2, 3)) # 8
```
## 导入模块
```python
import math
print(math.sqrt(16)) # 4.0
from random import randint
print(randint(1, 100))
```
## 动手练习
1. 编写计算圆面积的函数
2. 创建判断素数的函数
3. 使用 math 模块进行数学计算