一、作業內容
Mr_Cxy發布作業很久了,出了個差感覺已經被甩十萬八千里了,希望組織不要拋棄我,我也不放棄自己,開始追趕,本部分為第一周作業 (笨辦法學 Python 習題0-10),希望>Mr_Cxy給個鼓勵 。
二、作業代碼
- 習題0:安裝程序已完成
- 習題1:第一個程序
# 習題1:第一個程序
print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print("Yay! Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.\n') # 最后再換行
'''
Summary:
1、使用英文輸入狀態下單引號和雙引號輸出文本
2、文本中出現單引號則用雙引號輸出,反之亦然
3、文本中同時出現單引號和雙引號,則分別標出如print("I'd much rather you"'"not"''.')
'''
# 加分題1:讓你的腳本再多打印一行(我理解每一句后面多一行)
print('Hello World!\n')
print('Hello Again\t')
print('I like typing this.\n')
print('This is fun.\n')
print('Yay! Printing.\r')
print("I'd much rather you 'not'.\n")
print('I "said" do not touch this.\n')
'''
Summary:
1、文本輸出中\n表示回車后再多空出一行
2、文本輸出中\r和\t則只回車換行并不多空出一行
'''
# 加分題2:讓你的腳本只打印一行(我理解所有句子間無換行符)
print('Hello World!', end=' ')
print('Hello Again', end=' ')
print('I like typing this.', end=' ')
print('This is fun.', end=' ')
print('Yay! Printing.', end=' ')
print("I'd much rather you 'not'.", end=' ')
print('I "said" do not touch this.\n') # 最后再換行
'''
Summary:
1、end=' '表示結尾不換行(因為默認換行的)
'''
# 加分題3:如上所示,#號為單行注釋
'''
Summary:
1、單行注釋用#
2、多行注釋用三個單引號和三個雙引號均可
'''
- 習題2:注釋和井號
# 習題2:注釋和井號
# A comment,this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.") # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print("This won't run.")
print("This will run.")
'''
Summary:
1、#號為單行注釋符,英文稱作octothorpe或者pound character
2、使用#號時容易忽略的一點就是后面有一個空格
'''
- 習題3:數字和數學計算
# 習題3:數字和數學計算
print("I will now count my chickens:")
print("Hens",25+30/6)
print("Roosters",100-25*3%4)
print("Now I will count the eggs:")
print(3+2+1-5+4%2-1/4+6) # 里面不能有引號否則是輸出為文本而不是運算結果
print("Is it true that 3+2<5-7?")
print(3+2<5-7)
print("What is 3+2?",3+2)
print("What is 5-7?",5-7)
print("Oh,that's why it's False.")
print("How about some more.")
print("Is it greater?",5>=-2)
print("Is it less or equal?",5<=-2)
'''
Summary:
1、運算符含義:/整除、%取余數、*乘法
2、我這邊似乎就是浮點運算,部分結果與書中不一致
3、浮點數挺復雜的,現在有點一知半解
4、犯錯點:做運算時print里面加了引號發現輸出文本無法運算
5、做判斷運算時輸出的為ture或者false
'''
- 習題4:變量(variable)和命名
# 習題4:變量(variable)和命名
cars=100
space_in_a_car=4.0
drivers=30
passengers=90
cars_not_driven=cars-drivers
cars_driven=drivers
carpool_capacity=cars_driven*space_in_a_car
average_passengers_per_car=passengers/cars_driven
print("There are",cars,"cars available.")
print("There are only",drivers,"drivers available,")
print("There will be",cars_not_driven,"empty cars today.")
print("We can transport",carpool_capacity,"people today.")
print("We have",passengers,"to carpool today.")
print("We need to put about",average_passengers_per_car,"in each car.")
'''
Summary:
1、變量定義一定是一個整體的單詞,多個詞組要用下劃線連接成一個整體
2、變量的運行要按邏輯走,里面有些指針的思想
3、做了幾次作業已經感受到Python的Visualization做得很不錯,細細總結不同文本顏色的差異
4、輸出中注意文本與變量的關系和表示方法
5、比起以前C++好多了,Python中的變量只要定義過可以下拉框選,免得寫錯
'''
# 加分習題:即這個car_pool_capacity未定義,細細看car和pool之間多了一個下劃線
- 習題5:更多的變量和打印
# 習題5:更多的變量和打印
my_name="Zed A. Shaw"
my_age=35 # not a lie
my_height=74 # inches
my_weight=180 # lbs
my_eyes="Blue"
my_teeth="White"
my_hair="Brown"
print("Let's talk about %s."% my_name)
print("He's %d inches tall."% my_height)
print("He's %d pounds heavy."%my_weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair."%(my_eyes,my_hair))
print("His teeth are usually %s depending on the coffee."% my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d.\n"%(my_age,my_height,my_weight ,my_age+my_height+my_weight))
# 加分習題1:去掉“my_"
name="Zed A. Shaw"
age=35 # not a lie
height=74 # inches
weight=180 # lbs
eyes="Blue"
teeth="White"
hair="Brown"
print("Let's talk about %s."% name)
print("He's %d inches tall."% height)
print("He's %d pounds heavy."%weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair."%(eyes,hair))
print("His teeth are usually %s depending on the coffee."% teeth)
print("If I add %d, %d, and %d I get %d.\n"%(age,height,weight ,age+height+weight))
'''
Summary:
1、使用這個%叫格式化字符;
2、如上面代碼所示,常用的%s表示字符串,%f表示浮點數,%d表示十進制整數,%r所有字符串,當然還有更多
3、格式化字符的使用格式當然還可以更復雜,遠不止上面那么簡單,如%04d表示寬度為4
'''
# 加分習題4:英寸和磅轉換成厘米和千克,其中1英寸=2.54厘米,1磅=0.45392千克
inches_height=74 # inches
lbs_weight=180 # lbs
cm_height=inches_height*2.54
kg_weight=lbs_weight*0.45392
print("%s's height is %f, and weight is %r"%(name,cm_height,kg_weight))
# 嘗試了下%f和%r,height輸出結果%r為187.96,而%f輸出結果為187.960000
- 習題6:字符串(string)和文本
# 習題6:字符串(string)和文本
x="There are %d types of people."%10
binary="binary"
do_not="don't"
y="Those who know %s and those who %s."%(binary,do_not)
print(x)
print(y)
print("I said: %r."%x)
print("I also said: '%s'."%y)
hilarious=False
joke_evaluation="Isn't that joke so funny?!%r"
print(joke_evaluation % hilarious)
w="This is the left side of..."
e="a string with a right side."
print(w+e)
'''
Summary:
1、習題4和習題5、6都有數字,兩種表達方式都可以,但注意輸出時格式的不一樣
2、定義變量里也可以出現格式化字符
3、字符串運算(+、*)很神奇,減法和除法不好用,比如截取前面幾個字符的操作怎么搞
'''
- 習題7:更多打印
# 習題7:更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
print("."*10) # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print(end1+end2+end3+end4+end5+end6,end7+end8+end9+end10+end11+end12) # 逗號充當了空格類似與end=''
print(end1+end2+end3+end4+end5+end6), print(end7+end8+end9+end10+end11+end12)
'''
Summary:
1、print(end1+end2+end3+end4+end5+end6,end7+end8+end9+end10+end11+end12)輸出為Cheese Burger
2、print(end1+end2+end3+end4+end5+end6), print(end7+end8+end9+end10+end11+end12)輸出結果中兩個單詞之間有回車符,print間默認會有回車符
3、因為版本問題顯示結果不太一樣
4、哦,今天還有一個收獲,就是空格的輸入,要注意看提示,前面練習都沒有注意
'''
- 習題8:打印,打印
# 習題8:打印,打印
formatter="%r %r %r %r"
print(formatter % (1, 2, 3, 4))
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
'''
Summary:
1、第四個print倒是沒想到的,開始以為是一個死循環
2、段落輸入這個與以前C++不一樣,Python去一個個對齊括號時發現PEP8波浪警告
'''
- 習題9:打印,打印,打印
# 習題9:打印,打印,打印
# Here's some new strange stuff, remember type it exactly.
days="Mon Tue Wed Thu Fri Sat Sun"
months="Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days:",days)
print("Here are the months:",months)
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.
""")
'''
Sumary:
1、"""段落輸出,注意對比ex8中是每一段都需要引號標出來
'''
- 習題10:那是什么?
# 習題10:那是什么?
print("I am 6'2\"tall.") # 將字符串中的雙引號轉義
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
'''
Summary:
1、"""段落輸出用\'''效果是一樣的
2、\\輸出為\
3、其他常用轉義字符\n換行,\t橫向制表符,\r回車,\f換頁
'''
# 加分習題4 將轉義序列和格式化字符串放一起,并比較%r和%s
x="I like using \"Python\"!"
y="Today I finished %d homeworks."%5
print("output:%r"%x) # %r輸出的內容多了兩個單引號
print("output:%s"%y)
三、學習總結
- 每一個小知識點的總結都在代碼里用段落注釋標出來“Summary”所示標出來
- 每一個小的知識點其實學深入了需要掌握的東西還是很多的
- 跟著抄題做了這么多練習有的卻是需要好好總結,雖然代碼中Summary也做了部分總結,比如段落字符串輸出可以為每一段輸出加引號,也可以整體用三個引號表示;比如字符串輸出需要涉及到變量時,可以“文本”+變量+“文本”這種模式,也可以直接用格式化字符“文本%r文本”這種模式,要注意總結差異
- 注意從一開始養成好的習慣和追求完美的細節,比如空格這一細節在前面幾道題并沒有注意,后面發現雖不報錯但會提醒才意識到,標準化的理念要一開始就要樹立