巩固Python 基础
基础
对象和类型
五种基本数据类型
- 字符串(string) 简记为 str
- 整数(Integer) 简记为 int
- 加粗样式浮点数(float)
- 布尔数boolean) 简记为 bool
- 复数(complex)
# 检测对象类型
type()
为什么区分数据类型
- 不同类型对象运算规则不同
- 不同类型对象在计算机内表示方式不同
- 为何区分整数与浮点数
- 浮点数表示能力更强
- 浮点数有精度损失
- CPU有专门的浮点数运算部件
运算符
自动类型转换
-
若参与运算的两个对象的类型同,则结果类型不变
-
若参与运算的两个对象的类型不同,则按照以下类型自动转换
- bool -> int -> float -> complex
1.0 + 3
-->
4.0
True + 3
-->
4
True + 3.0
--> 4.0
求余运算符
%
10 % 3
--> 1
关系运算符
- 判断一个数x是否为偶数
- 用于判断两个值的关系
- 运算的结果只有两种(布尔型)
关系运算符 | 含义 | 举例 |
---|---|---|
== | 等于 | (equal) |
!=,<> | 不等于(not equal) | 1-0!= 20 |
> | 大于(greater) | 10>20 |
< | 小于(less) | 10 <20 |
>= | 大于等于(greater or equal) | 10 >= 20 |
<= | 小于等于(less or equal) | 10 <= 20 |
逻辑运算符
逻辑运算符 | 含义 | 举例 |
---|---|---|
and | 与(全真才真) | True and False ==> False |
or | 或(全假才假) | True or False ⇒ True |
not | 非(真变假,假变真) | not False ⇒ True |
运算符优先级
规则1
- 自上而下
- 括号最高
- 逻辑最低
规则2
- 自左向右
- 依次结合
模块
实现一定的功能的Python脚本集合
引入模块
import moudle_name
math 模块
# 引入模块
import math
# 查看模块内容
dir(math)
==》
['__doc__','__loader__','__name__', '__package__', '__spec__',
'acos','acosh', 'asin', 'asinh','atan', 'atan2', 'atanh',
'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf',
'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod',
'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose',
'isfinite', 'isinf','isnan', 'ldexp', 'lgamma', 'log', 'log10',
'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians''remainder',
'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
# 查看帮助
help(math.pow)
-->
Help on built-in function pow in module math:
pow(x, y, /)
Return x**y (x to the power of y).