Python 基础


巩固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).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值