1. 基础
#type(x) 数据类型
#isinstance(x,int) true or false
#输入输出
#sep
print('hello','world',sep=',')
#文件
# file_source=open('zen.txt','w')
# print('hello world',file=file_source)
# file_source.close()
# username=int(input())#字符串类型-整型
#运算
# 不能数字+字符串
print('='*20)
#向下取整
# a//b
#交换
# a,b=b,a
#比较 = != > < True or False
# if a<b:
# return 1
# else:
# return 0
#逻辑运算符 and or not
#顺序控制结构 循环 选择
if a<b:
return 1
elif a=b:
return 0
else:
return -1
#三元运算符
y = x if x>=0 else -x
y=a if a>=b else b
# while True:
# ...
# if False:
# break
# if i==4:
# continue
# for i in string:#字符串
# print(i)
# list = [1,2,2,3]
# for i in list:#列表
# print(i)
# range(start,stop,[,step])
for i in range(3):
print(i)
else:
...
#相当于flag
#=while-else
#字符串
# motto[0:]
#拼接
str_val='.'.join(list_val)
#大小写转换
str.upper()
str.lower()
str.title()
str.capitalize()
#检索
str.count('o')
str.find('o')
str.index()
str.startswith()
str.endswith()
#分割
str.split('',n)
str.splitlines('')
str.strip()#删除开头、结尾空格
#格式化字符串
f-string
f'a total number of {num}'
#列表和元组
str_val=['1','a',',']
# print(str_val)
list1=list("python")
# print(list1[0])
# print(list1[0:4])
# for i in list1:
# print(i)
number=len(list1)
i=0
while i<number:
print(list1[i])
i+=1
# for i,value in enumerate(list1):
# print(i,value)
#增
list1.append('3.6')
list1.insert(0,'P')
print(list1)
#改
list1[0]='L'
#查
if 'p' in list1:
print("Yes")
print(list1.count('p'))
#删
del list1[0]
list1.pop()
list1.remove('p')#删除第一个出现的
#排序
list1.sort(reverse=True)
print(list1)
#复制
list2=list1.copy()
#元组
list3=('p','t','h')#不可修改
var1,var2,var3=('p','t','h')
_,_,var3=list3
print(var3)
#字典和集合
vegetables={0:'土豆',2:'黄瓜',3:'西红柿'}
List={0:'土豆',2:'黄瓜',3:'西红柿'}
#增
#修改
vegetables[2]='黄花菜'
#删
del vegetables[2]
vegetables.pop(0)
vegetables.clear()
print(vegetables)
#查
# in
keys=List.keys()
values=List.values()
items=List.items()
#遍历
for key in keys:
print(key)
for value in values:
print(value)
for key,value in items:
print("key:",key,"value:",value)
#集合
set_val={1,2,3,4}
set_val.add('world')
# del set_val
set_val.pop()#随机删除
set_val.remove('world')
set_val.discard('world')
print(set_val)
intersection()#交集
union()#并集
difference()#差集
# len()
# max()
# min()
# del()
#function
# def hello():
# print("hello world")
# hello()
def hello(name):
print("hello",name)
name='Hzy'
hello(name)
def my_f(*args,**kwargs):#元组 字典
for arg in args:
print(arg)
#局部变量 全局变量
#递归
#匿名函数 lambda
#匿名函数
# (lambda x,y:x+y)(5,3)
def add(x,y):
return x+y
exp=lambda x,y:x+y
print(exp(5,3))
#列表
[(lambda x:x**2)(x)for x in range(10)]
print(x)
#sort
list_val=[8,5,6,7]
list_val.sort(key=lambda x:x)
print(list_val)
#简化代码
#函数式编程 结果不变
map()
filter()
reduce()
#函数装饰器
@my_decorator()
def for_loop():
pass
for_loop()
#面向对象
#类 class
#实例 instance
class Person(object):
def __init__(self,name):#初始化方法
self.name=name
def get_name(self):
print(self.name)
def get_stu(self):
self.get_name()
print("1")
# andy=Person("0")#实例化
# andy.name="0"
# andy.get_stu()
# Person.get_stu(andy)
#类的继承
class stu(Person):
def__init__(self,age):
super().__init__(name)#父类的init
self.age=age
def get_name(self):
print(self.name)
def get_num(self):
print("5")
class tea(Person):
def get_count(self):
pass
jack=stu("2",2)
jack.get_stu()
jack.get_name()
#方法重写
#先调用子类函数
#异常处理
try:
pass
except:
pass
#可以继续处理后面代码
try:
pass
except:
pass
else:
pass
try:
pass
except:
pass
finally:
pass
#raise
class ValueError(ValueError):
raise ValueError()
#文件操作
file=open('python.txt','r')
file=open('python.txt','w')#覆盖原文件
file=open('python.txt','x')
file=open('python.txt','a')#追加
file=open('python.txt','wb')#bytes utf-8
file=open('python.txt','rb')#二进制
file=open('python.txt','w+')
file.write("hello world")
file.close()
#文件指针
index=file.read()
file.seek(0,0)
#写入
file.write("hello world")
seq=['hello','world']
file.writelines(seq)
content=file.read()
line=file.readline()
print(line,end='')
#with
with open('test.txt','r') as reader:
content=reader.read()
print(content)
#os
import os
#requests 请求 get post
headers={
"User-Agent":"..."
}
response=requests.get(url,headers=headers)
#fake-useragent
#status_code
response.content#byte 爬取图片
response.text#文本
#正则表达式
import re
content='hello,你好,1234'
result=re.match('hello,你好,1234',content)
result=re.match('.*?(\d+)',content)#*?限定符
print(result)
#re.S search
#finall 匹配所有
#sub 替换
#compile 字符串编译成正则对象