一、作業內容
笨辦法學python 習題1-10
習題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.')
習題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.")
習題3:
print("I will now count my chickens:")
#print("")
print("Hens",25+30//6)
print("Rooters",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?")
#給出一個判斷5是否小于-2
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.")
#對判斷只能給出false和true兩種判斷
print("Is it greater?",5> -2)
print("Is it greater or equal?",5>= -2)
print("Is it less or equal?",5<= -2)
習題4:
cars =100
#有100輛車
space_in_a_car =4.0
#每輛車能運輸的次數
drivers =30
#司機= 30名
passengers =90
#乘客= 90名
cars_not_driven = cars - drivers
#沒有司機駕駛的汽車=總汽車-司機人數
cars_driven = drivers
#有司機的汽車= drivers
carpool_capacity = cars_driven * space_in_a_car
#可運輸乘客人次=有司機的汽車*每輛車的運輸次數
average_passengers_per_car = passengers / cars_driven
#平均每輛車上安排乘車人數=乘車人數/有司機的汽車數
print("There are",cars,"cars available.")
#有100輛車可供選擇
print("There are only",drivers,"drivers available")
#只有30個司機可用
print("There will be",cars_not_driven,"empty cars today.")
#今天會有70輛空車
print("We can transport",carpool_capacity,"people today.")
#今天我們可以運送120人
print("We have",passengers,"to carpool today.")
#我們今天有90人的乘車
print("We need to put about",average_passengers_per_car,"in each car")
#我們需要在每輛車上安排3人
#
習題5:
# -- coding: utf-8 --
name ='Zad 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))
#變量2個以上需要使用括號括起來
print("His teeth are usually %s depending on the coffee."% teeth)
#this line is tricky,try to get it exactly right
print("If I add %d, %d, and %d I get %d."% (age,height,weight,age + height + weight))
#%s字符串(采用str()的顯示)
#%r字符串(采用repr()的顯示)
#%c單個字符
#%b二進制整數
#%d十進制整數
#%i十進制整數
#%o八進制整數
#%x十六進制整數
#%e指數(基底寫為e)
#%E指數(基底寫為E)
#%f浮點數
#%F浮點數,與上相同
#%g指數(e)?或浮點數(根據顯示長度)
#%G指數(E)或浮點數(根據顯示長度)
#%%字符"%"
習題6:
x ="There are %d types of people."%10
bianry ="binary"
#把dot賦值給變量do_not.
do_not ="don't"
#把兩個格式化變量%時放入字符串those who know? and those中 并把字符串賦值給變量月,變量名分別為bianry,do_not
y ="those who know %s and those who %s."% (bianry,do_not)
#打印變量
print(x)
#打印變量
print(y)
#打印含有格式化變量%r的字符串I said: 變量名為x
print("I said %r ."% x)
print("I also said:'%s."%y)
#變量hilarious是一個布爾值False
hilarious =False
#把格式化的變量%r放入Isn't that joke so funny?并把字符串賦值給變量joke_evaluatio
joke_evaluation ="Isn't that joke so funny?! %r"
#打印joke_evaluatio,變量名是hilarious
print(joke_evaluation % hilarious)
#將字符串This is the left side of..。賦值給變量w
w ="This is the left side of..."
#將字符串a string with a right side賦值給變量
e ="a string with a right side."
#打印變量e和變量w
print(w + e)
習題7:
#打印Mary had a little lamb
print("Mary had a little lamb.")
#打印含有格式化變量%的字符串Its fleece was white as,變量為show
print("Its fleece was white as %s."%'show')
#打印And everywhere that Mary went
print("And everywhere that Mary went.")
#打印字符串,重復10次的結果
print("."*10)# what'd that do?
#將字符重賦值給變量end1
end1 ="c"
#將字符重賦值給變量end2
end2 ="h"
#將字符重賦值給變量end3
end3 ="e"
#將字符重賦值給變量end4
end4 ="e"
#將字符重賦值給變量end5
end5 ="s"
#將字符重賦值給變量end6
end6 ="e"
#將字符重賦值給變量end7
end7 ="B"
#將字符重賦值給變量end8
end8 ="u"
#將字符重賦值給變量end9
end9 ="r"
#將字符重賦值給變量end10
end10 ="g"
#將字符重賦值給變量end11
end11 ="e"
#將字符重賦值給變量end12
end12 ="r"
#將字符重賦值給變量end13
# watch that comma at the end. try removing it to see what happens
#打印end1到end6及end7到end12
print(end1 + end2 + end3 + end4 + end5 +end6)
print(end7 + end8 + end9 + end10 + end11 + end12)
習題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 % (
"I had this thing.",
"That you could type up right.",
"But it didn't.",
"So I said goodnight."
))
習題9:
#Here's some new strange stuff,remember type it exactly.
days ="Mon Tue Wed Thu Fri Sat Sun"
months ="Jan\nFeb\nMar\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 wa want,or 5,or 6.
""")
習題10:
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 alist:
\t* Cat food
\t* Fishies
\t* Catnip\n\t*Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)