《笨辦法學(xué) Python》開啟練習(xí)
1.第一個(gè)程序
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.')
此程序打印字符串傲隶,字符串單雙引號(hào)的包含要錯(cuò)開使用,如果最外層是雙引號(hào)窃页,里層要用單引號(hào)跺株,若外層是單引號(hào),里層可要雙引號(hào)脖卖。
2.注釋和井號(hào)
代碼的#中文為井號(hào)乒省,英文為 octothorpe 或者pound character。
程序中的#號(hào)可以用于注釋畦木,也可以用注解的方式將這段代碼臨時(shí)禁用袖扛。
3.數(shù)字和數(shù)學(xué)運(yùn)算
#打印句子 I will now count my chicken:
print(" I will now count my chicken:")
#打印Hen和25+30/6計(jì)算的值
print("Hens",25+30/6)
print("Roosters",100 - 25 * 3 % 4)
print("Now I will count the eggs:")
#打印引號(hào)中的句子
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.")
#打印句子:Is it greater?,并且打印5>-2判斷的結(jié)果(False十籍、True)
print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
print("How about some more.")
print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
在數(shù)學(xué)運(yùn)算整數(shù)乘除時(shí)運(yùn)算不夠精確蛆封,得出結(jié)果沒有小數(shù)部分,僅有整數(shù)妓雾,而浮點(diǎn)數(shù)能準(zhǔn)確得出結(jié)果娶吞。
#浮點(diǎn)數(shù)數(shù)學(xué)運(yùn)算
print("100/2.8=",100/2.8)
4.變量和命名
#定義變量名cars、pace_in_a_car 械姻、drivers 和passengers 并分別賦值給變量名
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
#變量名進(jìn)行數(shù)學(xué)運(yùn)算并且賦值給新的變量名
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.")
如果在運(yùn)算過程中使用未定義的變量名將出錯(cuò)妒蛇,在使用變量名之前先定義變量名。此例子中的4.0屬于浮點(diǎn)數(shù)楷拳,可以提高數(shù)值精度绣夺,得出準(zhǔn)確的值。
5.更多的變量和打印
#定義特征變量名并賦值
name='Zed D.Helon'
age=30
height=70 #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." % (age,
height, weight, age + height +weight))
打印變量名的值欢揖,有多個(gè)值打印時(shí)要用括號(hào)()陶耍。
print("使用變量將英寸和磅轉(zhuǎn)換成厘米和千克:")
inch=180
pound=65.5
cm=inch*2.54
kg=pound*0.45392
print("If his %d inches tall,about %d cm tall."%(inch,cm))
print("If his %d pounds heavy,about %d kg heavy."%(pound,kg))
其中,1英寸=2.54厘米她混;1鎊=0.45392千克
python字符串格式化符號(hào):
符號(hào) | 描述 |
---|---|
%c | 格式化字符及其ASCII碼 |
%s | 格式化字符串 |
%d | 格式化整數(shù) |
%f | 格式化浮點(diǎn)數(shù)字烈钞,可指定小數(shù)點(diǎn)后的精度 |
%o | 格式化無符號(hào)八進(jìn)制數(shù) |
%x | 格式化無符號(hào)十六進(jìn)制數(shù) |
%X | 格式化無符號(hào)十六進(jìn)制數(shù)(大寫) |
%e | 用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù) |
%p | 用十六進(jìn)制數(shù)格式化變量的地址 |
總結(jié)
1.使用變量名前先要定義變量名泊碑,不存在的變量名、大小寫字母錯(cuò)誤和寫錯(cuò)的變量名會(huì)報(bào)錯(cuò)毯欣。
2.打印字符和變量組合時(shí)候馒过,有多個(gè)變量打印要用括號(hào)()包含在里面。
3.字符串格式化符號(hào)要對(duì)應(yīng)使用酗钞,后期在字符串方面需要多練習(xí)腹忽,尤其是字符串函數(shù)方面的。