# 变量和数据类型

## 数字及其运算

- 计算身高体重指数
- BMI(Body Mass Index) 公式
  \begin{equation}BMI = \frac{weight}{height^{2}}\end{equation}


```python
height = 1.79 # 身高1米79
```


```python
weight = 68.7 # 体重68.7公斤
```


```python
bmi = weight / height ** 2
```


```python
bmi
```




    21.44127836209856



- 数据类型
- float/int
- str
- bool


```python
type(bmi)
```




    float




```python
type(2)
```




    int




```python
type("hello world")
```




    str




```python
type(True)
```




    bool




```python
2 + True # 数据类型转换
```




    3




```python
"hello" + 2 # 运算是有类型的 
```


```python
"hello" + "world" # + : 字符串连接
```


```python
2 + 22.4 # + ： 加法; 数据类型转换 int → float 
```




    24.4




```python
77 % 17 # 取模
```




    9




```python
77 // 17 # 求商
```




    4



## 字符串及其运算
>字符串是字符的集合

```python
he = "他"
didnt = "没"
verb = "上"
obj = "上海的车"
# 用字符串组成另一个字符串
he + didnt + verb * 3 + obj
```


```python
he = "他"
didnt = "没"
verb = "上"
obj = "上海的车"
# 用字符串组成另一个字符串
he + didnt + verb * 3 + obj
```




    '他没上上上上海的车'



- 如果 + * 代表往字符串集合中增加元素
- 那么 - / 运算是取字符串集合的子集？


```python
obj - verb
```


```python
obj / verb
```


```python
obj[1]
```




    '海'




```python
obj[0]
```




    '上'




```python
(he + didnt + verb * 3 + obj)[2:6] # 包括第2，但不包括第6，从第0开始
```

## 布尔型及其运算


```python
bmi > 25
```




    False




```python
type(bmi > 25)
```




    bool




```python
bmi > 20
```




    True




```python
bmi > 20 and not bmi > 25 # and, or, not
```




    True




```python
bmi > 20 and bmi < 25  # 更符合阅读习惯的写法
```




    True




```python
bmi == bmi or bmi != bmi # 完全正确
```




    True




```python
bmi == he or bmi != he 
```




    True



## 分支


```python
bmi = 40
if bmi > 40:                 # Python用缩进区分block
    print("hyper obese")     # 注意这行的开头有TAB
```


```python
bmi = 45                       # 改变bmi，观察分支选择
if bmi > 40:
    print("hyper obese")
else:
    print("not hyper obese")
```

    hyper obese



```python
# 对bmi值进行分类
if bmi > 40:
    print("hyper obese")
elif bmi > 35 and bmi <= 40:
    print("super obese")
elif bmi > 30 and bmi <= 35:
    print("obese")
elif bmi > 25 and bmi <= 30:
    print("over weight")
elif bmi > 18.5 and bmi <= 25:
    print("normal")
elif bmi > 16 and bmi <= 18.5:
    print("under weight")
else :
    print("slim") # https://en.wikipedia.org/wiki/Body_mass_index
```

    hyper obese


#### 根据[肥胖分类定义](https://en.wikipedia.org/wiki/Body_mass_index)补全上面的分类代码

| type                      | from |   to |
|---------------------------|------|------|
| very severely underweight |      |   15 |
| severely underweight      |   15 |   16 |
| underweight               |   16 | 18.5 |
| normal                    | 18.5 |   25 |
| overweight                |   25 |   30 |
| moderately obese          |   30 |   35 |
| severely obese            |   35 |   40 |
| very severely obese       |   40 |   45 |
| morbidly obese            |   45 |   50 |
| super obese               |   50 |   60 |
| hyper obese               |   60 |      |

>可见，各个种族，地区，年龄段对于肥胖的定义是有很大差别的

# 列表和循环

## 列表是数据的容器
>已经学过一种容器：str

## 列表及其运算
- 从通用List开始
- 以后基本只关注数值类型容器
- 注意程序语言和数学语言的映射


```python
ret = he + didnt + verb * 3 + obj # 用一个变量保存计算结果
l = list(ret)                     # 转换为列表
l                                 # 可以把一个字符串转换为列表，反之？
```




    ['他', '没', '上', '上', '上', '上', '海', '的', '车']




```python
type(l)
```




    list




```python
l[2:6]  # 重复之前的操作，取出'上'
```




    ['上', '上', '上', '上']




```python
l[:2]   # 到第2个为止，不包括第2个，从第0个开始
```




    ['他', '没']




```python
l[2:]   # 从第2个开始
```




    ['上', '上', '上', '上', '海', '的', '车']




```python
l[-1]   # 最后一个，或者，从右边开始第1个，注意不是从第0个开始
```




    '车'



##### 负数索引

|-9|-8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|他|没|上|上|上|上|海|的|车|他|没|上|上|上|上|海|的|车|


```python
l[-2:-1] # 虽然索引是从右往左计数的，但范围和这些数字的排列顺序是一致的
```




    ['的']



|-9|-8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|他|没|上|上|上|上|海|的|车|他|没|上|上|上|上|海|的|车|


```python
l[-2:]
```




    ['的', '车']



|-9|-8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|他|没|上|上|上|上|海|的|车|他|没|上|上|上|上|海|的|车|


```python
l[-7:-3]
```




    ['上', '上', '上', '上']



|-9|-8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|他|没|上|上|上|上|海|的|车|他|没|上|上|上|上|海|的|车|


```python
l[:]
```




    ['他', '没', '上', '上', '上', '上', '海', '的', '车']



|-9|-8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7|8|
|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
|他|没|上|上|上|上|海|的|车|他|没|上|上|上|上|海|的|车|


```python
len(l)
```




    9




```python
c = [6, 7, 3, 3, 3, 3, 10, 8, 4] # 特征：笔画数
f = l + c
len(f)
```




    18




```python
f
```

#### 列表里面的数据可以是不同类型的


```python
g = f      # g只是f的另一个名字
g[9] = 0   # 改变列表
f          # f也会跟着变化
```




    ['他', '没', '上', '上', '上', '上', '海', '的', '车', 0, 7, 3, 3, 3, 3, 10, 8, 4]




```python
g = list(f) # 新创建一个列表
f[9] = 6    # 改回去
g           # g[9]仍然是0
```




    ['他', '没', '上', '上', '上', '上', '海', '的', '车', 0, 7, 3, 3, 3, 3, 10, 8, 4]




```python
del g[9]   # 删掉第9个元素
g
```




    ['他', '没', '上', '上', '上', '上', '海', '的', '车', 7, 3, 3, 3, 3, 10, 8, 4]




```python
len(g)
```




    17




```python
len(f)
```




    18



## 循环


```python
for v in f:
    print(type(v)) # 注意这行的开头有TAB
```

    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>
    <class 'int'>



```python
len(range(1)) # 只生成一个数
```




    1




```python
range(1)[0]   # 这个数是0
```




    0




```python
range(len(f)) # f → f的索引
```




    range(0, 18)




```python
for i in range(len(f)): # 以索引做循环变量
    print(i, type(f[i]))
```

    0 <class 'str'>
    1 <class 'str'>
    2 <class 'str'>
    3 <class 'str'>
    4 <class 'str'>
    5 <class 'str'>
    6 <class 'str'>
    7 <class 'str'>
    8 <class 'str'>
    9 <class 'int'>
    10 <class 'int'>
    11 <class 'int'>
    12 <class 'int'>
    13 <class 'int'>
    14 <class 'int'>
    15 <class 'int'>
    16 <class 'int'>
    17 <class 'int'>



```python
for i, v in enumerate(f): # 上例的简化版本
    print(i, type(v))
```

    0 <class 'str'>
    1 <class 'str'>
    2 <class 'str'>
    3 <class 'str'>
    4 <class 'str'>
    5 <class 'str'>
    6 <class 'str'>
    7 <class 'str'>
    8 <class 'str'>
    9 <class 'int'>
    10 <class 'int'>
    11 <class 'int'>
    12 <class 'int'>
    13 <class 'int'>
    14 <class 'int'>
    15 <class 'int'>
    16 <class 'int'>
    17 <class 'int'>




07acee6d8e6ef25b7365deebd613c98d188f71de
ipynb
Jupyter Notebook
基础教程/A1-Python与基础知识/Python基础/02_变量和数据类型.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-07T02:48:50.000Z
2022-03-31T08:49:42.000Z
基础教程/A1-Python与基础知识/Python基础/02_变量和数据类型.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-13T15:07:19.000Z
2022-03-23T08:52:32.000Z
基础教程/A1-Python与基础知识/Python基础/02_变量和数据类型.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-07T02:55:15.000Z
2022-03-30T06:56:52.000Z
Qwen/Qwen-72B
1. YES
2. YES