目录
查看具体的指南 doc.python.org
一、基本数据类型
使用pycharm 中的python console验证简单的
1. bool型
b1=Ture
b2=False大写
b1=2>3
print(b1)
bool('True') 结果: True
bool('true') 结果: True
bool('asdfer') 结果: True
bool('') 结果: False
bool(1) 结果: True
bool(1.0) 结果: True
bool(0) 结果: False
在代码中必须用print(b1) 在python console 中可以直接输b1
2. int型
+ - * / %
// 是除完之后只剩整数
3. float型
判断两个浮点数(用+-等运算的)是否相等
x=13.25*2.3
y=13.25+13.25+13.25*0.3
eps=1e-6
abs(x-y)<eps
4. complex复数
i 用j 来表示
x=3+2j
y=4-3j
x*y
x/y
5. string
s='abc'
s="abc" 他俩相等
若需要转行 s="abc\ndef"
s="""abc"""
需要转行 s="""abc
def"""
一般在代码中写三个双引号用来写大段注释
(1)求字符串长度
s='中文abc'
len(s)
5
在python中中文表示一个字符,但在c++中表示两个
(2)字符串只能加减字符串,不能与整数型的相运算
s="中国abc"
s=s+'def'
s=s+str(22)
6. 判断是哪种类型
isinstance(s,int)
7. 判断两个值是否相等
x=1
y=1
id(x)
id(y)
判断两个id是否相等
python中都是引用
8. 幂值
2**10
二、序列类型
1. list
x=[1,2,3]
type(x) x的类型
x.append(4) 结果是[1,2,3,4]
(1) 访问数组
x=[1,2,3,4]
x[0] 取第一位数
x=[1,2,3,2]
x[1]
x[3]
x[1] is x[3]
判断某个元素是否在列表里
x=[1,2,3,2]
1 in x
两个列表相拼接+
s=[1,2,3]
t=["abc","def"]
s+t 结果为[1,2,3,'abc','def']
x=[[]]*3 结果为x=[[],[],[]]
x[1].append(2) x=[[2],[2][2]]
x=[[],[],[]]
x[1].append(2) x=[[],[2],[]]
列表元素的最后一位是 x[-1] 倒数第二x[-2]
切片 y=x[1:3] 截取位置1,2重建列表
y=x[0:4:2] 从0个开始,直到第四个,中间步数为2
查看某个元素在第几个位置上
x=[1,2,3,4]
x.index(3) 结果为2
查看某个元素出现了几次 x.count(元素)
删除元素
x=[1,2,3] del x[2] 结果:x [1,2]
2. tuple元组
x=(1,2,3)
type(x)
(1)元组是不能修改赋值的
(2)元组的引用是不会变的,但是如果引用的元素是可变的,那元组就会变
x1=[1,2] x2=[2,3] x3=[5,6]
x=[x1,x2,x3] 结果 x [[1,2],[2,3],[5,6]]
x=(x1,x2,x3) x1=['a','b'] 结果 x ([1,2],[2,3],[5,6])
x2.append('abc') 结果 x ([1,2],[2,3,'abc'],[5,6])
三、字符串类型
1. 定义
(1)字符内部有符号,加\
'I\'m botam' 结果 'I'm botam'
"I'm battom"
(2)构造路径时
s='D:\\dir\\a.txt' print(s) 结果为D:\dir\a.txt
(3)r的作用
在前面加r,自动不把\当成转义字符
s=r'D:\dir\a.txt' print(s) 结果为D:\dir\a.txt
2. 格式化字符串
(1)字符串拼接 用+ ,但不能把数值和字符串拼接,必须得先转化类型
name='Mike' age=12 ser=name+'is'+str(age)+'years old'
(2) % ‘%d+%m’%(12,13) %前面是字符串 后面是元组 注意两边的数目是一样的
pattern='%s is %d years old'
msd=pattern % (name,age)
pattern='10%s is %d years old' 第一个字符是10个位
msd=pattern % (name,age)
(3) format
msd='{:d}+{:d}={:d}'.format(1,21,1+21)
msd '1+21=22'
m='{name} is {age} years old.'.format (name='Mike',age=12)默认按顺序,若想改变顺序,则
msg='{1:d}+{1:d}+{0:d}'.format(2,3)
msg '3+3+2'
(4)f
f'{name} is {age} years old'
结果为 'Mike is 12 years old'
(5)*
'hello'*3
结果为'hello hello hello'
注意:字符串只读,不可写
(6)列表与字符串
text='1,2,3,45,78,99'
seq=list(text) 结果为['1',',','2',',','3',',','4','5',',','7','8',',','9','9']
seq[0]='A' 结果为['A',',','2',',','3',',','4','5',',','7','8',',','9','9']
想要把seq变为字符串型
s=""
s+=seq[0] s='A'
四、结构控制语句
1. If语句
if 条件:
print() 若有多个print 要对齐
score=int(input('plz input score:'))
if score>=60:
print('pass')
print('ok')
else:
print('Fail')
score=int(input('plz input score:'))
if score>=90:
grade='A'
elif score>=80:
grade='B'
elif score>=70:
grade='C'
elif score>=60:
grade='D'
else:
grade='E'
print(f'Your grade is {grade}')
注意:(1)浮点数不要用==比较,最好用差的绝对值小于一个小的数
(2) is 和==
(3) 与and 或or 非not in not in is is not ()
2. while
想要改变某个的名称,比如把sum改为total,可以用点右键,refactor rename
i = 1
total = 0
while i <= 100:
total += i
i += 1
print(total)
3. for
scores=[78,62,77,45,32]
for score in scores:
if score >= 60:
print('pass')
else:
print('fail')
隐含迭代:看有没有未访问的;next
注释可以用#
idx = 0
while idx < len(scores):
if scores[idx] >= 60:
print ('pass')
else:
print ('fail')
idx += 1
元组可拆解赋值
a,b=1,2
x=(1,2)
ab,na=x
枚举
scores=[76,85,96,42,23]
for iex,score in enumerate(scores):
print(iex,score)
range
scores=[76,85,96,42,23]
for i in range(1,4):
print(scores[i])
zip 拉链 一对一对的
scores=[76,85,96,42,23]
names=['Alice','Bob','Cell','Dog','Ellen']
for name,score in zip(names,scores):
print(name,score)
五、字典
1.key-value
scoreSheet = {'Mike':68,'Cloo':78}
x=scoreSheet['Bob']
score = {'Mike':68,'Cloo':78}
x=score.get('Bob')
print(x) 结果为none
x=score.get('Bob',0) 若没有,结果为0
增加一组:
score['Bob']=88
删除一组:
del score['Mike']
2. 字典 keys:values
scores.keys() 结果为键
scores.values() 结果为值
scores.items() 结果为字典的元组
3. 键值的遍历
scoreSheep = {
'Mike':(78,65,89),
'Mimo':(98,75,75),
'oi':(68,75,36),
}
1. for key in scoreSheep.keys():
print(key,scoreSheep[key])
2. for key,value in scoreSheep.items():
print(key,value)
3. for key in scoreSheep:
print(key,scoreSheep[key])
4. for name in scoreSheep:
literature, math, english = scoreSheep[name]
avermark = (literature + math + english) / 3
msg = f'{name} : {literature} , {math} ,{english}, {avermark}'
print(msg)
4. hash函数
要把各种数据对象转换为整数
把相应的键值转换为整数 hash的值也不能变
六、函数
1. 函数的定义
(1)def 函数名( ):
def func1():
print("hello world!")
func1()
(2)引用
def func1(x):
x[1]=100
x=[1,2,3]
print(x)
func1(x)
print(x) 结果为[1,2,3] [1,100,3]
如果函数引用的是列表、集合,可以改变,但如果是整数、浮点数,是不会改变的,他只是外部的
def func1(x):
x=2
x=1
print(x)
func1(x)
print(x) 结果为1 1
返回值
def func1(x):
return x**2+3*x-2
x=1
y=func1(x)
print(y)
(3)参数
def func1(x,y):
return x-y
z=func1(3,5)
print(z)
z=func1(y=3,x=5)
或者 func1(x,y=3) z=func1(2)
或者 func1(x=2,y=3) z=func1()
(4) 参数可变
def foobar(x,y,*args):
print(x,y,args)
foobar(1,2,"hello",36.80,False) 结果为1 2 ('hello', 36.8, False)
遍历参数
def foobar(x,y,*args):
for x in args:
print(x)
foobar(1,2,"hello",36.80,False)
def foobar(x,y,*args,**kwargs):
print(args) 结果为('hello', 36.8, False)
print(kwargs) {'arg1': 32, 'arg2': 96}
foobar(1,2,"hello",36.80,False,arg1=32,arg2=96)
元组、字典
2.一等公民 first class
要求:a.可被变量引用
def f(x):
return 3*x**2+2*x-5
y=f
print(y(1.0))
b. 参数传递(把函数当成参数)
def f(x):
return 3*x**2+2*x-5
def diff(x,func):
delta=1e-6
return (func(x+delta)-func(x))/delta
print(diff(1,f))
c 可加入到集合
def f1(x):
return x
def f2(x):
return x**2
def f3(x):
return x**3
funs=[f1,f2,f3]
for f in funs:
print(f(5)) 结果为 2 25 125
d 可以作为返回值
def fun1(x):
def fun2(y):
return x+y
return fun2
a=fun1(3)
print(a(5))
也可print(fun1(3)(5))
闭包:函数里面内嵌一个函数;返回的值必须是内嵌的函数;内嵌的函数必须用外面函数的参数;
3. lambda表达式
如果只是单行的,lambda可匿名
def diff(x,f):
delta=1e-6
return (f(x+delta)-f(x))/delta
dy=diff(2 ,lambda x: 2* x**2+3)
print(dy)
八、异常处理
1.语法错误
2. 语义错误
3. 运行时错误
(1)跟踪异常
try:
x=float("abc")
except:
print("can not convert")
print("Done")
try:
x=float("abc")
except Exception as e:
print(e.args)
print("Done") 结果为:("could not convert string to float: 'abc'",)
Done
finally 不管怎么finally后的输出
try:
x=float("1.2")
except Exception as e:
print(e.args)
finally:
x=0
print(x) 结果为0
try:
x = 0
x=float("1.2")
except Exception as e:
print(e.args)
print(x)
没有异常
def askuser():
x=float(input("plz input a floating number: "))
return x
print(askuser())
异常监视
def askuser():
while True:
try:
x=float(input("plz input a floating number: "))
return x
except ValueError as e:
print('Errors:' + e.args[0])
print(askuser())
九、文件处理
1.打开文件
f=open('news.txt','r')
lines=f.readlines()
print (lines) 注:文件与python文件在同一个文件夹,r表示读,\u3000表示空格
2.关闭文件
打开后,读取 f.close()
3. 读取文件
with open('news.txt','r')as f:
f=open('news.txt','r')
lines=f.readlines()
for line in lines:
print (lines) 相当于try
想把开头和换行的空格删掉
for line in lines:
line=line.strip()
tokens=line.split('。') 按句号分割
print(token)
但是结果中它按句号分割,若句号后没有字,仍然有一个空格出现,解决方法:
sents=[]
for line in lines:
line=line.strip()
tokens=line.split('。') 按句号分割
#for token in tokens:
# if len(tokens)>0
# sents.append(token)
这三行注释掉可以换成sent.extend(list(filter(lambda x: len(x)>0,tokens)))
print(sents)
强制读取
with open('news.txt','r',encoding='cp936') as f: cp936可换
得出每个字符串的长度
lens=[]
for sent in sents:
lens.append(lens(sent))
print(lens)
或者
lens=[len(sent) for sent in sents]
上面这是列表生成式
例
[(x,y) for x in range(1,4) for y in range(2,8)]
结果为:[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (3, 2),(3, 3), (3, 4), (3, 5), (3, 6), (3, 7)]
如果哪句话有“公司”这两字,输出这句话
for sent in sents:
if "公司" in sent:
print(sent)
4. 正则表达式(处理字符串的常用包)
import re
with open('news.txt','r') as f:
f = open('news.txt','r',encoding='cp936')
lines = f.readlines()
sents=[]
for line in lines:
line=line.strip()
tokens=line.split('。')
sents.extend(filter(lambda x:len(x),tokens))
regex="公司"
for sent in sents:
if re.search(regex,sent) is not None:
print(sent)
regex="公." .为通配符,表示公什么
regex="[工上]" 表示有工或上
regex="[0-9]" 表示包含数字
regex="[0-9]" 表示包含数字
regex="2[0-9]{3}" 表示2000年之后
print(re.findall(regex,sent)) 找出2000年以后的所有的值以列表的形式返回
如果regex='[0-9]' 上面的结果为每个数字列出来,比如['1','0','0']但不想要这种
如果regex='[0-9]+' 表示至少0-9出现一次
标准库参考中有更多正则的说法
5. 写入文件
with open('output.txt','w',encoding='utf-8') as f:
f.writelines(sents)
但结果没有换行,可以这样子写
with open('output.txt','w',encoding='utf-8') as f:
sents=[sent + '\n' for sent in sents]
f.writelines(sents)