python
发布时间:2020-12-20 12:44:44 所属栏目:Python 来源:网络整理
导读:缩进 内置常量(Built-in Constants) 内置函数(Built-in Functions) :简称BIF #BIF函数查询 前面两个下划线 _,后面两个下划线 _,总共四个下划线dir(__builtins__)#查询函数用法help(input)#Python 中不能把两个完全不同的东西加在一起,比如说数字和文本,
缩进 内置常量(Built-in Constants) 内置函数(Built-in Functions) :简称BIF #BIF函数查询 前面两个下划线 _,后面两个下划线 _,总共四个下划线 dir(__builtins__) #查询函数用法 help(input) #Python 中不能把两个完全不同的东西加在一起,比如说数字和文本,文本加数字没有意义,但是文本乘以数字,可以看做是将文本翻倍 print('pengyy ' * 5) #可以正常执行 >>> print('pengyy ' * 5) pengyy pengyy pengyy pengyy pengyy print('pengyy ' + 5) #报错 >>> print('pengyy ' + 5) Traceback (most recent call last): File "<pyshell#27>",line 1,in <module> print('pengyy ' + 5) TypeError: can only concatenate str (not "int") to str #在字符串中嵌入双引号 反斜杠 转义 print('my name is "pengyy"') print('my name is 'pengyy'') print("my name is 'pengyy'") #添加双引号进行转义,如果需转义的位置较多,可以使用 原始字符串 #原始字符串:只需要在 字符串 前面加上一个英文字母(r)就可以了 >>> str='d:nowday' >>> print(str) d: owday >>> str='d:nowday' >>> print(str) d:nowday >>> str=r'd:nowday' >>> str 'd:nowday' >>> print(str) d:nowday #原始字符串不能以反斜杠结尾() >>> str = r'C:Program Filespengyytest' SyntaxError: EOL while scanning string literal >>> str = r'C:Program Filespengyytest''' >>> str 'C:Program Filespengyytest' >>> print(str) C:Program Filespengyytest #长字符串 三重引号字符串 三个双引号 或 三个单引号 >>> str="""我的小鱼你醒了,还认识早晨吗? 昨夜你曾经说,愿夜幕永不降临。 你的香腮边轻轻滑落的,是你的泪,还是我的泪? 初吻吻别的那个季节,不是已经哭过了嘛? 我的指尖还记忆着,你慌乱的心跳。 温柔的体香里,那一绺长发飘飘。""" >>> str='''我的小鱼你醒了,还认识早晨吗? 昨夜你曾经说,愿夜幕永不降临。 你的香腮边轻轻滑落的,是你的泪,还是我的泪? 初吻吻别的那个季节,不是已经哭过了嘛? 我的指尖还记忆着,你慌乱的心跳。 温柔的体香里,那一绺长发飘飘。''' #对内置函数赋值操作 str='b' >>> param=12 >>> str(param) '12' >>> a=str(param) >>> print(a) 12 >>> str='b' >>> str(param) Traceback (most recent call last): File "<pyshell#5>",in <module> str(param) TypeError: 'str' object is not callable ------------------------------------------------------------------------------------------ >>> a=0.0000000000000000000000000023 >>> a 2.3e-27 # True 1 ; False 0 () >>> True+True 2 >>> True+False 1 >>> False+False 0 >>> True*False 0 #类型转换 int() str() float() 获取变量类型 type() isinstance() >> a=520 >>> type(a) <class 'int'> >>> a='520' >>> type(a) <class 'str'> >>> a=True >>> type(a) <class 'bool'> >>> a=5.20 >>> type(a) <class 'float'> >>> a='pengyy' >>> isinstance(a,str) True >>> isinstance(a,int) False >>> isinstance(320,int) True >>> isinstance(320.00,int) False >>> isinstance(320.00,float) True >>> isinstance(32,float) False >>> isinstance(32,bool) False # 常用操作符 # + - * / % ** // # % 取余数 ; ** 幂运算 ; // 认作为 向下取整(唤作 地板除法) >>> 5%2 1 >>> 5**2 25 >>> 5//2 2 >>> #三元操作符 x,y=4,5 if x>y: small=y else: small=x print (small) small=x if x<y else y print(small) #循环 while for while 条件: 循环体 for 目标 in 表达式: 循环体 #python3中print函数中的参数end默认值为’n’,表示换行,改变end的赋值,就不会换行了 text='pengyunyun' for i in text: print(i,end=' ') # while循环 随机数 # random 模块中的 randint() 函数,随机产生一个 int类型的整数 import random; print("------------我爱工作室") number=random.randint(1,10) print("随机产生的数字是: ",number) flag=True while flag: temp=input("请输入理想数字:") guess=int(temp) if guess == number: print("我草,你是我肚子里的蛔虫么") print("恭喜你,猜中了") flag=False else: if guess > number: print('猜错了,,,大了大了。。。') else: print("小了小了!!!") print("游戏结束,不玩了!") #assert 断言 #assert 断言语句和 if 分支有点类似,它用于对一个 bool 表达式进行断言,如果该 bool 表达式为 True,该程序可以继续向下执行;否则程序会引发 AssertionError 错误。 #有读者可能会问,明明 assert 会令程序崩溃,为什么还要使用它呢?这是因为,与其让程序在晚些时候崩溃,不如在错误条件出现时,就直接让程序崩溃。通常,assert 语句用在检查函数参数的属性(是参数是否是按照设想的要求传入),或者作为初期测试和调试过程中的辅助工具。 #assert 断言的执行逻辑是: if 表达式的值为 True: 程序继续运行; else: # 表示式的值为 False 程序引发 AssertionError 错误 #break continue #break终止当前循环 continue 结束本轮循环 继续下一轮循环 # 列表(list) 列表索引值从 0 开始 # append 末尾追加 # extend 末尾追加 ,参数是个列表 # insert 两个参数 第一个参数指定位置,第二个参数指的是插入的元素 # remove del pop 删除元素 # str=['aa','bb','cc','dd','yy'] str1=str str2=str[:] # str1 str2 都是 str的拷贝,但是两者有所区别 # str1 会随着 str的修改而变动 , str2 不会随着 str 的变动而变动 str=['aa','yy'] print (str) number=[1,2,4,5,6,7] print (number) mix=[1,3,'aa',[4,6]] print(mix) empty=[] print(empty) print(type(mix)) empty.append('dd') print(empty) empty.extend([1,3]) print(empty) empty.insert(1,'bb') print(empty) empty.insert(0,'aa') print(empty) print('从 str 列表删除元素') str.remove('cc') print(str) del str[2] print(str) num=number.pop() print(number) print(num) num=number.pop(1) print(number) print(num) # 列表(list) 切片 >>> str=['aa','yy'] >>> str1=str[1:3] >>> str1 ['bb','cc'] >>> str2=str[1:] >>> str2 ['bb','yy'] >>> str3=str[:3] >>> str3 ['aa','cc'] >>> str4=str[:] >>> str4 ['aa','yy'] >>> #列表 取值 调换元素顺序 >>> str=['aa','yy'] >>> temp=str[4] >>> str[4]=str[0] >>> str ['aa','aa'] >>> str[0]=temp >>> str ['yy','aa'] #列表元素翻转 排序 >>> str=['aa','yy'] >>> str.reverse() >>> str ['yy','aa'] >>> str.sort() >>> str ['aa','yy'] #元组(tuple) 不能被修改 >>> tuple1=(1,7) >>> tuple1 (1,7) >>> tuple1[2] 3 >>> tuple1[4:] (5,7) >>> tuple1[:4] (1,4) >>> tuple2=tuple1[:] >>> tuple2 (1,7) >>> tuple1[2]=9 Traceback (most recent call last): File "<pyshell#7>",in <module> tuple1[2]=9 TypeError: 'tuple' object does not support item assignment >>> temp=(1) >>> type(temp) <class 'int'> >>> temp=(1,) >>> type(temp) <class 'tuple'> >>> temp=() >>> type(temp) <class 'tuple'> >>> temp=1,3 >>> type(temp) <class 'tuple'> >>> temp=(1,3) >>> type(temp) <class 'tuple'> >>> temp=1,>>> type(temp) <class 'tuple'> >>> temp=('aa','dd') >>> temp=temp[:2]+('ee',)+temp[2:] >>> temp ('aa','ee','dd') # format函数 >>> "{0} are a {1} {2}".format('you','handsome','boy') 'you are a handsome boy' >>> "{a} are a {b} {c}".format('you','boy') Traceback (most recent call last): File "<pyshell#26>",in <module> "{a} are a {b} {c}".format('you','boy') KeyError: 'a' >>> "{a} are a {b} {c}".format(a='you',b='handsome',c='boy') 'you are a handsome boy' >>> "{0:.1f}{1}".format(27.658,'GB') '27.7GB' 函数 def 关键字创建函数 #关键字参数 >>> def say(name,words): print(name + '--->' +words) >>> say('彭云云','你有病啊') 彭云云--->你有病啊 >>> say('你有病啊','彭云云') 你有病啊--->彭云云 >>> say(words='你有病啊',name='彭云云') 彭云云--->你有病啊 #默认值参数 >>> def say(name='彭云云',words='你有病啊'): print(name + '--->' +words) >>> say() 彭云云--->你有病啊 >>> say(name='pengyy') pengyy--->你有病啊 >>> say('pengyy') pengyy--->你有病啊 >>> say('你有病啊') 你有病啊--->你有病啊 >>> say(words='你吃多了') 彭云云--->你吃多了 #收集参数(可变参数) >>> def test(*params): print(len(params)) print(params) print(type(params)) >>> test(1,3.14) 7 (1,3.14) <class 'tuple'> #既有可变参数,又指定某一特定参数 >>> def test(*params,addr): print(len(params),addr) print(params) print(type(params)) >>> test(1,5) Traceback (most recent call last): File "<pyshell#34>",in <module> test(1,5) TypeError: test() missing 1 required keyword-only argument: 'addr' >>> test(1,addr='南京') 7 南京 (1,5) <class 'tuple'> >>> def test(*params,addr='nnajing'): print(len(params),5) 7 nnajing (1,5) <class 'tuple'> >>> test(1,5) <class 'tuple'> #函数返回值 # 函数如果没有明确指定返回值,默认返回 None def sum(a,b): print(a+b) aa=sum(2,3) print(aa) >>> def sum(a,b): return a+b >>> print(sum(1,3)) 4 # 函数返回多个值 >>> def test(): return 1,3 >>> aa=test() >>> print(aa) (1,3) >>> type(aa) <class 'tuple'> #变量作用域 #global 关键字 #内嵌函数 >>> def fun1(): print("fun1()被调用") def fun2(): print('fun2()被调用') fun2() >>> fun1() fun1()被调用 fun2()被调用 #闭包(closure) #python中的闭包从表现形式上定义(解释)为:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). >>> def funx(x): def funy(y): return x * y return funy >>> a=funx(5) >>> type(a) <class 'function'> >>> b=a(6) >>> print(b) 30 # nonlocal 关键字 # lambda 表达式 >>> def dx(x): return 2*x+1 >>> dx(5) 11 >>> lambda x:2*x+1 <function <lambda> at 0x000001B9C95237B8> >>> g=lambda x:2*x+1 >>> g(5) 11 >>> def add(a,b): return a+b >>> add(3,4) 7 >>> lambda a,b:a+b <function <lambda> at 0x000001B9C9523950> >>> g=lambda a,b:a+b >>> g(3,4) 7 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |