笨办法学Python (exercise1-15)

时间:2022-03-30 19:31:45

#exercise1
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.'

#exercise2 #号的用处
#exercise3 数字和数学计算
print "Hen",25+30/6
print "Hen",round(25+30/6,1) #round函数返回浮点型数据
print 1/4
print "Roosters", 25*3%4 #先乘法后求模
print 3+2<5-7
print "is it greater?",5>-2
#exercise4 变量
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_passengers_per_car=passengers/cars_driven
print "we need to put about",average_passengers_per_car,"in each car."
#exercise5 标准文本格式,变量嵌套在文本里面
my_name='caijiangyao'
my_age=24
my_height=43
my_weigh=45
my_eye='black'
my_teeth='white'
my_hair='brown'

print "let's talk about %s." % my_name #变量为字符串时要用%s
print "she is %d. inches tall." %my_height #变量为数值时要用%d
print "she's got %s eyes and %s hair" % (my_eye,my_teeth) #多个变量嵌套时
print "if i add %d,%d and %d i get %d." %(my_age,my_height,my_weigh,my_age+my_height+my_weigh)
print "我的身高%d" % my_height

#exercise6 字符串和文本
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 #r引用简写的变量名x
print "i also said: '%s'" %y #引用简写的变量名y
print "i also said: %r" %y

hilarious=False
joke_evalution="isn't that joke so funny?! %r"
print joke_evalution %hilarious

w="this is the left side of ..."
e="a string with a right side."
print w+e

#exercise7 更多的打印
print "mary had a little lamp."
print "its fleece was white as %s" %'snow'
print "and everywhere that mary went."
print "."*10 #文本字符串与数字乘 10个..........

end1="c"
end2="h"
end3="e"
end4="e"
end5="s"
end6="e"
end7="b"
end8="u"
end9="r"
end10="g"
end11="e"
end12="r"
print end1+end2+end3+end4+end5+end6,
print end7+end8+end9+end10+end11+end12 #,使得两条print的记录连在一起

#exercise8 打印

打印 "%r"与"%s"有什么不同,"%r"用来做debug比较好,因为它会显示变量的原始数据(raw data),而其他的符号则是用来向用户显示输出的。
formatter="%r %r %r %r"
print formatter %(1,2,3,4)
print formatter %("one","two","three","four")
print formatter %(
"i had this ting",
"that you could type up right.",
"but it didn't sing.",
"so i said goodnight.")

#exercise9 打印,打印 ,打印
days="Mon Tue Wed Thu fri sat sun"
months="jan\nfeb\nmar\napr\nmay\njun"
print "here is the days:",days
print "here are the months:",months

print """ there's someting going on here.
with three double-quotes.
even 4 lines if we want , or 5 ,or 6.
"""
#exercise10 那是什么
print "i am 6'2\" tall."
print 'i am 6\'2" tall.' #在容易混杂的符号前加上反斜杠\(back slash)

tabby_cat="\ti'm tabbled in."
print tabby_cat
persian_cat="i'm split\non a line."#\n换行
print persian_cat
blackslach_cat="i'm\\ a\\cat."
print blackslach_cat #另一个\是原文中有的
fat_cat="""
i'll do a list:
\t*catfood
\t*catnip\n\t* grass
"""
print fat_cat
#exercise11 提问
print "how old are you?",
age=raw_input()
print "how tall are you?",
height =raw_input()
print "how much do you weight?",
weight =raw_input()

print "so you are %r old,%r tall,and %r heavy." %(age,height,weight)

#exercise12 提示别人
y=raw_input("name?")#这句话会用 “Name?” 提示用户,然后将用户输入的结果赋值给变量 y。
#这就是我们提问用户并且得到答案的方式。
age=raw_input("how old are you?")
height=raw_input("how tall are you?")
weight=raw_input("how much do you weigh?")
print "so you are %r old,%r tall,and %r heavy." %(age,height,weight)

#exercise13 参数,解压,变量
from sys import argv
script, first, second, third = argv
print "the script is called:",script
print "your first variable is:",first
print "your second variable is:",second
print "your third variable is:",third
python ex13.py first 2nd 3rd

#exercise14 参数,解压,变量
from sys import argv
ex1.py, zed=argv
prompt ='>'
print "

#exercise15 读文件
from sys import argv
script,filename=argv
txt=open(filename)
print "here's your file %r:" %filename
print txt.read()

print "type the filename again:"
ex15_sample
file_again=raw_input("> ")
txt_again=open(file_again)
print txt_again.read()