习题1
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.')
应该看到的结果:
Hello World!
Hello again.
I like typing this.
This is fun.
Yay!Printing.
I'd much rather you not.
I "said" do not touch this.
Process finished with exit code 0
附加练习
# (octothorpe)用来表示注释,在代码前加上#,此行代码不运行.
-----------------------------------------------------------------------------------------------------------------------------------
习题2
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print('I could have code like this.')# and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print('This will run.')
应该看到的结果
I could have code like this.
This will run.
Process finished with exit code 0
附加练习
#字符的英文为octothorpe或者pound character,用来作为代码的注释用,也可以禁止这行代码的运行。
-----------------------------------------------------------------------------------------------------------------------------------
习题3
print('I will now count my chickens:')#打印字符串print('Hens',25+30/6)#打印Hens, 25+ 30/6, /表示除以print('Roosters',100-25*3%4)#×表示乘以,%表示求余数print('Now I will count the eggs:')#打印字符串print(3+2+1-5+4%2-1/4+6)#打印数学运算结果, %表示求余数, /表示除以print('Is it true that 3 + 2 < 5 - 7?')#打印这句话,“3+2小于5-7吗”?print(3+2<5-7)#打印数学运算结果,结果是false
print('What is 3 + 2?',3+2)#打印字符串及数学运算结果,结果是What is 3 + 2? 5print('What is 5 - 7?',5-7)#打印字符串及数学运算结果,结果是What is 5-7? -2
print('Oh, 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)#打印字符串并判断真假
应该看到的结果
I will now count my chickens:
Hens 30.0
Roosters 97
Now I will count the eggs:
6.75
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False
Process finished with exit code 0
附加练习
ex3-1.py
print (2 + 5 / 5 -1)
结果是2.0,结果保留至小数点后一位。
浮点数是表示数字的标准,整数也可以用浮点数的格式来存储。
运算优先级分别是括号,指数,乘,除,加,减。
-----------------------------------------------------------------------------------------------------------------------------------
习题4
cars=100#有100辆carsspace_in_a_car=4.0#有4个车位drivers=30#有30位driverpassengers=90#有90位passengerscars_not_driven=cars-drivers#没有开的车辆数=cars- driverscars_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 available.')
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.')
-----------------------------------------------------------------------------------------------------------------------------------
习题5
my_name='Zed A. Shaw'my_age=35
my_height=74
my_weight=180
my_eyes='Blue'my_teeth='White'my_hair='Brown'
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\'s got %s eyes and %s hair.'%(my_eyes, my_hair))
print('His teeth are usually %s depending on the coffee.'%my_teeth)
# this line is tricky, try to get it exactlyrightprint('If I add %d, %d, and %d I get %d.'%(my_age, my_height,my_weight, my_age+my_height+my_weight))
应该看到的结果
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
附加练习
name='Zed A. Shaw'age=35
height=74
weight=180
eyes='Blue'teeth='White'hair='Brown'
print('Let\'s talk about %s.'%name)
print('He\'s %d inches tall.'%height)
print('He\'s %d pounds heavy.'%weight)
print('Actually that\'s not too heavy.')
print('He\'s got %s eyes and %s hair.'%(eyes, hair))
print('His teeth are usually %s depending on the coffee.'%teeth)
# this line is tricky, try to get it exactlyrightprint('If I add %d, %d, and %d I get %d.'%(age, height, weight,age+height+weight))
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
心得体会:改变变量名字,输出的结果是一样的。s%表示把变量带到字符串中,d%表示把变量带到数值中,r%意思为不管什么都打印出来。Python格式化字符包括:%s,%r,%c,%b,%d,%i,%o,%x,%e,%E,%f,%F,%g,%G.
英寸转化成厘米:my_height =74*2.54
磅转化成千克:my_weight =180*0.45
-----------------------------------------------------------------------------------------------------------------------------------
习题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'tthat joke so funny?! %r"
print(joke_evaluation%hilarious)
w="This is the leftside 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.
附加练习
红色代码是把一个字符串放进另一个字符串,总共4处。print(w
+ e)和print(w, e)输出的字符串是一样的,为什么?
-----------------------------------------------------------------------------------------------------------------------------------
习题7
print('Mary had a little lamb.')
print('Its fleece was white as %s.'%'snow')
print('And everywhere that Mary went.')
print('.'*10)#what'dthat 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 that comma atthe end. try removing it to see what happensprint(end1+end2+end3+end4+end5+end6, end7+end8+end9+end10+end11+end12)
应该看到的结果
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
心得体会:
print(end1+end2+end3+end4+end5+end6)
print(end7+end8+end9+end10+end11+end12)
输出
Cheese
Burger
print(end1+end2+end3+end4+end5+end6, end7+end8+end9+end10+end11+end12)
print(end1+end2+end3+end4+end5+end6+end7+end8+end9+end10+end11+end12)
输出输出CheeseBurger
-----------------------------------------------------------------------------------------------------------------------------------
习题8
formatter="%r %r %r %r"
print(formatter)%(1,2,3,4)
print(formatter)%("one","two","three","four")
print(formatter)%(True,False,False,True)
print(formatter)%("I had this thing.","That you could type up right.","But it didn't sing.","So I said goodnight.")
看到的结果
%r %r %r %r
Traceback (most recent call last):
File "C:/Users/ex8.py",line 3, in
print(formatter) %(1, 2, 3, 4)
TypeError: unsupported operand type(s) for %: 'NoneType' and'tuple'
错误分析:括号要把赋予的变量都括起来,代码改为
formatter="%r %r %r %r"
print(formatter%(1,2,3,4))
print(formatter%("one","two","three","four"))
print(formatter%(True,False,False,True))
print(formatter%("I had this thing.","That you could type up right.","But it didn't sing.","So I said goodnight."))
应该看到的结果
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'I had this thing.' 'That you could type up right.'"But it didn't sing." 'So I said goodnight.'
-----------------------------------------------------------------------------------------------------------------------------------
习题9
# Here's some new strange stuff, remember type it exactly.
days="Mon Tue Wed Thu Fri Sat Sun"months="Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print('Here are the days:', days)
print('Here are the months', months)
print("There's something going on here.\nWith the three double - quotes.\nWe'll be able to type as much as we like.\nEven 4 lines if we want, or 5, or 6.")
应该看到的结果
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
There's 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.
-----------------------------------------------------------------------------------------------------------------------------------
习题10
tabby_cat="\tI'm tabbed in."persian_cat="I'm split\non a line."backslash_cat="I'm\\a\\cat."
fat_cat="I'll do a list:\n\t* Cat food\n\t* Fishies\n\t* Catnip\n\t* Grass"
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
应该看到的结果
I'm tabbed in.
I'm split
on a line.
I'm \a \ cat.
I'll do a list:
*Catfood
*Fishies
*Catnip
* Grass