Python入门书《笨办法学Python》笔记
第一篇、习题1至习题10
习题1、print打印字符串
代码段:
#coding=utf-8
print "Hello world!"
print "hello again"
print "I like typing like this "
print "This is fun"
print "Yay! printing."
print "I'd much rather you 'not'."
print 'I "said" don not touch this'
print"________________________end1_________________________"
结果:
打印出对应字符串
注意:
1、Python不区分”及’但是要成对出现,是对应关系。
2、#coding=utf-8 可支持输入中文
3、如果字符串中出现单个的“或’,一定不能与外层形同,避免匹配错误
习题2、注释和#号
代码段:
#coding=utf-8
"""
A comment, this is so you can read your program later.
anytihing after the # is ignored by python
"""
print "I could have code like this."
#and the comment after# is ignored
#you can use a comment to "disable" or comment a piece of code:
#print "This won't run"
print "This will run."
print "This # will be print"
print "_____________end_______________________"
结果:
仅输出未被注释掉的内容
I could code like this,
This will run.
This # will be print
注意:
1、注意#可以用来添加注释,也可以暂时屏蔽代码段
2、如果在字符串中间出现了#,则为字符串的一部分,会被打印出来
3、使用成对的三个引号,可以做大段注释
习题3、数字和数学计算
代码段:
#coding=utf-8
print "Test for calculate"
print "25 + 30 / 6 = ", 25 + 30 / 6
print "100 - 25 * 3 % 4 = ", 100 - 25 * 3 % 4
print "3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 = ",
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
print "Is it True that 3 + 2 < 5 - 7 ", 3 + 2 < 5 - 7
print "What is 3 + 2 ?", 3 + 2
print "What is 5 - 7 ?", 5 - 7
print "That's why it's False."
print "How about some more?"
print "Is it greater?", 5 > -2
print "Is it greater or equal? ", 5 >= -2
print "Is it less or equal? ", 5 <= -2
print "________________________end3______________________"
结果:
输出:30、97、7、False 、5、 -2、 True 、True、 False
与笔算结果一致
注意:
1、Python运算遵循基本的数学规则,并且整数相除取整,舍弃小数部分
2、%为取余符号 7 / 4 = 1, 7 % 4 = 3
习题4、变量和命名
代码段:
#coding=utf-8
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 "There are ", cars, "cars available"
print "There are only", drivers, "drivers acailable"
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."
print "___________________end4__________________________"
结果:
There are 100 cars available
There are only 30 drivers acailable
There will be 70 empty cars today!
we can transport 120.0 people today!
We have 90 to carpool today.
we need to put about 3 in each car.
注意:
1、在编程中,变量不过是用来指代某个东西的名字,程序员通过使用变量可以让程序读起来更接近自然语言
2、= 符号的作用是赋值运算符, == 符号的作用是判断左右两端是否相等
3、同样的代码, x=100, 与 x = 100, 后者操作符两边加上空格,会让代码更容易阅读
4、将Terminal终端的输出保存至txt文件中,其操作命令是: sudo script -f output.txt
—————————————————————————————
习题5、更多的变量和打印
涉及到格式控制工具%s、%d、%r
代码段:
#coding=utf-8
my_name = 'Zed A. Shaw'
my_age = 35 #not a lie
my_height = 74 #inches
my_weight = 180# lbs,
my_eyes = 'Blue'
my_teeth = 'white'
my_hair = 'Brown'
#1inches = 2.54cm, 大约是 74 * 2.54 = 187.9cm
#1lbs = 0.4532KG, 大约是180 * 0.4532 = 81.576KG
print "Let's talk about %s." %my_name
print "He's %d inches tall."%my_height
print "He's %d pounds heavy."%my_weight
print "Actually that's not too heavy."
print "He got %s eyes and %s hair." %(my_eyes, my_hair)
print "His teeth are usually %s ." %my_teeth
#this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d."%(my_age, my_weight, my_height, my_age + my_weight + my_height)
结果:
Let’s talk about Zed A. Shaw.
He’s 74 inches tall.
He’s 180 pounds heavy.
Actually that’s not too heavy.
He got Blue eyes and Brown hair.
His teeth are usually white depending on the coffee.
If I add 35, 180, and 74 I get 289.
注意:
1、这里使用”格式化字符串”创建了包含变量内容的字符串,使用专门的格式和语法把变量内容放到字符串中,相当于告诉Python, 这是一个格式化字符串,把这些变量放到哪几个位置
2、这里,%d是数字占位符,%s是字符串占位符,要与后面的%数目对应,按顺序配对
附加练习:
使用格式化字符串%r,它的含义是不管什么都打印出来
print "print my_name %r." %my_name
print "He's %r cm tall." %int(my_height * 2.54)
print "He's %r kilograms." %int(my_weight * 0.4532)
附加练习结果:
Print my_name ‘Zed A. Shaw’
He’s 187 cm tall
He’s 81 kilograms.
-r用来做调试(debug)比较好,因为它会显示变量的原始数据
习题6、字符串和文本
代码段:
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
print "I also said: '%s'" %y
hilarious = False
joke_evaluation = "Isn't that joke so funny ?!%r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
结果:
There are 10 types of people.
Those who know binary and those who don’t.
I said: ‘There are 10 types of people.’.
I also said: ‘Those who know binary and those who don’t.’
Isn’t that joke so funny ?!False
This is the left side of…a string with a right side.
注意:
1、Python可以通过文本中的双引号(”“)或者单引号(‘)识别出来字符串
2、格式化字符串:只需要将格式化变量放到字符串中,紧跟着一个百分号%,再紧跟着变量名就可以
3、但是需要注意的是,如果在字符串中通过格式化字符放入多个变量,需要将变量放到括号中(),而且变量之间用(,)隔开
习题7、更多打印
代码段:
print "Mary had a little lamb."
print "Its fleece was white as %s" %'snow'
print "And everywhere that mary went."
print "." * 10 #what's that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
#watch the comma in the end . try removing it to see what happens
print end1 + end2 +end3 + end4 + end5 + end6,
print end7 + end8 +end9 + end10 + end11 + end12
结果:
Mary had a little lamb.
Its fleece was white as snow
And everywhere that mary went.
注意:
1、lamb:羊羔 fleece:羊毛
2、print ‘.’ * 10, 同一行将.打印了10次
3、加号会将字符连接起来,输出Cheese Burger,其字面意思是奶酪,我认为是形象比喻小羊在Mary后面,咩咩的叫声
4、逗号分隔符提示,本行输出未结束,于是Cheese Burger在同一行打印出来
5、在Python中创建字符串,单引号,双引号都是可以得,区别是一般单引号创建较短的字符串,例如’a‘, ‘snow’
———————————————————————————————————————————
习题8、打印,打印
代码段:
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("One", "Two", "Three", "Four")
print formatter % (True, True, False, False)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % ("I had this thing.",
"That's you could type up right.",
"But it didn't sing.",
"So I said goodnight!.")
结果:
1 2 3 4
‘One’ ‘Two’ ‘Three’ ‘Four’
True True False False
‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’
‘I had this thing.’ “That’s you could type up right.” “But it didn’t sing.” ‘So I said goodnight!.’
注意:
1、(1、2、3、4)为整数类型,(False、True、False、True)属于布尔类型, (“one” “two” “three”)、”(%r %r %r %r”)及后面长句子数据字符串类型,在%后面是具体类型的常量、变量、表达式
2、最后一行的输出既有双引号,也有单引号,Python会用最有效的方式打印字符串,可能不是按照输入的方式
3、一般%r用于调试,查看程序员原始版本,又被称为原始表示(raw representation)
———————————————————————————————————————————
习题9、打印,打印,打印
代码段:
#here are some new strange stuff, remember type it exactly.
days = " Monday Tuesday Wednesday Thursday Friday Saturday Sunday "
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There are something going on here.
with the three double-quotes
we'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
结果:
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
AugThere are something going on here.
with the three double-quotes
we’ll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
注意:
1、引入了转义序列中的换行符\n,打印时遇到\n直接跳转到下一行
2、三个双引号或者三个单引号配合成对使用,也是字符串的一种形式,若没有名字或者打印操作,常用于屏蔽代码段
3、需要注意三个单引号或双引号必须连续,中间不要有空格
习题10、转义序列相关内容
代码段:
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm spilt \non a line"
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I' ll do a list
\t *cat food
\t *Fishes
\t *Catnip\n\t *Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
结果:
(这里是一个Tbale)I’m tabbed in.
I’m spilt
on a line
I’m \ a \ cat.
I' ll do a list
*cat food
*Fishes
*Catnip
*Grass
注意:
1、常用转义序列:
转义字符 功能
\\ 反斜杠(\)
\n 换行符
\t Table键
2、注意如果%r占位符打印,则会将/n这些全部打印出来,因为%r打印的是写到代码里面的原始字符串,会包含转义字符
记住%r用于调试,%s用于显示
———————————————————————————————————————————