【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
#学习总结:\或\\代表转义,是将难以打印的特殊字符放在字符串,使其具有特别的意义。在打印多行命令行时用"""三引号,并且中间不可以有空格。当使用'''三单引号和使用"""三双引号打印没有区别。

你可能感兴趣的:(【Python爬虫】01作业)