sync from local backup
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
# 函数与模块
|
||||
|
||||
## 学习目标
|
||||
- 理解函数的作用
|
||||
- 掌握函数的定义和调用
|
||||
- 了解模块的使用
|
||||
|
||||
## 定义函数
|
||||
|
||||
```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 模块进行数学计算
|
||||
Reference in New Issue
Block a user