# Begin
print(123,456,789, end="\n", sep="*") #123*456*789
lst = ['jay','jj']
for i,j in enumerate(lst,2):
print(i,j)
a = 18
a = format(a,'06')
print(a) #000018
print((1 or (2 and 3>4)))
lst = [1,2,3,4,5,6,7]
for i in range(len(lst)):
print(lst[i])
lst = [1,2,3,[1]]
lst1 = lst.copy() #id不同 内层元素操作共享
lst1[-1].append(2)
print(id(lst1))
print(id(lst))
print(lst,lst1) #[1, 2, 3, [1, 2]] [1, 2, 3, [1, 2]]
python 是解释性语言
逐行的解释执行
编译型:
代码想执行,必须先经过编译.用户拿着着编译的结果取运行
执行效率高
开发效率较低
*输入输出
print(12,34,56,end='',sep='*')
input()
<input type='text'>
变量: 把一个数据暂时存储在内存中,方便后面的程序使用.
命名规范:
1 数字字母下划线
2 不能纯数字,不能字母开头
3 不能关键字 break true false return if while else
4 要有意义 a b n m--算法
5 大小写
6 不要太长
7 不要用中文
8 驼峰 下划线
if
条件判断:
if 条件:
if 体
elif 条件:
...
else:
...
while 最适合干死循环
while 条件:
代码块
服务器-> nginx 内部轮询机制(死循环) 宕机/游戏(死循环)/Windows
break 结束一个循环的执行
continue 停止当前本次循环,继续下次循环
* 运算符
+ - * / // % != >= <=
and 并且 与 左真右真才为真
or 或者 或 一真就真
not 非 非真即假,非假即真
顺序 () not and or
+= -=
in /not in
* 数据 type
int 整数
+ - * / % // > < ==
格式化
str 字符串
join() 爬虫 拼接列表
split() str切割
strip() 去掉空白 空格 制表符 回车
replace() 替换
startswith() 以什么开头
upper() 大写
lower() 小写
endswith() 以什么结束
isdigit() 是不是数字?
title() 单词首字母大写
find() 查找 找不到返回-1
index() 索引 找不到报错
isalpha() 是否纯字母?
isalnum() 是否字母数字?
format() 格式化
索引和切片
str[index]
str[start:end] 顾头不顾尾
str[start:end:step] 头:尾:步长
前闭后开区间 [ )
for 变量 in 可迭代对象:
变量各种操作
bool: 布尔值
True False
1 0
list 列表 [ 元素 ...]
索引 切片
lst[0] 索引从0开始
增
append 追加
insert 插入
extend 迭代添加
删
pop()
remove()
del()
clear()
改
索引修改
lst[index] = 值
查
索引查询
for循环
range(10,10,2)
enumerate 枚举用法
tuple 元组 特点:不可变,可哈希
索引 (1,) 计数
dict 字典 {key:value,...} 哈希表(数据结构)
特点:
key 必须是可哈希的
value 没有要求
增
dict[key]=value
setdefault(key,value) 存在不处理,不存在就创建
update({k:v})
删
pop(key) 不存在的报错KeyError
popitem() 随机删除 空字典报错KeyError: 'popitem(): dictionary is empty'
clear() 清空
del dict 删整个
改
dict[key]=new_value
查
dict.get(key) 找不到返回None
dict[key] 报错 KeyError
for
dict.keys()
dict.values()
dict.items()
没有索引
set集合 {1,}
相当于只存放key的字典,必须是可哈希的
去重
不可哈希的
frozenset
冻结集合
可哈希的
bytes
encode 编码
decode 解码
UTF-8 可变长度的unicode
英文:8bit
欧洲:16bit
中文: 24bit
GBK
ASCII 8bit
-> ANSI 16bit
UNICODE 16bit
0111 0111
万国码
32bit
深浅拷贝
= 没有创建新对象
浅copy 只拷贝第一层
深copy 完全拷贝 数据独立
小数据池
避免重复的大量的创建同一个数据
int -5~256
bool 肯定全都被保存
str 大部分的字符串都会被驻留
文件操作:
open(路径,mode='',encoding='itf-8)
with open() as f:
xxx
with的特点是执行完文件操作,默认帮你关闭连接
__enter__
__exit__
来源:https://www.cnblogs.com/zhangchen-sx/p/10952312.html