【Python爬虫】01作业

习题0

在老师的帮助下完成,特别感谢程程老师!

习题1

#-*- coding: utf-8-*-
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.')
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex1.py
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
#在每行前加#用来表示注解,也可指临时禁用此行代码。

习题2

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 will run.")

print("This will run.")
print("Hi # there.")
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex2.py
I could have code like this.
This will run.
Hi # there.

Process finished with exit code 0

习题3

print("I will now count my chickens:")

print("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?")

print(3 + 2 < 5 - 7)

print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)

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)
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex3.py
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

Process finished with exit code 0
# 在Python3中//表示除以,在Python2中/表示除以,且结果为整数,不是浮点数。%指求余数符号。运算优先级为括号,指数,乘,除,加,减。

习题4


cars = 100
space_in_a_car = 4
drivers = 30
passengers = 90
cars_driven = drivers
cars_not_driven = cars - drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_not_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.")

C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex3.py
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

Process finished with exit code 0
# x, y, j为常用变量名,赋值时用“=”,即x = ?

习题5

#name = 'Zed A. Shaw'
#age = 35 # not a lie
#height = 74 # inches
#weight = 180 # 1bs
#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))
from sympy.physics.units import centimeters, kg

name = 'Zed A. Shaw'
age = 35
height = 74  # inches * 2.54 centimeters
weight = 180  # 1bs * 0.4536 kilograms
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'

# how to transform height and weight?
print("Let's talk about %r." % name)
print("He's %r centimeters tall." % height)
print("He's %r kilograms heavy." % weight)
print("Actually that's not too heavy.")
print("He's got %r eyes and %r hair." % (eyes, hair))
print("His teeth are usually %r depending on coffee." % teeth)

# this line is tricky, try to get it exactly right
print("If I add %r, %r, and %r I get %r." % (age, height, weight, age + height + weight))
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex5.py
Let's talk about 'Zed A. Shaw'.
He's 74 centimeters tall.
He's 180 kilograms heavy.
Actually that's not too heavy.
He's got 'Blue' eyes and 'Brown' hair.
His teeth are usually 'White' depending on coffee.
If I add 35, 74, and 180 I get 289.

Process finished with exit code 0
学习总结:想通过Python直接进行公式换算没有解决。%s用来表示代入后面的字符串,%d表示取整数,%r可以代入任何内容既可为字符串又可为数字。
# Python的所有格式符
%s    字符串 (采用str()的显示)

%r    字符串 (采用repr()的显示)

%c    单个字符

%b    二进制整数

%d    十进制整数

%i    十进制整数

%o    八进制整数

%x    十六进制整数

%e    指数 (基底写为e)

%E    指数 (基底写为E)

%f    浮点数

%F    浮点数,与上相同

%g    指数(e)或浮点数 (根据显示长度)

%G    指数(E)或浮点数 (根据显示长度)

%%    字符"%"

习题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)
#打印变量x和变量y
print(x)
print(y)
#其中,%r 指不管什么都打印出来,此处用变量x字符串代替,以下同理。
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和变量e,且两变量赋值都为字符串
w = "This is the left side of ..."
e = "a string with a right side."
print(w + e)
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex6.py
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.

Process finished with exit code 0

习题7

# 打印字符串
print("Mary had a little lamb.")
# 打印字符串,其中%s用字符串snow 替代
print("Its fleece was white as %s." % 'snow')
# 打印
print("And everywhere that Mary went.")
# 打印 10次 "."
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 what happens
# 打印变量累加,其中指令print必须顶格
print(end1 + end2 + end3 + end4 + end5 + end6)
print(end7 + end8 + end9 + end10 + end11 + end12)
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex7.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese
Burger

Process finished with exit code 0
#学习总结当命令行中出现*,用来表示重复出现,后面数字为几,则重复*前面的内容多少次。

习题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 % (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."))
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

Process finished with exit code 0
#学习总结:%s与%r是有区别的,通常%s仅用于表示代入字符串,而%r多用于指debug中的原始数据,之任何内容都可以代入命令行,任何内容都可以打印出来。r%用于debug,s%用于显示。

习题9

# Here's some new strange stuff, remember type ot 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.
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.
""")
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex9.py
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.

Process finished with exit code 0
#学习总结: 字符串以\n开始表示换行。为了将字符串连接起来,可用+连接。

习题10

#print("I am 6'2\" tall.")
#print('I am 6\'2" tall.')

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
"""
C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex10.py
    I'm tabbed in.
I'm split
on a line.
I'm \ a\ cat.

I'll do a list:
    * Cat food
    * Fishies
    * Catnip
    * Grass


Process finished with exit code 0
#学习总结:\或\\代表转义,是将难以打印的特殊字符放在字符串,使其具有特别的意义。在打印多行命令行时用"""三引号,并且中间不可以有空格。当使用'''三单引号和使用"""三双引号打印没有区别。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容