python學(xué)習(xí)(re0)day 2
2019/6/25
一、前言
今天是學(xué)習(xí)python的第二天.
二、學(xué)習(xí)內(nèi)容
1、常用數(shù)據(jù)類型及內(nèi)置方法
1.列表(list)
定義:在中括號(hào)[]內(nèi)存放任意多個(gè)值,用逗號(hào)隔開.
具體函數(shù)和內(nèi)置方法如下:
#定義學(xué)生列表,可存放多個(gè)學(xué)生 students=['a','b','c','d'] print(students[1]) student_info=['e',18,'mele',['喝酒','泡吧']] print(student_info[3]) print(student_info[3][1]) #2 切片(顧頭不顧尾,步長(zhǎng)) print(student_info[0:4:2]) #3 長(zhǎng)度 print(len(student_info)) #4 成員運(yùn)算 print('e' in student_info) print('e' not in student_info) #5 追加 student_info=['e',18,'mele',['喝酒','泡吧']] student_info.append('合肥學(xué)院') #6 刪除 del student_info[2] print(student_info) #7 index獲取某個(gè)值的索引 student_info_1=['h',17,'femele','尬舞','喊麥','17'] print(student_info_1.index(17)) #8 獲取某個(gè)值的數(shù)量 print(student_info_1.count(17)) #9 pop取值 默認(rèn)取列表中最后一個(gè)值 有索引就取索引的值 student_info_1.pop() print(student_info_1) # sex=student_info_1.pop(2) print(sex) print(student_info_1) #10 remove student_info_1=['h',17,'femele','尬舞','喊麥','17'] student_info_1.remove(17)#從左到右刪除第一個(gè)遇到的值 print(student_info_1) name=student_info_1.remove('h') print(name) print(student_info_1) #11 insert 插入 student_info_1.insert(3,'合肥學(xué)院') print(student_info_1) #12 extend 合并列表 student_info_a=['h',17,'femele','尬舞1','喊麥2','17'] student_info_b=['g',17,'femele','尬舞1','喊麥2','17'] student_info_a.extend(student_info_b) print(student_info_a) #13 循環(huán) for student in student_info_1: print(student)
2.元組(tuple)
定義:在中括號(hào)()內(nèi)存放任意多個(gè)值,用逗號(hào)隔開.
注意:元組與列表不一樣的是,只能在定義的時(shí)候初始化,不能對(duì)其進(jìn)行修改.
優(yōu)點(diǎn):在內(nèi)存中占用的資源比列表要小.
#tuple元組 tuple1=(1,2,3,'四','five') print(tuple1) #1 按索引取值 print(tuple1[2])#取第三個(gè)值 #2 切片(顧頭不顧尾,步長(zhǎng)) print(tuple1[0:5:2]) #3 長(zhǎng)度 print(len(tuple1)) #4 成員運(yùn)算 print(1 in tuple1) print(1 not in tuple1) #5 循環(huán) for line in tuple1: print(line) print(line,end='')
3.可變和不可變類型
'''' #不可變類型 數(shù)字類型 int float 字符串類型 str 元組 tuple #可變類型 列表list 字典dict 集合可變和不可變都有''' number=100 print(id(number)) number=111 print(id(number)) print() sal=100.1 print(id(sal)) sal=111.1 print(id(sal)) print() str1='hello python' print(id(str1)) str2=str1.replace('hello','like') print(id(str2)) #可變類型 #list1與list2指向同一個(gè)內(nèi)存地址 list1=[1,2,3] list2=list1 list1.append(4) print(id(list1)) print(id(list2))
4.字典(dict)
定義:在{}內(nèi),可存放多個(gè)值,以key-value存取,取值速度快,key是不可變,value可變
#字典dict dict1=({'age':18,'name':'peke'}) print(dict1) print(type(dict1)) #取值 字典名+[] 括號(hào)內(nèi)是對(duì)應(yīng)的值 print(dict1['age']) #2 存儲(chǔ)一個(gè)level:9到字典中 dict1['level']=9 print(dict1) #3 len print(len(dict1)) #4 print('name'in dict1)#值判斷key print('peke'not in dict1) #5 刪除 del dict1['level'] print(dict1) #6 key value items print(dict1.keys()) print(dict1.values()) print(dict1.items()) #7 循環(huán) for key in dict1: print(key) print(dict1[key]) #8 get print(dict1.get('age')) '''''' #print(dict1['sex'])#KeyError: 'sex' cant find dict1=({'age':18,'name':'peke'}) print(dict1.get('sex')) dict2=(dict1.get('sex','mele')) print(dict2)
2、文件處理
#文件處理
'''
寫文件
wt
讀文件
rt
追加寫文件
at
#指定字符編碼 以什么方式寫就得以什么方式打開
執(zhí)行代碼的過程
1 先啟動(dòng)python解釋器
2 把寫好的python文件加載到解釋器中
3 檢測(cè)python的語(yǔ)法 執(zhí)行代碼
'''
#參數(shù)1 文件的絕對(duì)路徑 #參數(shù)2 操作文件的模式 f=open('file.txt',mode='wt',encoding='utf-8') f.write('tank') f.close()#關(guān)閉操作系統(tǒng)文件資源 #2 讀文本文件 f=open('file.txt','r',encoding='utf-8') print(f.read()) f.close() #3 追加 f=open('file.txt','a',encoding='utf-8') f.write('\n 合肥學(xué)院') f.close()
上下文處理(利用with處理)
# 上下文處理 #with 可以管理open打開的文件 會(huì)在with執(zhí)行完后自動(dòng)調(diào)用close() 關(guān)閉文件 # with open() as f 句柄 with open('file.txt',mode='wt',encoding='utf-8') as f1: f.write('墨菲定律') with open('file.txt',mode='r',encoding='utf-8') as f2: print(f.read()) with open('file.txt',mode='a',encoding='utf-8') as f3: f.write('圍城') #f.close() #with 管理圖片 with open('cxk.jpg','rb') as f4: res=f.read() print(res) jpg=res with open('cxk.jpg','wb') as f5: f.write(jpg) #with管理多個(gè)文件 with open('cxk.jpg','rb') as f4,open('cxk.jpg','wb') as f5: res=f4.read() f5.write(res)
3、函數(shù)
#函數(shù)
#1 解決代碼冗余的問題
#2 使代碼結(jié)構(gòu)更清晰
#3 方便管理
先定義后調(diào)用
def 函數(shù)名(參數(shù)1,參數(shù)2,.....):
邏輯代碼
返回值(可有可無)
函數(shù)定義的三種方式
1 無參函數(shù)
2 有餐函數(shù)
3 空函數(shù) pass
#1 def login(): user=input('請(qǐng)輸入用戶名').strip() pwd=input('請(qǐng)輸入密碼').strip() if user=="cheng" and pwd=="123": print("login successful") else: print("login error") print(login) login()#調(diào)用 #2 def login(username,password): if username== "cheng" and password == "123": print("login successful") else: print("login error") login('cheng','123') #3 ''' ATM 1:提現(xiàn) 2:... ... .... .. .. ''' def register(): pass #在定義階段x,y為形參 #在調(diào)用階段x,y為實(shí)參
#關(guān)鍵數(shù)參數(shù) def func(x,y): print(x,y) func(x=100,y=10) #傳參數(shù)的時(shí)候不能多傳也不能少傳 #默認(rèn)參數(shù) ''' 在定義階段,為參數(shù)設(shè)置默認(rèn)值 ''' def foo(x=10,y=10): print(x,y) foo() foo(11,22) ''' 函數(shù)的嵌套定義 函數(shù)的對(duì)象 函數(shù)的名稱空間 在python中頂格寫的全部稱為全局名稱空間 在函數(shù)內(nèi)部定義的為局部名稱空間 python解釋器自帶的都稱為內(nèi)置名稱空間 加載順序 內(nèi)置—>全局—>局部 查找順序 局部->全局—>內(nèi)置 ''' # 1 在函數(shù)內(nèi)部定義函數(shù) def func1(): print('from func1') def func2(): print('form func2') # 2 函數(shù)對(duì)象 def f1(): pass def f2(): pass dic1={'1':f1,'2':f2} ch =input("請(qǐng)選擇功能") if ch=='1': print(dic1[ch]) dic1[ch]() elif ch=='2': print(dic1[ch]) dic1[ch]() # 3 函數(shù)的名稱空間 x=10 def func1(): print('from func1...') x=20 print(x) def func2(): print('form func2...')
三、總結(jié)
今天是python從零開始學(xué)習(xí)的第二天(day2),昨天把python的數(shù)據(jù)類型中的字符串學(xué)了,今天則把列表,字典,元組數(shù)據(jù)類型進(jìn)行學(xué)習(xí),然后又復(fù)習(xí)了文件操作和函數(shù)基本操作,其他有諸如with來處理上下文,省略了文件操作中的close()函數(shù),和函數(shù)的命名空間的區(qū)別.
浙公網(wǎng)安備 33010602011771號(hào)