Python變量學(xué)習(xí)

2018-06-20 10:38 更新

1.變量介紹

變量來源于數(shù)學(xué),是計(jì)算機(jī)語言中能儲(chǔ)存計(jì)算結(jié)果或能表示值的抽象概念,變量可以通過變量名訪問,在指令式語言中,變量通常是可變的。

其中Teacher是變量名,zhangzexiang是變量值;

這句話意思是,把變量值zhangzexiang儲(chǔ)存在變量名Teacher中。

  1. Teacher="zhangzexiang"
  2. print (Teacher)

2.用變量進(jìn)行計(jì)算

2.1 這個(gè)就是第1章 打印輸出 第2節(jié) 引號規(guī)則里面說的給變量名賦值。

  1. a=6
  2. b=4
  3. print(a+b)

2.2 我們也可以將a+b的計(jì)算結(jié)果用c來代表,最后直接打印輸出c。

  1. a=6
  2. b=4
  3. c=a+b
  4. print(c)

2.3 不止是數(shù)字,字母也是可以進(jìn)行變量名賦值的。

  1. a="zhang"
  2. b="zexiang"
  3. print(a+b)

2.4 自增自減

  1. a=7
  2. a=a-1
  3. print (a)

3.大量練習(xí)

  1. cars=100 #汽車數(shù)量
  2. space_in_a_car=4.0 #汽車空間
  3. drivers=30 #司機(jī)數(shù)量
  4. passengers=90 #拼車數(shù)量
  5. cars_not_driven=cars-drivers #空車數(shù)量=汽車數(shù)量-司機(jī)數(shù)量
  6. cars_driven=drivers #汽車驅(qū)動(dòng)=司機(jī)數(shù)量
  7. carpool_capacity=cars_driven*space_in_a_car #拼車能力=汽車驅(qū)動(dòng)*汽車空間
  8. average_passengers_per_car=passengers/cars_driven #平均每輛車的乘客=拼車數(shù)量/汽車驅(qū)動(dòng)
  9. print("There are",cars,"cars available.") #有100輛汽車可用。
  10. print("There are only",drivers,"drivers available.")#目前只有30名司機(jī)。
  11. print("There will be",cars_not_driven,"empty cars today.")#今天將有70輛空車。
  12. print("We can transport",carpool_capacity,"people today.")#我們今天可以運(yùn)送120.0人。
  13. print("We have",passengers ,"to carpool today.")#今天我們有90個(gè)拼車。
  14. print("We need to put about",average_passengers_per_car,"in each car")#我們需要在每輛車?yán)镅b3.0個(gè)座位

  1. my_name='Zed A.Shaw'
  2. my_age=35 # not a lie
  3. my_height=74 # inches
  4. my_weight=180 #Lbs
  5. my_eyes="Blue"
  6. my_teeth="White"
  7. my_hair="Brown"
  8. print("Let's talk about %s."%my_name)
  9. print("He's %d inches tall."%my_height)
  10. print("He's %d pounds heavy."%my_weight)
  11. print("Actually that's not too heavy.")
  12. print("He's got %s eyes and %s hair."%(my_eyes,my_hair))
  13. print("His teeth are usually %s depending on the coffee."%my_teeth) # this line is tricky , try to get it exactly right
  14. print("If I add %d,%d,%d I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight))
  15. '''
  16. 讓我們來討論一下Zed A.Shaw。
  17. 他的74英寸高。
  18. 他是180磅重。
  19. 實(shí)際上,這并不太重。
  20. 他有藍(lán)色的眼睛和棕色的頭發(fā)。
  21. 他的牙齒通常是白色的,這取決于咖啡。
  22. 如果加35,74,180得到289。
  23. '''
  1. x=("There are %d types of people."%10)
  2. binary="binary"
  3. do_not="don't"
  4. y=("Those who know %s and those who %s."%(binary,do_not))
  5. print(x)
  6. print(y)
  7. print("I said: %r." %x)
  8. print("I also said:%s." %y)
  9. hilarious=False #滑稽的
  10. joke_evaluation=("Isn't that joke so funny?! %r ") #笑話評價(jià)
  11. print(joke_evaluation%hilarious)
  12. w=("This is the left side of")
  13. e=(" a string with a right side.")
  14. print(w+e)
  15. '''
  16. There are 10 types of people.
  17. Those who know binary and those who don't.
  18. I said: 'There are 10 types of people.'.
  19. I also said:Those who know binary and those who don't..
  20. Isn't that joke so funny?! False
  21. This is the left side of a string with a right side.
  22. 10種類型的人。
  23. 知道二進(jìn)制的和不知道二進(jìn)制的。
  24. 我說:“有10種類型的人。”
  25. 我也說過:那些知道二進(jìn)制和不知道二進(jìn)制的人。
  26. 這個(gè)笑話好笑嗎?假
  27. 這是一個(gè)有右側(cè)的弦的左邊。
  28. '''

  1. print("Mary had a little lamb.")
  2. print("Its fleece was white as %s." %'snow')
  3. print("And everywhere that Mary went.")
  4. print("."*10)# what'd that do?
  5. end1="C"
  6. end2="h"
  7. end3="e"
  8. end4="e"
  9. end5="s"
  10. end6="e"
  11. end7="B"
  12. end8="u"
  13. end9="r"
  14. end10="g"
  15. end11="e"
  16. end12="r"
  17. ## watch that comma at the end. try removing it to see what happens
  18. print(end1+end2+end3+end4+end5+end6,end=" ")
  19. print(end7+end8+end9+end10+end11+end12)
  20. '''
  21. Mary had a little lamb.
  22. Its fleece was white as snow.
  23. And everywhere that Mary went.
  24. ..........
  25. Cheese Burger
  26. 瑪麗有只小羊羔。
  27. 它的羊毛像雪一樣白。
  28. 和瑪麗去的地方。
  29. ..........
  30. 奶酪漢堡。
  31. '''

  1. formatter=("%r %r %r %r") #格式器
  2. print (formatter %(1,2,3,4))
  3. print(formatter%("one","two","three","four"))
  4. print(formatter%(True,False,False,True))
  5. print(formatter%(formatter,formatter,formatter,formatter))
  6. print(formatter%("I had this thing.",
  7. "That you could type up right.",
  8. "But it did't sing.",
  9. "So I said goodnight."))
  10. '''
  11. 1 2 3 4
  12. 'one' 'two' 'three' 'four'
  13. True False False True
  14. '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
  15. 'I had this thing.' 'That you could type up right.' "But it did't sing." 'So I said goodnight.'
  16. print字符打錯(cuò)了, 字符串換行的時(shí)候要加, 與""號。
  17. '''

  1. #Exercise
  2. #Here's some new strange stuff,remember type it exactly.
  3. days="Mon Tue Wed Thu Fri Sat Sun"
  4. months="Jan\nFeb\nMar\nApr\nMay\nJun\njul\nAug"
  5. print("Here are the days:",days)
  6. print("Here are the months:",months)
  7. print("""
  8. There's something going on Here.
  9. With the three double-quotes.
  10. We'll be able to type as much as we like.
  11. Even 4 lines if we want , or 5 , or 6.""")
  12. '''
  13. Here are the days: Mon Tue Wed Thu Fri Sat Sun
  14. Here are the months:
  15. Jan
  16. Feb
  17. Mar
  18. Apr
  19. May
  20. Jun
  21. jul
  22. Aug
  23. There's something going on Here.
  24. With the three double-quotes.
  25. We'll be able to type as much as we like.
  26. Even 4 lines if we want , or 5 , or 6.
  27. 現(xiàn)在是這樣的日子:Mon Tue Wed Thu Fri Sat Sun
  28. 這里是幾月:
  29. 1
  30. 2
  31. 3
  32. 4
  33. 5
  34. 6
  35. 7
  36. 8
  37. 這里有一些東西。
  38. 三個(gè)雙引號。
  39. 我們可以輸入我們喜歡的東西。
  40. 即使是4行,也可以是56。
  41. '''

  1. ## Exercise
  2. tabby_cat="\tI'm tabbed in."
  3. persian_cat="I'm split\non a line."
  4. backslash_cat="I'm\\a\\cat."
  5. fat_cat="""
  6. I'll do a list:
  7. \t*Cat food
  8. \t*Flishies
  9. \t*Catnip\n\t*Grass
  10. """
  11. print(tabby_cat)
  12. print(persian_cat)
  13. print(backslash_cat)
  14. print(fat_cat)
  15. '''
  16. I'm tabbed in.
  17. I'm split
  18. on a line.
  19. I'm\a\cat.
  20. I'll do a list:
  21. *Cat food
  22. *Flishies
  23. *Catnip
  24. *Grass
  25. '''

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號