笨办法学Python (exercise1-15)

#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()

时间: 2024-08-11 08:04:41

笨办法学Python (exercise1-15)的相关文章

笨办法学Python 练习13和14

原题: 1 from sys import argv 2 3 script, first, second, third = argv 4 5 print "The script is called:", script 6 print "Your first variable is:", first 7 print "Your second variable is:", second 8 print "Your third variabl

笨办法学 Python (Learn Python The Hard Way)

最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 3: 数字和数学计算 习题 4: 变量(variable)和命名 习题 5: 更多的变量和打印 习题 6: 字符串(string)和文本 习题 7: 更多打印 习题 8: 打印,打印 习题 9: 打印,打印,打印 习题 10: 那是什么? 习题 11: 提问 习题 12: 提示别人

笨办法学Python(二十九)

习题 29: 如果(if) 下面是你要写的作业,这段向你介绍了"if语句".把这段输入进去,让它能正确执行.然后我们看看你是否有所收获. 1 people = 20 2 cats = 30 3 dogs = 15 4 5 6 if people < cats: 7 print "Too many cats! The world is doomed!" 8 9 if people > cats: 10 print "Not many cats!

笨办法学Python(二十七)

习题 27: 记住逻辑关系 到此为止你已经学会了读写文件,命令行处理,以及很多 Python 数学运算功能.今天,你将要开始学习逻辑了.你要学习的不是研究院里的高深逻辑理论,只是程序员每天都用到的让程序跑起来的基础逻辑知识. 学习逻辑之前你需要先记住一些东西.这个练习我要求你一个星期完成,不要擅自修改日程,就算你烦得不得了,也要坚持下去.这个练习会让你背下来一系列的逻辑表格,这会让你更容易地完成后面的习题. 需要事先警告你的是:这件事情一开始一点乐趣都没有,你会一开始就觉得它很无聊乏味,但它的目

笨办法学Python(二十一)

习题 21: 函数可以返回东西 你已经学过使用 = 给变量命名,以及将变量定义为某个数字或者字符串.接下来我们将让你见证更多奇迹.我们要演示给你的是如何使用 = 以及一个新的 Python 词汇return 来将变量设置为"一个函数的值".有一点你需要及其注意,不过我们暂且不讲,先撰写下面的脚本吧: 1 def add(a, b): 2 print "ADDING %d + %d" % (a, b) 3 return a + b 4 5 def subtract(a

笨办法学Python(十七)

习题 17: 更多文件操作 现在让我们再学习几种文件操作.我们将编写一个 Python 脚本,将一个文件中的内容拷贝到另外一个文件中.这个脚本很短,不过它会让你对于文件操作有更多的了解. 1 from sys import argv 2 from os.path import exists 3 4 script, from_file, to_file = argv 5 6 print "Copying from %s to %s" % (from_file, to_file) 7 8

笨办法学Python(三十二)

习题 32: 循环和列表 现在你应该有能力写更有趣的程序出来了.如果你能一直跟得上,你应该已经看出将"if 语句"和"布尔表达式"结合起来可以让程序作出一些智能化的事情. 然而,我们的程序还需要能很快地完成重复的事情.这节习题中我们将使用 for-loop (for 循环)来创建和打印出各种各样的列表.在做的过程中,你会逐渐明白它们是怎么回事.现在我不会告诉你,你需要自己找到答案. 在你开始使用 for 循环之前,你需要在某个位置存放循环的结果.最好的方法是使用列表

笨办法学Python(三十)

习题 30: Else 和 If 前一习题中你写了一些 "if 语句(if-statements)",并且试图猜出它们是什么,以及实现的是什么功能.在你继续学习之前,我给你解释一下上一节的加分习题的答案.上一节的加分习题你做过了吧,有没有? 你认为 if 对于它下一行的代码做了什么? If 语句为代码创建了一个所谓的"分支",就跟 RPG 游戏中的情节分支一样.if 语句告诉你的脚本:"如果这个布尔表达式为真,就运行接下来的代码,否则就跳过这一段.&quo

笨办法学Python(十五)

习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非常仔细,如果不仔细的话,你可能会吧有用的文件弄坏或者清空.导致前功尽弃. 这节练习涉及到写两个文件.一个正常的 ex15.py 文件,另外一个是 ex15_sample.txt,第二个文件并不是脚本,而是供你的脚本读取的文本文件.以下是后者的内容: 1 This is stuff I typed i

笨办法学Python(六)

习题 6: 字符串(string)和文本 虽然你已经在程序中写过字符串了,你还没学过它们的用处.在这章习题中我们将使用复杂的字符串来建立一系列的变量,从中你将学到它们的用途.首先我们解释一下字符串是什么东西.    字符串通常是指你想要展示给别人的.或者是你想要从程序里"导出"的一小段字符.Python 可以通过文本里的双引号 " 或者单引号 ' 识别出字符串来.这在你以前的 print 练习中你已经见过很多次了.如果你把单引号或者双引号括起来的文本放到 print 后面,它