目录
运算符
and or not
in not in
is/is not
循环结构
while
for
编辑函数
可变参数
关键字参数
返回值
lamada匿名函数
闭包(内嵌函数)
类
继承
模块
运算符
逻辑运算符优先级最低。
and or not
python">if a > 9 and b > 9:print("a > 9 and b > 9: ")
if a == 10 or b == 10:print("a == 10 or b == 10: ")
if not 1 == 2:print("1 == 2: ")
in not in
python">list = [ 1 , 2,3,4,5];
if 1 in list:print("1 in list:")
if 8 not in list:print("8 not in list: ")
python">tup1 = (1,2,3)
if 2 in tup1:print("2 in tup1")
dic = {1:"001",2:"002"}
if 2 in dic:print("2 in dic: ")#默认查找key
is/is not
python">a = 10
b = 10
if a is b:print("a is b")
else:print("a not b")
循环结构
while
python">i=0
while i<3:print(i)i+=1
else:print("else i = ",i)
python">i=0
while True:i+=1;if i > 10:breakif i%2!=0:continueprint(i,end=" ")
for
python">list = [1,2,3,4,5,6];
for i in list:print(i,end=" ")
else:print("end i = ",i)
python">for i in range(10 , 100 ,10):print(i,end=" ")
函数
python">def fun1():print("fun1")
fun1()
def func2(a,b=0):print(a,b)
func2(1)
b=0和c++中的缺省参数相似
可变参数
python">def func3(fmt,*values):print(fmt)print("values type is",type(values))print(values)for v in values:print("v = ",v)
func3("%d %d",1,2)
可变参数存放在元组中
关键字参数
python">def func4(a,**kw):print("a = ",a)print("kw type is ",type(kw))if "name" in kw:print(kw["name"])if "age" in kw:print(kw["age"])
func4(1,name="zy")
python">def func4(a,**kw):print("a = ",a)print("kw type is ",type(kw))if "name" in kw:print(kw["name"])if "age" in kw:print(kw["age"])
#func4(1,name="zy")
func4(1)
关键字参数存储于字典中
返回值
默认返回None return和return None是一致的。
python">def func1():print("fun1")
re = func1()
print(re)
python">def fun2():print("fun2")return#return None
re=fun2()
print(re)
python">def func3():list = [1,2,3,4]return list
v1=func3()
print(v1)
返回局部的数列。
lamada匿名函数
python">fun = lambda x,y:x*y
print("lambda fun =",fun(3,5))
python">def func4():return 5,"test",[1,2,3]#返回时会将引用计数+1
i,s,l=func4()
print("i= ",i)
print("s= ",s)
print("l= ",l)
闭包(内嵌函数)
python">def outfun(a):print("outfun ",a);def infun(b):#a 外部函数变量nonlocal aa+=1return a+breturn infun
fun=outfun(1)
print(fun(3))
python">gx=1001
def fun(a,b):global gxgx+=100c=gxprint(a,b,c)def infun(d):e=1003print(e)return infunf=fun(1,2)
f(3)
类
python">
# class Vedio(object):
# def __int__(self):
# print("Create Vedio")
# def __del__(self):
# print("Delete Vedio")
# vedio=Vedio()
#
# print(vedio)
# print(Vedio)
python">class Vedio(object):age=20#私有成员变量__pwd = "123"path = ""def __init__(self,path):self.path=pathprint("path =",path)print("Creat e Vedio")self.name = "xxx"def __del__(self):print("Delete Vedio")def getPwd(self):return self.__pwd
vedio=Vedio("e:/vedio.mp4")
vedio.title="test"
print(vedio.title)
print("vedio.name= ",vedio.name)
print(vedio.age)
print(vedio.getPwd())
print(Vedio)
继承
python">print("===============================")class Mp4Video(Vedio):#不会主动调用父类的构造函数,需要显示调用def __init__(self,path):print("Create Mp4Video")def __del__(self):print("Delete Mp4Video")
mp4=Mp4Video("e:/test.mp4")
print(mp4.getPwd())print(isinstance(mp4,Mp4Video))
isinstance判断类型