octothorpe、poundcharacter 指#,python中表註釋
數字和數學計算
• +plus 加號
• -minus 減號
• /slash 斜槓
• *asterisk 星號
• %percent 百分號
• <less-than 小於號
• >greater-than 大於號
•<= less-than-equal 小於等於號
•>= greater-than-equal 大於等於號
小測試. ex3.py
print "I willnow count my chickens:"
print"Henss", 25 + 30 / 6
print"Roosters", 100 - 25 * 3 % 4
print "Now Iwill count the eggs:"
print 3 + 2 + 1 - 5+ 4 % 2 - 1 / 4 + 6
print "Is ittrue that 3 + 2 < 5 - 7?"
print 3 + 2 < 5-7
print "What is3 +2?", 3 + 2
print "What is5 -7?", 5 - 7
print "Oh,that's why it's false."
print "Howabout some more."
print "Is itgreater?",5 > -2
print "Is itgreater or equal?", 5 >= -2
print "Is itless or equal?", 5 <=-2
輸出:
加入自己註釋。默認情況下python不認中文
可以在頭部添加# -*-coding: utf -8 -*-
# -*- coding: utf-8-*-
#我要數小雞了
print "I willnow count my chickens:"
# 輸出 Henss ,25+5=30
# 輸出 Roosters ,25 *3 % 4 是取餘,75/4=18.....3,100-3=97
print"Henss", 25 + 30 / 6
print"Roosters", 100 - 25 * 3 % 4
print "Now Iwill count the eggs:"
# 輸出 3 + 2 + 1 - 5 +4 % 2 - 1 / 4 + 6 =1 + 0 - 1 / 4 +6 =1 + 0 + 0 + 6 = 7 ,1 / 4 爲 0是因爲前面運算都是整型,所以忽略後面小數點爲0
print 3 + 2 + 1 - 5+ 4 % 2 - 1 / 4 + 6
# 輸出 3 + 2 是否 小於 5 -7 ? ,結果錯誤輸出false 錯誤
print "Is ittrue that 3 + 2 < 5 - 7?"
print 3 + 2 < 5-7
# 3 + 2 和 3 + 2的關係是什麼?
print "What is3 +2?", 3 + 2
# 5 - 7 和 5 - 7的關係是什麼?
print "What is5 -7?", 5 - 7
print "Oh,that's why it's false."
print "Howabout some more."
# 輸出求問? 它是否更大? 5 和-2 相比
print "Is itgreater?",5 > -2
# 相似的,5 是否大於 或者等於 -2 ,結果顯然,5 是大於或等於 -2 的,結果爲true.
print "Is itgreater or equal?", 5 >= -2
print "Is itless or equal?", 5 <=-2
浮點型測試
print"Henss" , 25 + 30 / 6
print"table" , 1 / 4
print"table" , float(1) / float(4)
對於整型情況下
print "Is it greater or equal?",5>=-2
print "Is it equal?",5/4>=
3 /
2
print "Is it equal?",5/4<=
3 /
2
變量(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_passsenger_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,"emptycars today."
print "we can transport", carpool_capacity,"peopletoday."
print "we have",passengers,"to carpool today."
print "we need to put about", average_passsenger_per_car,"in each car."