變量來源于數(shù)學(xué),是計(jì)算機(jī)語言中能儲(chǔ)存計(jì)算結(jié)果或能表示值的抽象概念,變量可以通過變量名訪問,在指令式語言中,變量通常是可變的。
其中Teacher
是變量名,zhangzexiang
是變量值;
這句話意思是,把變量值zhangzexiang
儲(chǔ)存在變量名Teacher
中。
Teacher="zhangzexiang"
print (Teacher)
2.1 這個(gè)就是第1章 打印輸出 第2節(jié) 引號規(guī)則
里面說的給變量名賦值。
a=6
b=4
print(a+b)
2.2 我們也可以將a+b的計(jì)算結(jié)果用c來代表,最后直接打印輸出c。
a=6
b=4
c=a+b
print(c)
2.3 不止是數(shù)字,字母也是可以進(jìn)行變量名賦值的。
a="zhang"
b="zexiang"
print(a+b)
2.4 自增自減
a=7
a=a-1
print (a)
cars=100 #汽車數(shù)量
space_in_a_car=4.0 #汽車空間
drivers=30 #司機(jī)數(shù)量
passengers=90 #拼車數(shù)量
cars_not_driven=cars-drivers #空車數(shù)量=汽車數(shù)量-司機(jī)數(shù)量
cars_driven=drivers #汽車驅(qū)動(dòng)=司機(jī)數(shù)量
carpool_capacity=cars_driven*space_in_a_car #拼車能力=汽車驅(qū)動(dòng)*汽車空間
average_passengers_per_car=passengers/cars_driven #平均每輛車的乘客=拼車數(shù)量/汽車驅(qū)動(dòng)
print("There are",cars,"cars available.") #有100輛汽車可用。
print("There are only",drivers,"drivers available.")#目前只有30名司機(jī)。
print("There will be",cars_not_driven,"empty cars today.")#今天將有70輛空車。
print("We can transport",carpool_capacity,"people today.")#我們今天可以運(yùn)送120.0人。
print("We have",passengers ,"to carpool today.")#今天我們有90個(gè)拼車。
print("We need to put about",average_passengers_per_car,"in each car")#我們需要在每輛車?yán)镅b上3.0個(gè)座位
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,%d I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight))
'''
讓我們來討論一下Zed A.Shaw。
他的74英寸高。
他是180磅重。
實(shí)際上,這并不太重。
他有藍(lán)色的眼睛和棕色的頭發(fā)。
他的牙齒通常是白色的,這取決于咖啡。
如果加35,74,180得到289。
'''
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 ") #笑話評價(jià)
print(joke_evaluation%hilarious)
w=("This is the left side of")
e=(" a string with a right side.")
print(w+e)
'''
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said:Those who know binary and those who don't..
Isn't that joke so funny?! False
This is the left side of a string with a right side.
有10種類型的人。
知道二進(jìn)制的和不知道二進(jìn)制的。
我說:“有10種類型的人。”
我也說過:那些知道二進(jìn)制和不知道二進(jìn)制的人。
這個(gè)笑話好笑嗎?假
這是一個(gè)有右側(cè)的弦的左邊。
'''
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,end=" ")
print(end7+end8+end9+end10+end11+end12)
'''
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
瑪麗有只小羊羔。
它的羊毛像雪一樣白。
和瑪麗去的地方。
..........
奶酪漢堡。
'''
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 did't sing.",
"So I said goodnight."))
'''
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it did't sing." 'So I said goodnight.'
print字符打錯(cuò)了, 字符串換行的時(shí)候要加, 與""號。
'''
#Exercise
#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.""")
'''
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months:
Jan
Feb
Mar
Apr
May
Jun
jul
Aug
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.
現(xiàn)在是這樣的日子:Mon Tue Wed Thu Fri Sat Sun
這里是幾月:
1月
2月
3月
4月
5月
6月
7月
8月
這里有一些東西。
三個(gè)雙引號。
我們可以輸入我們喜歡的東西。
即使是4行,也可以是5或6。
'''
## Exercise
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*Flishies
\t*Catnip\n\t*Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
'''
I'm tabbed in.
I'm split
on a line.
I'm\a\cat.
I'll do a list:
*Cat food
*Flishies
*Catnip
*Grass
'''
更多建議: