1、
<pre>
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.
hello world
hello world!hello again
hello world! hello again
</pre>
<pre>
思考题
- 让你的脚本再多打印一行。
print("hello world"+'\n') - 让你的脚本只打印一行。
print("hello world!",end="")
print("hello again")
print("hello world!","hello again") - 在一行的起始位置放一个 ‘#’ (octothorpe) 符号。它的作用是什么?自己研究一下。
答:注释作用。
</pre>
2、
<pre>
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.
</pre>
<pre>
思考题:
没有找到错误。。
</pre>
3、
<pre>
打印文字i will now count my chickens:
print("i will now count my chickens:")
输出hens 后面附上25+(30/6)的结果
print("hens",25+30//6)
输出roosters 后面附上100减去(25*3)除以4的余数
print("roosters",100-25*3%4)
打印文字now i will count the eggs:
print("now i will count the eggs:")
打印计算结果(3+2+1-5)+(4%2)-(1//4)+6
print(3+2+1-5+4%2-1//4+6)
打印文字is it true that 3+2<5-7?
print("is it true that 3+2<5-7?")
计算出(3+2)小于(5-7)的布尔值并打印出来
print(3+2<5-7)
打印文字what is 3+2? 后面附上3+2的结果
print("what is 3+2?",3+2)
打印文字what is 5-7?后面附上5-7的结果
print("what is 5-7?",5-7)
打印文字oh,that's why it's false.
print("oh,that's why it's false.")
打印文字how about some more.
print("how about some more.")
打印文字is it greater? 后面附上5大于-2的布尔值结果
print("is it greater?",5>-2)
打印文字is it greater or equal?后面附上5>=-2的布尔值
print("is it greater or equal?",5>=-2)
打印文字is it less or equal?后面附上5<=-2的布尔值
print("is it less or equal?",5<=-2)
输出:
i will now count my chickens:
hens 30
roosters 97
now i will count the eggs:
7
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
</pre>
<pre>
思考题:
自己找个想要计算的东西,写一个 .py 文件把它计算出来。计算10的阶乘。
a = 10
b = 1
while a>1:
b = b*a
a = a-1
else:
print(b)
输出:3628800
print("i will now count my chickens:")
print("hens",25.0+30.0//6.0)
print("roosters",100.0-25.0*3.0%4.0)
print("now i will count the eggs:")
print(3.0+2.0+1.0-5.0+4.0%2.0-1.0//4.0+6.0)
print("is it true that 3.0+2.0<5.0-7.0?")
print(3.0+2.0<5.0-7.0)
print("what is 3+2?",3.0+2.0)
print("what is 5-7?",5.0-7.0)
print("oh,that's why it's false.")
print("how about some more.")
print("is it greater?",5.0>-2.0)
print("is it greater or equal?",5.0>=-2.0)
print("is it less or equal?",5.0<=-2.0)
输出:
i will now count my chickens:
hens 30.0
roosters 97.0
now i will count the eggs:
7.0
is it true that 3.0+2.0<5.0-7.0?
False
what is 3+2? 5.0
what is 5-7? -2.0
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
</pre>
4、
<pre>
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
100-30
cars_not_driven = cars-drivers
30
cars_driven = drivers
30*4.0
carpool_capacity = cars_driven*space_in_a_car
90/30
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 car pool today.")
print("we need to put about",average_passengers_per_car,"in each car")
输出:
there are 100 cars available
there are only 30 drivers available.
there will be 70 empty cars today.
we can transport 120.0 people today.
we have 90 to car pool today.
we need to put about 3 in each car
</pre>
<pre>
思考题:
作者打错了变量,多打了一个
不会有问题,因为人数肯定是整数。
a = 10
b = 11
c = a+b
print(c)
</pre>
5、
<pre>
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'
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 exactly right
print("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.
</pre>
<pre>
思考题:
name = "zed a. shaw"
age = 35 #not a lie
height = 74#inches
weight = 180 #lbs
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 exactly right
print("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.
英镑和英寸转换
</pre>
6、
<pre>
06
x = "there are %d types of people." % 10
10赋值给了%d的位置。
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)
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
</pre>
<pre>
思考题:
为什么w+e 可以生成一个更长的字符串、
在原来的后面加上代码
a =w+e
print(a)
输出:
this is the left side of...a string with a right side
</pre>
7、
<pre>
print("mary had a little lamb")
print("its fleece was white as %s."% "snow")
print("and everywhere that mary went.")
print("."* 10)
what'd 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 that comma at the end. try removing it to see waht 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.
..........
cheese
burger
</pre>
<pre>
思考题:
无任何错误
</pre>
8、
<pre>
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 %(formatter,formatter,formatter,formatter))
print(formatter %(
"i had this thing.",
"that you could type up right.",
"but it didn't sing.",
"so i said goodnight."
))
最后一行因为其中有段字符串里包含了单引号,所以输出的时候括起来要使用双引号
</pre>
<pre>
思考题:
见上面。
</pre>
9、
<pre>
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.
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 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.
</pre>
<pre>
思考题:
木有
</pre>
10、
<pre>
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:
\t* Cat food
\t* Fishies
\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:
* Cat food
* Fishies
* Catnip
* Grass
</pre>
<pre>
思考题:
改成单引号后没有区别。
!!!将转义序列和格式化字符串放到一起,创建一种更复杂的格式。这句话没有明白。答疑时要提出!!!
a = "\t* Catnip\n\t* Grass"
print("ceshi:%r,%s" %(a,a))
输出:
ceshi:'\t* Catnip\n\t* Grass', * Catnip
* Grass
可以看出%s自动把转义的省略了。
</pre>