# 前言
- 记录一下学习 Python 过程中的笔记,方便日后查找 / 复习
# 参考教程
# 相关概念
# IO
- 输入和输出的统称
- Input:输入
- Output:输出
# 动态语言、静态语言
| |
| |
| |
| value = 123 |
| value = 'test' |
| value = True |
| value = 1.23e5 |
| print(value) |
| |
| |
| |
| int value = 0; |
| value="test"; |
# Python 的诞生
- Python 是著名的 “龟叔” Guido van Rossum 在 1989 年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言。
# Paython 的优缺点
# Python 的优点
- 拥有丰富的生态和大量覆盖各个领域的第三方库,这使得 Python 拥有极高的开发效率
- 跨平台兼容运行、Windows、macOS、Linux
- 适用领域:AI 和大模型,网络爬虫,自动化运维,Web 后端,数据分析等
# Python 的缺点
- Python 是解释型语言,运行性能较低,代码执行前需要翻译成机器码,执行速度约为 C++ 的 1/50、Java 的 1/20,不适合高频交易、实时图像处理等高性能场景
- 不适用于大型项目,不适用于高并发,不适用于多线程程序,不利于大型项目维护
- 源码无法加密,解析型语言发布应用相当于发布源码
- 内存消耗大,处理大量数据时内存占用较高。
- 移动端开发弱,缺乏主流 APP 原生支持
# Python 解释器
注:同一份 Python 代码在不同的解析器环境中运行结果可能会有所不同
CPython :Python 官网默认安装解释器
IPython :基于 CPython 之上的一个交互式解释器,只是在交互方式上有所增强
PyPy :特点是执行速度,采用 JIT 技术,对 Python 代码进行动态编译(注意不是解释),所以可以显著提高 Python 代码的执行速度
Jython :运行在 Java 平台上的 Python 解释器,可以直接把 Python 代码编译成 Java 字节码执行
IronPython :运行在微软.Net 平台上的 Python 解释器,可以直接把 Python 代码编译成.Net 的字节码
# Python 数据类型
# 整形
| |
| number = 100 |
| number = -8080 |
| |
| number = 0xff00 |
| |
| number = 10_000_000_000 |
# 浮点型
| |
| number = 1.23 |
| number = -4.56 |
| |
| number = 1.23e9 |
| |
| number = 1.2e-5 |
# 字符串
| |
| text = 'abc' |
| text = "efg" |
| text = "I'm ok" |
| |
| text = 'She said, "I\'m not coming to the party."' |
| text = "She said, \"I'm not coming to the party.\"" |
| |
| text = r'C:\Users\Name\Desktop' |
| text = r'''He said, "My mom always reminds me, 'An apple a day keeps the doctor away.'" ''' |
| |
| text = ''' |
| Examination subjects: |
| Mysql |
| Python |
| AI and Big data |
| ''' |
# 布尔值
| |
| |
| buer = True |
| buer = False |
| buer = 114 > 514 |
| buer = 1919 > 810 |
# 空值
# 常量
# 运算符
# 逻辑运算符
| a = False |
| b = False |
| c = True |
| value = a or b and not c |
| |
| |
| print(value) |
| |
| buer = True and True |
| buer = True and False |
| buer = False and False |
| buer = 11 > 4 and 5 > 14 |
| |
| buer = True or True |
| buer = True or False |
| buer = False or False |
| buer = 11 > 4 or 5 > 14 |
| |
| buer = not True |
| buer = not False |
| buer = not 1 > 2 |
# 算术运算符
# 真除法 (/)
| |
| value = 6 / 2 |
| value = -1 / 1 |
| value = 10 / 3 |
| value = 6 / 15 |
# 地板除 (//)
| |
| value = 5 // 2 |
| value = -7 // 2 |
# 字符编码
# ASCII 字符集 / 编码
- 最早时期美国定制的编码,长度为 1 个字节 / 8 Bit,只能表示 255 个字符,只能存放一些英文字母、数字、标点符号、控制字符
- ASCII 既是编码又是字符集
- 如:A-->65,a-->97,0-->48
# Unicode 字符集
- 收录了世界上所有的字符,对每个字符都进行统一编号,如:A-->
U+0041 ,中 --> U+4E2D ,😀--> U+1F600
- 在此之前各国都定制了自己的字符集:中文 -->
GB2312 ,日文 --> Shift_JIS ,韩文 --> Euc-kr
# UTF-8 编码
- UTF-8 是 Unicode 最常用的存储方式
- 英文符号存储使用 1 个字节,向下兼容 ASCII 编码。如:A 在 ASCII 和 UTF-8 中都是 0x41。
- 中文 / 日文 / 韩文:3 字节。emoji 或生僻字:4 字节。
- UTF-8 定义一个 Unicode 字符在二进制中的存储方式:
- 如:你 -->U+4F60 (Unicode 编码)-->11100100 10111101 10100000 (1110 作为三字节的起始符,10 作为后续字节开头)
- 如:好 -->U+597D (Unicode 编码)-->11100101 10100101 10111101 (1110 作为三字节的起始符,10 作为后续字节开头)
- 如:A-->U+0041-->01000001 (0 作为 1 字节的起始符)
# Unicode 编码 UTF-8 的过程
- 例:
'你好'.encode('UTF-8') 输出 b'\xe4\xbd\xa0\xe5\xa5\xbd'
你 --> U+4F60 (对应 Unicode 码点)--> 20320 (对应十进制)--> 01001111 01100000 (对应二进制两字节)
- 根据
U+4F60 所在 Unicode 范围匹配到 UTF-8 三字节模板: 1110xxxx 10xxxxxx 10xxxxxx
- 二进制代入到模板:(1110 0100) (10 111101) (10 100000)-->
11100100 10111101 10100000
- 代入生成的二进制转十六进制:
11100100 10111101 10100000 --> E4 BD A0
\x 转义为 python 十六进制字符: E4 BD A0 --> b\xe4\xbd\xa0
好 的编码过程同理,解码过程同理
# Python 编码 / 解码
| |
| print(ord('中')) |
| print(ord('A')) |
| |
| |
| print(chr(65)) |
| print(chr(20013)) |
| |
| |
| print('\u4f60\u597d') |
| print('\U0001F600') |
| print(hex(ord('中'))) |
| |
| x = b'ABCD' |
| |
| print('ABCD'.encode('ascii')) |
| print('你好'.encode('UTF-8')) |
| print('世界'.encode('ascii')) |
| |
| print(b'ABCD'.decode('ascii')) |
| print(b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('UTF-8')) |
| print(b'\xe4\xbd\xa0\xe5\xa5\x00'.decode('UTF-8')) |
| print(b'\xe4\xbd\xa0\xe5\xa5\x00'.decode('UTF-8',errors='ignore')) |
# 字符串格式化
# 占位符 %s %d %f %x
| |
| print('Hello %s!' % 'World') |
| print('Hello %s, you have $%s' % ('花园仓鼠',114514)) |
| |
| |
| print('Today is %d-%02d-%02d' % (2026,10,9)) |
| print('Dear Customer, your Order R%06d is finished' % 12) |
| |
| |
| print('The value of π is %f' % 3.1415926) |
| print('The value of π is %.2f' % 3.1415926) |
| |
| |
| print('%x' % 114514) |
| print('%X' % 114514) |
| print('%#x' % 114514) |
| print('%#08X' %114514) |
| |
| print('学号:{0},姓名:{1},{2}岁,是学生,平均分:{3:.2f}'.format('114514','李田所',24,191.9810)) |
| print('来自{city}的{name}今年{age}岁'.format(city='北京',name='小华',age=18)) |
| print('{:*<10}'.format(24)) |
# f-string (最常用)
| name = '小明' |
| age = 25 |
| print(f'我叫{name},今年{age}岁') |
| |
| price = price=3.14159 |
| print(f'price is {price:.2f}') |
| |
| student={"name":"小红","score":95} |
| print(f'学生:{student["name"]},成绩:{student["score"]}') |
| |
| a,b = 10,3 |
| print(f'{a}除以{b}等于{(a/b):.2f}') |