Python 笔记
- hello world
print("hello world!")
- print 换行符
print('hello \n world')
- print 行结束符
print('hello \n world', end='?')
- 基础数据类型
a = 6
b = 1.34
c = True
d = 'hello world'
变量命名规范
数字 字母 下划线
不能以数字开头
不能使用保留字
严格区分大小写
保留字 from imort as True False None class pass with def yield retrun if elif else for while continue break try raise except assert finally is not in and or labmda del global nonlocal
复合数据类型
list 列表
ls = [6, 1.34, True, 'alex']
tuple 元组
tl = [6, 1.34, True, 'alex']
dict 字典
dl = [6, 1.34, True, 'alex']
set 集合
st = [6, 1.34, True, 'alex']
运算符
格式化字符串
python 3.6 以后支持 f-string format的简化版本
print(f'name is {name}, age is {age}, alex age is {sta.age}') print(f'get {12*3+5}') print(F"name is {name:#<6s}, age is {age:3d}, weight is {weight:^9.2f}") print(F"name is {'davida':#<6s}, age is {111:3d}, weight is {123456.789:9.2f}") print(f'hex value {254:#X}')
format 方式
print('name is {:6s} next'.format(name)) print('name is {:.3s}'.format(name)) print('age is {:0>6d}'.format(age)) print('age is {:0<6d}'.format(age)) print('age is {:\>6d}'.format(age)) print('age is {:6d}'.format(age + 111)) # 默认是右对齐 # print('age is {:>6d} next'.format(age+1)) print('age is {:<6d} next'.format(age + 1)) print('age is {:^6d} next'.format(age + 1)) print('weight is {:+12.3f}'.format(weight)) print('weight is {:.2%}'.format(weight / 120)) print('id is {:6d}'.format(id)) print('\n\n') print('name is {}, age is {}'.format(name, age)) print('name is {0}, age is {1}, name is {0}'.format(name, age)) info_list = [name, age] print('name is {0[0]}, age is {0[1]}, name is {0[0]}'.format(info_list)) print('name is {name}, age is {age}, name is {name}'.format(name=name, age=age)) info_dict = {'name': name, 'age': age} print('name is {name:?>12s}, age is {age}, name is {name}'.format(**info_dict)) class Student(object): def __init__(self, name, age): self.name = name self.age = age sta = Student('alex', 12) stb = Student('lucy', 11) print('alex name is {0.name}, lucy age is {1.age}'.format(sta, stb))
% 方式: 变量和占位符一一对应,不够灵活
print('age is %d' % age) print('age is %6d' % age) print('age is %11d' % age) print('age is %06d' % age) print('age is %011d' % age) print('name is %s' % name) print('name is %24s' % name) print('name is %.3s' % name) print('name is %a' % name) print('weight is %f' % weight) print('weight is %.2f' % weight) # %6.2f 表示总长度为6(包括小数点),保留2位小数 print('weight is %6.2f' % weight) print('MULTI: name is %s, age is %d' % (name, age)) print('MULTI: name is %s, age is %s' % (name, age))
转义字符
基础数据类型转换
复合数据类型转换 容器
字符串编码
赋值与引用的概念
运算符重载/公共操作
内置函数 基础
print详解
if
for
while
def 函数定义
def 参数
def 函数说明信息
lambda
内置函数 高阶
文件操作 open
面向对象 class
导入模块 from import as
内置模块 os
内置模块 time
内置模块 random
内置模块 json
内置模块 request
内置模块 urllib
列表推导式
字典推导式
集合推导式
常用模块 requests
常用模块 pandas
常用模块 pathlib