Python的顺序及循环结构

ops/2024/9/23 5:40:44/

目录

运算符 

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

模块


http://www.ppmy.cn/ops/109951.html

相关文章

线程安全问题和锁

所属专栏&#xff1a;Java学习 1. 线程的状态 新建&#xff08;New&#xff09;状态&#xff1a;当一个线程对象被创建&#xff0c;但还未调用 start () 方法启动时&#xff0c;处于新建状态。此时线程仅仅是一个 Java 对象&#xff0c;系统尚未为其分配资源。 就绪&am…

【文件包含】——日志文件注入

改变的确很难&#xff0c;但结果值得冒险 本文主要根据做题内容的总结&#xff0c;如有错误之处&#xff0c;还请各位师傅指正 一.伪协议的失效 当我们做到关于文件包含的题目时&#xff0c;常用思路其实就是使用伪协议&#xff08;php:filter,data,inpput等等&#xff09;执行…

some electronic products

纽扣电池 button cell 运动手环 sports wristband 智能手环 smart bracelet 皮卡丘夜灯 pikachu night lamp 数字显示充电器 Charger with a digital display 磁吸无线充 magnetic wireless charger 直流电机调速器 DC motor speed controller 继电器模块 relay module 锂离子电…

Docker快速部署Apache Guacamole

Docker快速部署Apache Guacamole ,实现远程访问 git clone "https://github.com/boschkundendienst/guacamole-docker-compose.git" cd guacamole-docker-compose ./prepare.sh docker-compose up -dhttps://IP地址:8443/ 用户名:guacadmin 密码:guacadmin docker …

如何在YoloV8中添加注意力机制(两种方式)

文章目录 概要添加注意力机制流程#添加方式一&#xff1a;将注意力机制添加到额外的一层添加方式二&#xff1a;将注意力机制添加到其中一层&#xff0c;不引入额外的层 概要 提示&#xff1a;这里可以添加技术概要 例如&#xff1a; openAI 的 GPT 大模型的发展历程。 添加…

【FFMPEG】FFplay音视频同步分析(下)

audio_decode_frame函数分析 首先说明一下&#xff0c;audio_decode_frame() 函数跟解码毫无关系&#xff0c;真正的解码函数是 decoder_decode_frame 。 audio_decode_frame() 函数的主要作用是从 FrameQueue 队列里面读取 AVFrame &#xff0c;然后把 is->audio_buf 指向…

Debian 12 中为 root 用户修改最大打开文件数进程数的限制

在 Debian 12 中&#xff0c;管理和配置打开文件的限制涉及到系统级别和用户级别的设置。以下是详细的步骤来修改和管理“打开文件”限制&#xff1a; 1. 查看当前的限制 首先&#xff0c;了解当前的限制配置&#xff1a; 系统级别&#xff1a; cat /proc/sys/fs/file-max这…

可测试,可维护,可移植:上位机软件分层设计的重要性

互联网中&#xff0c;软件工程师岗位会分前端工程师&#xff0c;后端工程师。这是由于互联网软件规模庞大&#xff0c;从业人员众多。前后端分别根据各自需求发展不一样的技术栈。那么上位机软件呢&#xff1f;它规模小&#xff0c;通常一个人就能开发一个项目。它还有必要分前…