一、Python简介
1.1 print "Hello World"
1.2 变量和算术表达式
#print
principal = 1000
rate = 0.05
numyears = 5
year = 1
while year <= numyears:
principal = principal * (1 + rate)
print year, principal
# print(year, principal)
print "%3d %0.2f" % (year, principal)
# print("%3d %0.2f" % (year, principal))
print format(year,"3d"), format(principal, "0.2f")
# print(format(year,"3d"), format(principal,"0.2f"))
print "{0:3d} {1:0.2f}".format(year,principal)
# print("{0:3d} {1:0.2f}".format(year,principal))
year += 1
#同一行上用分号隔开多条语句
#循环主体由缩进表示,每个缩进层次使用4个空格
#格式化字符序列
#%3d将一个整数格式化为在一个宽度为3的列中右对齐
1.3条件语句
a = 1
b = 2
if a < b:
print "Computer says yes"
else:
print "Computer syas no"
if a < b:
pass # Do nothing
else:
print "Computer syas no"
# or and not
product = "game"
type1 = "pirate memory"
age = 9
if product == "game" and type1 == "pirate memory" \
and not (age < 4 or age > 8):
print "I will take it!"
else:
print "no...."
#switch case
suffix = ".html"
if suffix == ".html":
content = "text/html"
elif suffix == ".jpg":
content = "image/jpeg"
elif suffix == ".png":
content = "image/png"
else:
raise RuntimeError("Unknown content type")
print content
#True False
s = 'p'
if 'spam' in s:
has_spam = True
else:
has_spam = False
print has_spam
has_spam = "spam" in s
print has_spam