1.這是python的語言特性,python先創(chuàng)建對(duì)象,在給變量賦值時(shí),不需要定義變量的名稱和類型,它實(shí)際是用變量引用對(duì)象。變量類型在給變量賦值時(shí)自動(dòng)聲明
2.原因類似變量無須聲明類型
3.python用下劃線作為變量前綴和后綴指定特殊變量,對(duì)解釋器有特殊意義,也是內(nèi)建標(biāo)識(shí)符所使用的特殊符號(hào),故一般避免用下劃線作為變量的開頭和結(jié)尾
4.python一行可以書寫多個(gè)語句,多個(gè)語句間用";"分隔。但是為了良好的編程風(fēng)格,不推薦這么做
5.python可以將一個(gè)語句分成多行書寫,行的末尾用反斜杠"/"標(biāo)識(shí)。python一行語句最好不超過80字符,所以若一個(gè)語句過長(zhǎng),可以分成多行書寫。其他情況下,還是保持一個(gè)物理行寫一個(gè)邏輯行吧
6.變量賦值
(a)賦值語句 x, y, z = 1, 2, 3 在x中賦值1,y中賦值2,z中賦值3
(b)執(zhí)行在z, x, y = y, z, x后,x值是3,y的值是1,z的值是2
7.標(biāo)識(shí)符
int32;PRintf;print;_print;this;self;__name__;bool;true;type;thisIsAVar;R_U_ready;Int;True;if;do;access
是合法的python標(biāo)識(shí)符
其中,print;if是python關(guān)鍵字。
8.
#!/user/bin/env python# -*- coding:utf-8 -*-'makeTextFile.py -- create text file'import osls = os.linesepwhile True: fname = raw_input("請(qǐng)輸入要?jiǎng)?chuàng)建的文件名".decode("utf-8").encode("gbk"))#判斷文件是否已經(jīng)存在,如果存在則重新輸入 if os.path.exists(fname): print u"錯(cuò)誤:'%s' 文件已經(jīng)存在!" % fname else: breakall = []print u"/n 請(qǐng)輸入每一行的內(nèi)容,在單行輸入英文狀態(tài)的'.'完成輸入。/n"while True: entry = raw_input("輸入沒一行的內(nèi)容吧:/n".decode("utf-8").encode("gbk"))#判斷輸入內(nèi)容是否是退出字符,若是則結(jié)束輸入 if entry == '.': break else: all.append(entry)#創(chuàng)建文件,并以寫入模式打開fobj = open(fname,'w')#將輸入的每行寫入文件中fobj.writelines(['%s%s' % (x,ls) for x in all])fobj.close()print u"成功創(chuàng)建!程序結(jié)束!"
9.操作系統(tǒng):Windows 10 64位
os.linesep:'/r/n'
10.① try-except-else 替代
#!/user/bin/env python# -*- coding:utf-8 -*-'makeTextFile.py -- create text file'import osls = os.linesepwhile True: fname = raw_input("請(qǐng)輸入要?jiǎng)?chuàng)建的文件名".decode("utf-8").encode("gbk"))#判斷文件是否已經(jīng)存在,如果存在則重新輸入 try: fobj = open(fname) except IOError: break else: print u"錯(cuò)誤:%s 文件已經(jīng)存在" % fname all = []print u"/n請(qǐng)輸入每一行的內(nèi)容,在單行輸入英文狀態(tài)的'.'完成輸入。/n"while True: entry = raw_input("輸入每一行的內(nèi)容吧:/n".decode("utf-8").encode("gbk"))#判斷輸入內(nèi)容是否是退出字符,若是則結(jié)束輸入 if entry == '.': break else: all.append(entry)#創(chuàng)建文件,并以寫入模式打開fobj = open(fname,'w')#將輸入的每行寫入文件中fobj.writelines(['%s%s' % (x,ls) for x in all])fobj.close()print u"成功創(chuàng)建!程序結(jié)束!"
②os.path.exists()替換try-except-else
#!/user/bin/env python# -*- coding:utf-8 -*-'readTextFile.py -- read and display text file'import os #用戶輸入要打開的文件fname = raw_input("請(qǐng)輸入文件名:")print #嘗試以閱讀模式打開文件if os.path.exists(fname): fobj = open(fname,'r') for eachline in fobj: print eachline, fobj.close()else: print "錯(cuò)誤:文件不存在!"
11.
#!/user/bin/env python# -*- coding:utf-8 -*-"readTextFile.py -- read and display text file"#用戶輸入要打開的文件fname = raw_input("Enter file name".decode("utf-8").encode("gbk"))print #嘗試以閱讀模式打開文件try: fobj = open(fname,'r')except IOError,e: print "*** file open error:",eelse:#打印出文件內(nèi)容 for eachline in fobj: print eachline.strip('/n') fobj.close()
給自己的提示:字符串的strip()方法用于移除字符串頭尾指定的字符,它返回移除指定字符后的新字符串
eg:
str = "11111Let's see how the strip() works.1111111"print str.strip('1')
輸出:
Let's see how the strip() works.
12.修改后程序如下
#!user/bin/env python# -*- coding:utf-8 -*-'read_or_makeTextFile.py -- read and display text file,or create text file 'import osls = os.linesepwhile True: choise = raw_input("打開文件請(qǐng)輸入1,創(chuàng)建文件輸入2,退出輸入3:".decode("utf-8").encode("gbk")) if not choise.isdigit(): print u"請(qǐng)輸入數(shù)字" elif int(choise) == 1: fname = raw_input("請(qǐng)輸入文件名:".decode("utf-8").encode("gbk")) print #嘗試以閱讀模式打開文件 if os.path.exists(fname): fobj = open(fname,'r') for eachline in fobj: print eachline, fobj.close() else: print "錯(cuò)誤:文件不存在!" elif int(choise) == 2: while True: fname = raw_input("請(qǐng)輸入要?jiǎng)?chuàng)建的文件名".decode("utf-8").encode("gbk")) #判斷文件是否已經(jīng)存在,如果存在則重新輸入 if os.path.exists(fname): print u"錯(cuò)誤:'%s' 文件已經(jīng)存在!" % fname else: break all = [] print u"/n請(qǐng)輸入每一行的內(nèi)容,在單行輸入英文狀態(tài)的'.'完成輸入。/n" while True: entry = raw_input("輸入每一行的內(nèi)容吧:/n".decode("utf-8").encode("gbk")) #判斷輸入內(nèi)容是否是退出字符,若是則結(jié)束輸入 if entry == '.': break else: all.append(entry) #創(chuàng)建文件,并以寫入模式打開 fobj = open(fname,'w') #將輸入的每行寫入文件中 fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.close() print u"成功創(chuàng)建!程序結(jié)束!" elif int(choise) == 3: print u"程序?qū)⑼顺?/span>" break else: print u"請(qǐng)重新輸入"
13.修改后程序如下
1 #!user/bin/env python 2 # -*- coding:utf-8 -*- 3 4 'read_or_makeTextFile.py -- read and display text file,or create text file ' 5 6 import os 7 ls = os.linesep 8 print os.getcwd() 9 while True:10 choise = raw_input("打開文件請(qǐng)輸入1,創(chuàng)建文件輸入2,編輯文件輸入3,退出輸入4:".decode("utf-8").encode("gbk"))11 12 if not choise.isdigit():13 print u"請(qǐng)輸入數(shù)字"14 elif int(choise) == 1:15 fname = raw_input("請(qǐng)輸入文件名:".decode("utf-8").encode("gbk"))16 17 print 18 19 #嘗試以閱讀模式打開文件20 21 if os.path.exists(fname):22 fobj = open(fname,'r')23 for eachline in fobj:24 print eachline,25 fobj.close()26 else:27 print u"錯(cuò)誤:文件不存在!"28 elif int(choise) == 2:29 while True:30 fname = raw_input("請(qǐng)輸入要?jiǎng)?chuàng)建的文件名".decode("utf-8").encode("gbk"))31 #判斷文件是否已經(jīng)存在,如果存在則重新輸入32 if os.path.exists(fname):33 print u"錯(cuò)誤:'%s' 文件已經(jīng)存在!" % fname34 else:35 break36 all = []37 print u"/n請(qǐng)輸入每一行的內(nèi)容,在單行輸入英文狀態(tài)的'.'完成輸入。/n"38 39 while True:40 entry = raw_input("輸入每一行的內(nèi)容吧:/n".decode("utf-8").encode("gbk"))41 #判斷輸入內(nèi)容是否是退出字符,若是則結(jié)束輸入 42 if entry == '.':43 break44 else:45 all.append(entry)46 #創(chuàng)建文件,并以寫入模式打開47 fobj = open(fname,'w')48 #將輸入的每行寫入文件中49 fobj.writelines(['%s%s' % (x,ls) for x in all])50 fobj.close()51 print u"成功創(chuàng)建!程序結(jié)束!"52 elif int(choise) == 3:53 fname = raw_input("請(qǐng)輸入文件名:".decode("utf-8").encode("gbk"))54 print55 56 #嘗試以讀寫模式("r+")模式打開文件57 if os.path.exists(fname):58 fobj = open(fname)59 count = len(fobj.readlines())60 fobj.close()61 lines = []62 for i in range(1,count):63 line = raw_input("請(qǐng)輸入修改后的每行的內(nèi)容:".decode("utf-8").encode("gbk"))64 lines.append(line)65 while True:66 save = raw_input("保存修改輸入1,不保存輸入2:".decode("utf-8").encode("gbk"))67 if not save.isdigit():68 print u"請(qǐng)輸入數(shù)字1保存或者2不保存!!"69 elif int(save) == 1:70 fobj = open(fname,'w')71 fobj.writelines(['%s%s' % (x,ls) for x in lines])72 fobj.close73 print u"修改成功!"74 break75 elif int(save) == 2:76 break77 else:78 print u"錯(cuò)誤!文件不存在!"79 80 elif int(choise) == 4:81 print u"程序?qū)⑼顺?/span>"82 break83 else:84 print u"請(qǐng)重新輸入"
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注