企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > * python不用花括号表示同一个代码块,用缩进对齐的方式,表示同一个代码块 ## 1. hello world > * 执行Python脚本有两种方法 > 1. python <脚本> > 2. 像执行shell脚本一样执行 1. python <脚本> ~~~ print("hello world") if True: print ("True") else: print ("False") ~~~ ![](https://box.kancloud.cn/08c2b3d3a1e1e7934b663c09fd71e792_588x63.png) 2. 像执行shell脚本一样执行 > 1. 脚本开头加入解析器 > 2. 脚本具有可执行权限 ` vim sys.py` ~~~ #!/usr/bin/python3 # 加入解析器 import sys print('================Python import mode=========================='); print ('命令行参数为:') for i in sys.argv: print (i) print ('\n python 路径为',sys.path) ~~~ ~~~ chmod +x sys.py ./sys.py # 执行脚本 ~~~ ![](https://box.kancloud.cn/73b53a067e2944c717e64dd34176bba4_958x201.png) * * * * * ## 2. 注释 > 确保对模块, 函数, 方法和行内注释使用正确的风格 > Python中的注释有单行注释和多行注释: > Python中单行注释以 # 开头,例如:: ~~~ # 这是一个注释 print("Hello, World!") ~~~ > 多行注释用三个单引号 ''' 或者三个双引号 """ 将注释括起来,例如: 1. 单引号(''') ~~~ #!/usr/bin/python3 ''' 这是多行注释,用三个单引号 这是多行注释,用三个单引号 这是多行注释,用三个单引号 ''' print("Hello, World!") ~~~ 2. 双引号(""") ~~~ #!/usr/bin/python3 """ 这是多行注释,用三个单引号 这是多行注释,用三个单引号 这是多行注释,用三个单引号 """ ~~~ `print("Hello, World!") ` * * * * * ## 3. 迭代器 1. for 循坏迭代 ~~~ #!/usr/bin/python3 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 for x in it: print (x, end=" ") ~~~ 2. while 循环迭代 ~~~ #!/usr/bin/python3 import sys # 引入 sys 模块 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 while True: try: print (next(it)) except StopIteration: sys.exit() ~~~ * * * * * ## 4. yield >1 . 在 Python 中,使用了 yield 的函数被称为生成器(generator)。 > 2. 跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器。 >3. 在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回yield的值。并在下一次执行 next()方法时从当前位置继续运行。 以下实例使用 yield 实现斐波那契数列: ~~~ #!/usr/bin/python3 import sys def fibonacci(n): # 生成器函数 - 斐波那契 a, b, counter = 0, 1, 0 while True: if (counter > n): return yield a a, b = b, a + b counter += 1 f = fibonacci(10) # f 是一个迭代器,由生成器返回生成 while True: try: print (next(f), end=" ") except StopIteration: sys.exit() ~~~ 打印结果如下 0 1 1 2 3 5 8 13 21 34 55 ## 5. 函数 > 你可以定义一个由自己想要功能的函数,以下是简单的规则: > 1. 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ()。 > 2. 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。 > 3. 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。 > 3. 函数内容以冒号起始,并且缩进。 > 4. return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。 ~~~ def 函数名(参数列表): 函数体 # 对齐表示在一个代码块 ~~~ ## 6. 格式化字符串 ### 6.1 str.format 1. 位置格式,从零开始,按照顺序输出format的内容 ~~~ >>> '{1},{0},{1}'.format('kzc',18) '18,kzc,18' >>> '{1},{0},{0}'.format('kzc',18) '18,kzc,kzc' >>> ~~~ 2. 关键字参数,通过参数输出format的内容 ~~~ >>> '{name},{age}'.format(age=18,name='kzc') 'kzc,18' ~~~ 3. 通过索引(list) > 0[1],表示format中第一个数据,[1]表示下标 ~~~ >>> p=['kzc',18] >>> '{0[0]},{0[1]}'.format(p) 'kzc,18' >>> '{0[1]},{0[0]}'.format(p) '18,kzc ~~~ 4. 精度 ~~~ >>> '{0:.3f}'.format(321.33345) # 0表示format的第一个数据(0默认可不写),.3f表示浮点数格式,并保留三个小数 '321.333' >>> '{:.2f}'.format(321.33345) '321.33' ~~~ * * * * * #### 6.2 % `print("I'm %s. I'm %d year old" % ('Vamei', 99))` ## 7. 装饰器 > * 被装饰函数指向被装饰后的函数,不在指向 > * 代码遵循开放封闭原则,装饰器可以在不修改源代码的前提下,对功能进行修改 ![](https://box.kancloud.cn/aa20b059af7f9a2e97ecc684452abf22_679x347.png) ### 7.1 return ~~~ def decorate(func): #1.因为是被装饰的,会把这个test函数传入给装饰器,func指向test()函数 def inner(): return func() # 返回一下被装饰函数的返回值,不管他有没有返回值,这样比较通用 return inner # 2.装饰器返回一个被装饰的函数的引用给test @decorate def test(): return "test value" print(type(test)) # test表示一个函数,test()表示函数调用 print(test()) # 3.此时test指向inner()函数` ~~~ ![](https://box.kancloud.cn/5a276bf25496a75d11117bde96308e5b_602x75.png) ### 7.2 装饰器参数 #### 1. 定参 ~~~ from time import ctime,sleep def w1(func): def inner(a,b): print("%s called at %s" %(func.__name__,ctime())) print(a,b) func(a,b) return inner def w2(func): def inner(): @w1 def foo(a,b): print(a,b) foo(3,5) ~~~ #### 2. 变参 ~~~ def decorate(func): def inner(*x): for i in x: print(i) return inner @decorate def test(*a): # 使用元组类型参数 for i in a: print(i) test(123,32,'d','k') def decorate1(func): def inner(**x): # 字典类型参数 for i in x.keys(): print(i,x[i]) return inner @decorate1 def test1(**a): for i in a: print(i) test1(a="dailin",b="chenhuan") ~~~ ## 8. random 1. uniform(10,20)随机生成区间小数 ~~~ In [2]: import random In [3]: random.uniform(10,20) Out[3]: 18.093162205505266 In [4]: random.uniform(10,20) Out[4]: 13.232674331833612 ~~~ 2. 生成区间整数 ~~~ In [5]: random.randint(10,20) Out[5]: 18 ~~~ 3. 随机选取字符 ~~~ In [9]: random.choice('sbae') Out[9]: 's' In [10]: random.choice('sbae') Out[10]: 'b' ~~~ 4. 随机抽取 ~~~ In [13]: random.sample('sbae',2) Out[13]: ['s', 'a'] ~~~ ## 9. 队列 ~~~ # Python3.X from queue import Queue # Python2.X from Queue import Queue ~~~ ## 10. 元类 元类就是⽤来创建这些类(对象) 的, 元类就是类的类, 我们由元类创建类(对象),再由类(实例)创建实例对象 > 1. 在大多数编程语言中,类就是用来描述如何生成对象的代码段,python中任然成立. > 2. 类(用class关键字定义的代码段)在python中也是对象,python解释器在遇到class关键字时就会创建一个对象 ~~~ >>> class ObjectCreator(object): … pass ~~~ 会在内存中,生成一个对象名字为ObjectCreator,这个对象具有生成对象的能力,对元类可以: > 1. 你可以将它赋值给⼀个变量 > 2. 你可以拷贝它 > 3. 你可以为它增加属性 > 4. 你可以将它作为函数参数进⾏传递 ~~~ In [2]: class test: ...: pass ...: In [3]: print(test) <class '__main__.test'> # test元类 In [4]: print(type(test)) <class 'type'> In [7]: print(hasattr(test,'t')) # 判断是否有某些属性 False In [8]: t = test() In [9]: print(t) <__main__.test object at 0x0000000004D54C18> In [10]: print(type(t)) <class '__main__.test'> # 他的类型是test元类 ~~~ ## 11. 安装pip3工具 ~~~ sudo apt-get -y install python3-pip ~~~