閱讀《笨方法學python3》,歸納的知識點
相關代碼詳見github地址:https://github.com/BMDACMER/Learn-Python
習題1:安裝環境+練習 print函數使用 主要區別雙引號和單引號的區別
習題2:注釋符號#
習題3:運算符優先級,跟C/C++, Java類似
以下運算符優先級:從下往下以此遞增,同行為相同優先級
Lambda #運算優先級最低 邏輯運算符: or 邏輯運算符: and 邏輯運算符:not 成員測試: in, not in 同一性測試: is, is not 比較: <,<=,>,>=,!=,== 按位或: | 按位異或: ^ 按位與: & 移位: << ,>> 加法與減法: + ,- 乘法、除法與取余: *, / ,% 正負號: +x,-x
習題4:變量名+打印 介紹了以下這種形式的打印
print("There are", cars, "cars available.")
習題5:更多打印方式 比如 f"XXX"
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}")
習題6:繼續使用f"XX"打印
習題7:format打印方式,可采用end=‘ ’代替換行
print("Its fleece was white as {}.".format('show')) print(end1 + end2 + end3 + end4 + end5 + end6, end=' ') print(end7 + end8 +end9 + end10 +end11 + end12) 顯示如下: Its fleece was white as show. Cheese Burger
習題8:更多打印方式(這種打印方式見的較少)
formatter = '{} {} {} {}' print(formatter.format(1, 2, 3, 4)) print(formatter.format("one", "two", "three", "four")) print(formatter.format(True, False, False, True)) print(formatter.format(formatter, formatter, formatter, formatter)) # print("\n") print(formatter.format( "Try your", "Own text here", "Maybe a poem", "Or a song about fear" )) 顯示如下: 1 2 3 4 one two three four True False False True {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} Try your Own text here Maybe a poem Or a song about fear
習題9:多行打印,換行打印
小結:打印方式
1)print("There are", cars, "cars available.") # 變量名直接打印 2)print(f"Let's talk about {my_name}") # 帶有f"{}"的打印方式 3) 變量名+format() hilarious = False joke_evaluation = "Isn't that joke so funny?! {}" print(joke_evaluation.format(hilarious)) 4)print("Its fleece was white as {}.".format('show')) # 5)formatter = '{} {} {} {}' print(formatter.format(1, 2, 3, 4)) print(formatter.format("one", "two", "three", "four")) 6)print(''' There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want,or 5,or 6 ''') # 多行打印
習題10:轉義字符
習題11:介紹 input()函數,並使用到習題5介紹的打印方式
print("How old are you?", end=' ') age = input() print("How tall are you?", end=' ') height = input() print("How much do you weight?", end=' ') weight = input() print(f"So,you're {age} old,{height} tall and {weight} heavy.")
習題12:介紹input()函數 加提示符
age = input("How old are you?")
print("How old are you? {}".format(input())) # 先運行后面的在運行前面的提示
習題13: 參數、解包和變量
from sys import argv # read the WYSS section for how to run this script, first, second, third = argv print("The script is called:", script) print("Your first variable is:", first) print("Your second variable is:", second) print("Your third variable is:", third) # 注: 把argv中的東西取出,解包,將所有的參數依次復制給左邊的這些變量 >>python ex13.py first second third
練習14:提示與傳遞 主要講解 input()函數 配合輸入格式
from sys import argv ''' PS E:\dev\code\笨方法學python3> python ex14.py Zed Hi Zed,I'm the ex14.py script.' I'd like to ask ypu a few questions. Do ypu like me Zed? >yes Where do ypu live Zed? >yicheng What kind of computer do you have? >Dell ''' script , user_name = argv prompt = '>' print(f"Hi {user_name},I'm the {script} script.'") print("I'd like to ask you a few questions.") print(f"Do ypu like me {user_name}?") likes = input(prompt) print(f"Where do you live {user_name}?") lives = input(prompt) print("What kind of computer do you have?") computer = input(prompt) print(f''' Alright, so you said {likes} about liking me. You live in {lives}. Not sure where that is. And you have a {computer} computer. Nice. ''')
練習15:讀取文件
txt = open(filename) print(f"Here's your file {filename}:") print(txt.read()) txt.close()
練習16:讀寫文件
close:關閉文件。跟你的編輯器中的“文件”->“保存”是一個意思 read:讀取文件的內容 readline:只讀取文本文件中的一行 truncate:清空文件,請小心使用該命令 write('stuff'):將“stuff”寫入文件 seek(0): 將讀寫位置移動到文件開頭
練習17:繼續讀寫文件
in_file = open(from_file) indata = in_file.read() out_file = open(to_file,'w') out_file.write(indata)
練習18:函數
練習19:函數與變量
pytyon在定義函數的時候跟C/C++區別蠻大,python不需要定義參數類型,定義某一函數時 可參考如下:
def function(vailable1, vailable2,*):
練習20: 函數和文件
from sys import argv script, input_file = argv def print_all(f): print(f.read()) def rewind(f): f.seek(0)
# 帶有行號的打印內容 def print_a_line(line_count,f): print(line_count, f.readline()) current_file = open(input_file) # 接受的參數,打開文件 print("First let's print the whole file:\n") print_all(current_file) # 打印該文件 print("Now let's rewind, kind of like a tape.") rewind(current_file) # 將讀寫位置移動到文件開頭(第一個字符的前面一個位置) 方便后續讀取 如不執行該操作,后續打印為空 print("Let's print three lines:") current_line = 1 print_a_line(current_line,current_file) current_line = current_line + 1 print_a_line(current_line,current_file) current_line = current_line + 1 print_a_line(current_line, current_file)