Python编程中的布尔运算符、统计指标与函数定义
立即解锁
发布时间: 2025-08-29 10:27:49 阅读量: 15 订阅数: 25 AIGC 

# Python 编程中的布尔运算符、统计指标与函数定义
## 1. 布尔运算符
### 1.1 基本概念
在 Python 中,条件运算符(如 `>`、`<`、`>=`、`<=`、`==` 和 `!=`)可用于形成简单条件,如 `grade >= 60`。为了形成更复杂的条件,可以使用布尔运算符 `and`、`or` 和 `not`。
### 1.2 `and` 运算符
`and` 运算符用于确保在执行控制语句的套件之前,两个条件都为 `True`。以下是一个示例代码:
```python
gender = 'Female'
age = 70
if gender == 'Female' and age >= 65:
print('Senior female')
```
上述代码中,`if` 语句有两个简单条件:
- `gender == 'Female'` 用于判断一个人是否为女性。
- `age >= 65` 用于判断这个人是否为老年人。
`and` 运算符的真值表如下:
| expression1 | expression2 | expression1 and expression2 |
| --- | --- | --- |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
### 1.3 `or` 运算符
`or` 运算符用于测试两个条件中是否有一个或两个都为 `True`。以下是一个示例代码:
```python
semester_average = 83
final_exam = 95
if semester_average >= 90 or final_exam >= 90:
print('Student gets an A')
```
上述代码中,`if` 语句有两个简单条件:
- `semester_average >= 90` 用于判断学生本学期的平均成绩是否为 A(90 分及以上)。
- `final_exam >= 90` 用于判断学生的期末考试成绩是否为 A。
`or` 运算符的真值表如下:
| expression1 | expression2 | expression1 or expression2 |
| --- | --- | --- |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
### 1.4 短路求值
Python 在知道整个条件为 `False` 时,会立即停止对 `and` 表达式的求值;在知道整个条件为 `True` 时,会立即停止对 `or` 表达式的求值,这称为短路求值。例如:
```python
gender = 'Male'
age = 70
if gender == 'Female' and age >= 65:
print('Senior female')
```
在上述代码中,由于 `gender` 不等于 `'Female'`,整个表达式必然为 `False`,因此 Python 会立即停止求值。
### 1.5 `not` 运算符
`not` 运算符用于反转条件的含义,即 `True` 变为 `False`,`False` 变为 `True`。以下是一个示例代码:
```python
grade = 87
if not grade == -1:
print('The next grade is', grade)
```
`not` 运算符的真值表如下:
| expression | not expression |
| --- | --- |
| False | True |
| True | False |
### 1.6 运算符优先级
运算符的优先级和分组从高到低依次为:
| 运算符 | 分组 |
| --- | --- |
| () | 从左到右 |
| ** | 从右到左 |
| * / // % | 从左到右 |
| + - | 从左到右 |
| < <= > >= == != | 从左到右 |
| not | 从左到右 |
| and | 从左到右 |
| or | 从左到右 |
## 2. 集中趋势的度量
### 2.1 基本概念
在数据分析中,集中趋势的度量包括均值(mean)、中位数(median)和众数(mode)。
- 均值:一组值的平均值。
- 中位数:将所有值按升序排列后,位于中间的数值。
- 众数:出现频率最高的数值。
### 2.2 手动计算均值
以下是一个手动计算均值的示例代码:
```python
grades = [85, 93, 45, 89, 85]
mean = sum(grades) / len(grades)
print(mean)
```
### 2.3 使用 `statistics` 模块计算
Python 标准库的 `statistics` 模块提供了计算均值、中位数和众数的函数。以下是一个示例代码:
```python
import statistics
grades = [85, 93, 45, 89, 85]
mean = statistics.mean(grades)
median = statistics.median(grades)
mode = statistics.mode(grades)
print('Mean:', mean)
print('Median:', median)
print('Mode:', mode)
```
需要注意的是,`mode` 函数在列表中有两个或多个“最频繁”的值时会引发 `StatisticsError`。例如,对于列表 `[85, 93, 45, 89, 85, 93]`,该列表是双峰的,因为 85 和 93 都出现了两次。
## 3. 函数定义
### 3.1 自定义函数
在 Python 中,可以使用 `def` 关键字定义自定义函数。以下是一个定义平方函数的示例代码:
```python
def square(number):
"""Calculate the square of number."""
return number ** 2
result1 = square(7)
result2 = square(2.5)
print(result1)
print(result2)
```
上述代码中,`square` 函数接受一个参数 `number`,并返回该参数的平方。
### 3.2 函数的文档字符串
函数的文档字符串(docstring)用于简要解释函数的目的。可以使用单引号或双引号来定义文档字符串。例如:
```python
def square(number):
```
0
0
复制全文
相关推荐










