Python的顺序及循环结构

目录

运算符 

and   or  not

 in  not in

 is/is not

循环结构

while

for

​编辑函数

可变参数

关键字参数

返回值

lamada匿名函数

闭包(内嵌函数)

继承

模块


运算符 

 

逻辑运算符优先级最低。

and   or  not

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

list = [ 1 , 2,3,4,5];
if 1 in list:
    print("1 in list:")
if 8 not in list:
    print("8 not in list: ")
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

a = 10
b = 10
if a is b:
    print("a is b")
else:
    print("a not b")

循环结构

while

i=0
while i<3:
    print(i)
    i+=1
else:
    print("else i = ",i)
i=0
while True:
    i+=1;
    if i > 10:
        break
    if i%2!=0:
        continue
    print(i,end="  ")

for

list = [1,2,3,4,5,6];
for i in list:
    print(i,end="  ")
else:
    print("end i = ",i)
for i in range(10 , 100 ,10):
    print(i,end="  ")



函数

 

def fun1():
    print("fun1")
fun1()
def func2(a,b=0):
    print(a,b)
func2(1)

b=0和c++中的缺省参数相似

可变参数

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)

可变参数存放在元组中

关键字参数

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")
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是一致的。

def func1():
    print("fun1")
re = func1()
print(re)
def fun2():
    print("fun2")
    return
    #return None
re=fun2()
print(re)
def func3():
    list = [1,2,3,4]
    return list
v1=func3()
print(v1)

返回局部的数列。

lamada匿名函数

fun = lambda x,y:x*y
print("lambda fun =",fun(3,5))
def func4():
    return 5,"test",[1,2,3]#返回时会将引用计数+1
i,s,l=func4()
print("i= ",i)
print("s= ",s)
print("l= ",l)

闭包(内嵌函数)

def outfun(a):
    print("outfun ",a);
    def infun(b):
        #a 外部函数变量
        nonlocal  a
        a+=1
        return a+b
    return infun
fun=outfun(1)
print(fun(3))

gx=1001
def fun(a,b):
    global gx
    gx+=100
    c=gx
    print(a,b,c)
    def infun(d):
        e=1003
        print(e)
    return infun

f=fun(1,2)
f(3)


# class Vedio(object):
#     def __int__(self):
#         print("Create Vedio")
#     def __del__(self):
#         print("Delete Vedio")
# vedio=Vedio()
#
# print(vedio)
# print(Vedio)
class Vedio(object):
    age=20
    #私有成员变量
    __pwd = "123"
    path = ""
    def __init__(self,path):
        self.path=path
        print("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)

继承

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判断类型

模块

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值