Python之路,day4-Python基础
生活随笔
收集整理的這篇文章主要介紹了
Python之路,day4-Python基础
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.集合
2.元組 只讀列表,只有count,index2個(gè)方法
3.字典
key-value對(duì)
1.特性
2.查詢速度快,比列表快
python中的hash在同一程序下值相同
python字典中的hash只有key是hash的
hash之后二分查找,劈半劈半
注:只有unicode有encode方法
函數(shù): 1 # 2 def qq(): 3 print("nihao!!!") 4 qq() 5 print(qq()) 6 7 #傳參數(shù) 8 def aa(name): 9 print("woshi,%s"%name) 10 aa('hsc') 11 12 #傳兩個(gè)參數(shù) 13 def aa(name,age,sex='F'): 14 #非默認(rèn)參數(shù)必須放到非默認(rèn)參數(shù)前面 15 #位置參數(shù),非默認(rèn)參數(shù),name,age 16 #默認(rèn)參數(shù) sex ... 17 print("woshi,%s,%s"%(name,age)) 18 aa('hsc',22) 19 #指定參數(shù)賦值,叫做關(guān)鍵字參數(shù) 20 21 #非固定參數(shù) 22 #*args 為了潛在的擴(kuò)展需求 23 #**kwargs 24 #指名字為字典,不指為元組
返回值
1.一旦你的函數(shù)經(jīng)過調(diào)用并開始執(zhí)行,那么你的函數(shù)外部的程序,就沒有辦法再控制函數(shù)執(zhí)行的過程了
此時(shí)外部程序只能安靜的等待函數(shù)的執(zhí)行結(jié)果,為啥要等待函數(shù)結(jié)果,因?yàn)橥獠砍绦蛞鶕?jù)函數(shù)的執(zhí)行
結(jié)果來決定下一步怎么走,這個(gè)執(zhí)行結(jié)果就是以return的形式返回給外部程序
2.return 代表著一個(gè)函數(shù)的結(jié)束
3.return 可以返回任意數(shù)據(jù)類型
4.對(duì)于用戶角度,函數(shù)可以返回任意數(shù)量的值,但對(duì)于py本身來講,函數(shù)只能返回一個(gè)值 1 def auth(): 2 username = input('user:').strip() 3 password = input('passwd:').strip() 4 #get data from db 5 _username = 'hsc' 6 _password = 'qwer' 7 if username == _username and password == _password: 8 print('psssed ') 9 return True 10 else: 11 return False 12 13 def home(): 14 if login_status == True: 15 print('welcome') 16 else: 17 auth() 18 def pay(): 19 if login_status == True: 20 print('welcome') 21 else: 22 auth() 23 24 25 26 login_status = auth() 27 28 home() 29 pay()
局部變量 1 login_status = False#全局變量 2 3 def auth(): 4 username = input('user:').strip() 5 password = input('passwd:').strip() 6 #get data from db 7 _username = 'hsc' 8 _password = 'qwer' 9 if username == _username and password == _password: 10 print('psssed ') 11 #修改全局變量 12 global login_status #利用global修改全局變量 13 login_status = True 14 #局部變量,只在當(dāng)前函數(shù)內(nèi)生效 15 # 這個(gè)login_status與上面的全局變量是完全不同的兩個(gè)變量 16 #占用不同的內(nèi)存空間 17 def home(): 18 if login_status == True: 19 print('welcome') 20 else: 21 auth() 22 print('welcome') 23 def pay(): 24 if login_status == True: 25 print('welcome') 26 else: 27 auth() 28 29 30 31 auth() 32 print(login_status)
######嵌套 1 name = 'alex' 2 3 def changeName(): 4 name = 'jack' 5 print(name) 6 7 def changeName2(): 8 name = 'rain' 9 print('name2',name) 10 changeName2() 11 changeName()
遞歸 1 # def calc(n): 2 # print(n) 3 # if n//2 > 0: 4 # calc(n//2) 5 # calc(10) 6 7 8 #不對(duì) 9 # def calc(n): 10 # # print(n) 11 # if int(n/2) > 0: 12 # return calc(int(n/2)) 13 # return n 14 # # print(n) 15 # 16 # calc(10) 17 18 # def func(n): 19 # print('------',n+1) 20 # func(n+1) 21 # 22 # func(0)
#########編程范式
1.面向過程
函數(shù)
2.面向?qū)ο?br />
3.函數(shù)式編程
內(nèi)置函數(shù) 1 abs()#求絕對(duì)值 2 all()#判斷列表內(nèi)參數(shù)是否全為真 3 any()#列表任意一個(gè)值為真,返回真 4 ascii()#以ascii表的形式顯示內(nèi)容 5 print(ascii('地方'.encode())) 6 bin()#把數(shù)字轉(zhuǎn)換為二進(jìn)制 7 print(bin(10)) 8 bool()#判斷是否為真true false 9 bytes#顯示參數(shù)在ascii表中的位置數(shù)字,0-255之間的數(shù)字,ascii表 10 bytearray()#修改字符串中的一個(gè)值 11 callable()#判斷一個(gè)對(duì)象是否可以調(diào)用 12 chr()#把數(shù)字轉(zhuǎn)換為在ascii表中對(duì)應(yīng)的字符 13 ord()#把字符轉(zhuǎn)換為在ascii表中對(duì)應(yīng)的數(shù)字 14 compile()### 15 f=open('返回值.py',encoding='utf-8') 16 code = compile(f.read(),'','exec') 17 print(code) 18 exec(code)### 19 eval()#數(shù)字運(yùn)算 20 exec()#運(yùn)行代碼 21 complex#返回復(fù)數(shù) 22 dir()#返回文件(列表。。。。)可用的方法 23 divmod()#返回商和余數(shù) 24 divmod(10,2) 25 enumerate#格式化輸出 26 filter()#后面值滿足才會(huì)留下來 27 for i in filter(lambda x:x>5,range(10)): 28 print(i) 29 float#浮點(diǎn)型 30 format() 31 frozenset# 32 a = frozenset({1,4,4,5,5,6}) 33 b = {1,4,5,5,6} 34 print(a) 35 globals()#把當(dāng)前程序所在內(nèi)存里的所有數(shù)據(jù)都以字典型形式打印出來 36 locals()#打印局部 37 hex()#求十六進(jìn)制 38 #0x---》十六進(jìn)制的表示,例:0x8 39 list()#轉(zhuǎn)換為列表 40 max()#求最大 41 min()#求最小 42 oct()#八進(jìn)制 43 #0o---》十六進(jìn)制的表示,例:0o8 44 pow(4,9)#冪 45 46 #分隔符 47 msg = "又回到最初的起點(diǎn)" 48 f = open("tofile","w") 49 print(msg,"記憶中你青澀的臉",sep="|",end="",file=f) 50 51 reversed#反轉(zhuǎn) 52 data = [3,4,5,6,7,8] 53 data = reversed(data) 54 for i in data:print(i) 55 56 round()#四舍五入,五舍六入 57 58 set#列表變集合 59 60 slice#切片 61 a = range(20) 62 pattern = slice(3,8,2) 63 for i in a[pattern]:#等于a[3:8:2] 64 print(i) 65 66 sorted()#將字符串按ascii表排序 67 a = 'hsc' 68 a = sorted(a) 69 print(a) 70 71 sum#列表求和 72 73 tuple#轉(zhuǎn)換為元組 74 75 vars()#把當(dāng)前程序所在內(nèi)存里的所有數(shù)據(jù)都以字典型形式打印出來 76 77 zip#拉鏈,合并 78 a = [1,3,5,7] 79 b = [2,4,6,8] 80 for i in zip[a,b]: 81 print(i)
2.元組 只讀列表,只有count,index2個(gè)方法
3.字典
key-value對(duì)
1.特性
2.查詢速度快,比列表快
python中的hash在同一程序下值相同
python字典中的hash只有key是hash的
hash之后二分查找,劈半劈半
注:只有unicode有encode方法
函數(shù): 1 # 2 def qq(): 3 print("nihao!!!") 4 qq() 5 print(qq()) 6 7 #傳參數(shù) 8 def aa(name): 9 print("woshi,%s"%name) 10 aa('hsc') 11 12 #傳兩個(gè)參數(shù) 13 def aa(name,age,sex='F'): 14 #非默認(rèn)參數(shù)必須放到非默認(rèn)參數(shù)前面 15 #位置參數(shù),非默認(rèn)參數(shù),name,age 16 #默認(rèn)參數(shù) sex ... 17 print("woshi,%s,%s"%(name,age)) 18 aa('hsc',22) 19 #指定參數(shù)賦值,叫做關(guān)鍵字參數(shù) 20 21 #非固定參數(shù) 22 #*args 為了潛在的擴(kuò)展需求 23 #**kwargs 24 #指名字為字典,不指為元組
?
返回值
1.一旦你的函數(shù)經(jīng)過調(diào)用并開始執(zhí)行,那么你的函數(shù)外部的程序,就沒有辦法再控制函數(shù)執(zhí)行的過程了
此時(shí)外部程序只能安靜的等待函數(shù)的執(zhí)行結(jié)果,為啥要等待函數(shù)結(jié)果,因?yàn)橥獠砍绦蛞鶕?jù)函數(shù)的執(zhí)行
結(jié)果來決定下一步怎么走,這個(gè)執(zhí)行結(jié)果就是以return的形式返回給外部程序
2.return 代表著一個(gè)函數(shù)的結(jié)束
3.return 可以返回任意數(shù)據(jù)類型
4.對(duì)于用戶角度,函數(shù)可以返回任意數(shù)量的值,但對(duì)于py本身來講,函數(shù)只能返回一個(gè)值 1 def auth(): 2 username = input('user:').strip() 3 password = input('passwd:').strip() 4 #get data from db 5 _username = 'hsc' 6 _password = 'qwer' 7 if username == _username and password == _password: 8 print('psssed ') 9 return True 10 else: 11 return False 12 13 def home(): 14 if login_status == True: 15 print('welcome') 16 else: 17 auth() 18 def pay(): 19 if login_status == True: 20 print('welcome') 21 else: 22 auth() 23 24 25 26 login_status = auth() 27 28 home() 29 pay()
?
局部變量 1 login_status = False#全局變量 2 3 def auth(): 4 username = input('user:').strip() 5 password = input('passwd:').strip() 6 #get data from db 7 _username = 'hsc' 8 _password = 'qwer' 9 if username == _username and password == _password: 10 print('psssed ') 11 #修改全局變量 12 global login_status #利用global修改全局變量 13 login_status = True 14 #局部變量,只在當(dāng)前函數(shù)內(nèi)生效 15 # 這個(gè)login_status與上面的全局變量是完全不同的兩個(gè)變量 16 #占用不同的內(nèi)存空間 17 def home(): 18 if login_status == True: 19 print('welcome') 20 else: 21 auth() 22 print('welcome') 23 def pay(): 24 if login_status == True: 25 print('welcome') 26 else: 27 auth() 28 29 30 31 auth() 32 print(login_status)
?
######嵌套 1 name = 'alex' 2 3 def changeName(): 4 name = 'jack' 5 print(name) 6 7 def changeName2(): 8 name = 'rain' 9 print('name2',name) 10 changeName2() 11 changeName()
?
遞歸 1 # def calc(n): 2 # print(n) 3 # if n//2 > 0: 4 # calc(n//2) 5 # calc(10) 6 7 8 #不對(duì) 9 # def calc(n): 10 # # print(n) 11 # if int(n/2) > 0: 12 # return calc(int(n/2)) 13 # return n 14 # # print(n) 15 # 16 # calc(10) 17 18 # def func(n): 19 # print('------',n+1) 20 # func(n+1) 21 # 22 # func(0)
?高階函數(shù)
1 # calc2 = lambda x:x*x 2 # def calc(n): 3 # return -n 4 # 5 # a=6 6 # calc(a) 7 8 def add(x,y,f): 9 return f(x) + f(y) 10 11 def calc(n): 12 return n+1 13 14 res = add(3,-6,calc) 15 #abs 求絕對(duì)值 16 print(res)?
#########編程范式
1.面向過程
函數(shù)
2.面向?qū)ο?br />
3.函數(shù)式編程
內(nèi)置函數(shù) 1 abs()#求絕對(duì)值 2 all()#判斷列表內(nèi)參數(shù)是否全為真 3 any()#列表任意一個(gè)值為真,返回真 4 ascii()#以ascii表的形式顯示內(nèi)容 5 print(ascii('地方'.encode())) 6 bin()#把數(shù)字轉(zhuǎn)換為二進(jìn)制 7 print(bin(10)) 8 bool()#判斷是否為真true false 9 bytes#顯示參數(shù)在ascii表中的位置數(shù)字,0-255之間的數(shù)字,ascii表 10 bytearray()#修改字符串中的一個(gè)值 11 callable()#判斷一個(gè)對(duì)象是否可以調(diào)用 12 chr()#把數(shù)字轉(zhuǎn)換為在ascii表中對(duì)應(yīng)的字符 13 ord()#把字符轉(zhuǎn)換為在ascii表中對(duì)應(yīng)的數(shù)字 14 compile()### 15 f=open('返回值.py',encoding='utf-8') 16 code = compile(f.read(),'','exec') 17 print(code) 18 exec(code)### 19 eval()#數(shù)字運(yùn)算 20 exec()#運(yùn)行代碼 21 complex#返回復(fù)數(shù) 22 dir()#返回文件(列表。。。。)可用的方法 23 divmod()#返回商和余數(shù) 24 divmod(10,2) 25 enumerate#格式化輸出 26 filter()#后面值滿足才會(huì)留下來 27 for i in filter(lambda x:x>5,range(10)): 28 print(i) 29 float#浮點(diǎn)型 30 format() 31 frozenset# 32 a = frozenset({1,4,4,5,5,6}) 33 b = {1,4,5,5,6} 34 print(a) 35 globals()#把當(dāng)前程序所在內(nèi)存里的所有數(shù)據(jù)都以字典型形式打印出來 36 locals()#打印局部 37 hex()#求十六進(jìn)制 38 #0x---》十六進(jìn)制的表示,例:0x8 39 list()#轉(zhuǎn)換為列表 40 max()#求最大 41 min()#求最小 42 oct()#八進(jìn)制 43 #0o---》十六進(jìn)制的表示,例:0o8 44 pow(4,9)#冪 45 46 #分隔符 47 msg = "又回到最初的起點(diǎn)" 48 f = open("tofile","w") 49 print(msg,"記憶中你青澀的臉",sep="|",end="",file=f) 50 51 reversed#反轉(zhuǎn) 52 data = [3,4,5,6,7,8] 53 data = reversed(data) 54 for i in data:print(i) 55 56 round()#四舍五入,五舍六入 57 58 set#列表變集合 59 60 slice#切片 61 a = range(20) 62 pattern = slice(3,8,2) 63 for i in a[pattern]:#等于a[3:8:2] 64 print(i) 65 66 sorted()#將字符串按ascii表排序 67 a = 'hsc' 68 a = sorted(a) 69 print(a) 70 71 sum#列表求和 72 73 tuple#轉(zhuǎn)換為元組 74 75 vars()#把當(dāng)前程序所在內(nèi)存里的所有數(shù)據(jù)都以字典型形式打印出來 76 77 zip#拉鏈,合并 78 a = [1,3,5,7] 79 b = [2,4,6,8] 80 for i in zip[a,b]: 81 print(i)
?
轉(zhuǎn)載于:https://www.cnblogs.com/heshaochuan/p/6032763.html
總結(jié)
以上是生活随笔為你收集整理的Python之路,day4-Python基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础-接口
- 下一篇: centos6.8升级python3.5