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的余數(shù)
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>
思考題:
作者打錯了變量瞬测,多打了一個
不會有問題横媚,因為人數(shù)肯定是整數(shù)。
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)
多個參數(shù)賦值需要在括號里孝宗,第一個字符串包含字符串
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)
竟然還可以在變量里放入參數(shù)穷躁,然后賦值的,第四個字符串包含字符串
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>
思考題:
改成單引號后沒有區(qū)別。
Q浮M赝恰!將轉義序列和格式化字符串放到一起描孟,創(chuàng)建一種更復雜的格式驶睦。這句話沒有明白。答疑時要提出D湫选3『健!
a = "\t* Catnip\n\t* Grass"
print("ceshi:%r,%s" %(a,a))
輸出:
ceshi:'\t* Catnip\n\t* Grass', * Catnip
* Grass
可以看出%s自動把轉義的省略了青抛。
</pre>