【Python爬虫作业】- 第一周01 笨方法0-10加分题

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>
思考题

  1. 让你的脚本再多打印一行。
    print("hello world"+'\n')
  2. 让你的脚本只打印一行。
    print("hello world!",end="")
    print("hello again")
    print("hello world!","hello again")
  3. 在一行的起始位置放一个 ‘#’ (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>
思考题:

python转义字符串

改成单引号后没有区别。

!!!将转义序列和格式化字符串放到一起,创建一种更复杂的格式。这句话没有明白。答疑时要提出!!!

a = "\t* Catnip\n\t* Grass"
print("ceshi:%r,%s" %(a,a))
输出:
ceshi:'\t* Catnip\n\t* Grass', * Catnip
* Grass

可以看出%s自动把转义的省略了。
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容