学习「“笨方法”学Python 3」第一篇

本文整理并记录《“笨方法”学Python3》的习题内容,除了展示原文中的代码外,还记录了我在学习过程中遇到的问题。对我来说,这本书作为Python学习的入门教材再合适不过了,照着书本敲代码 -> 将运行结果与原文正确结果对照 -> 进行下一个练习并重复上述过程,这样的学习过程对于初学者来说很有成就感也容易快速入门。

断断续续学习《“笨方法”学Python3》(《Learn PYTHON 3 the HARD WAY》)有一段时间了,上次集中专注学习这本书还是在百词斩实习的时候🤨(艾老师催读的工作量…安排的不够饱和哦🤣)

这本书所谓的“笨方法”是指:
1、从头到尾完成每一个习题(共52个);
2、一字不差地录入每一段程序;
3、让程序运行起来。

在完成项目练习即习题45~52之前,我先来简单回顾下习题1~44教会了我什么?

习题0 准备工作

习题1 第一个程序

1
2
3
4
5
6
print("Hello World")
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.')

习题2 注释和#号

1
2
3
print("I could have code like this")#and the comment after this is ignored
#You can also use a "comment --> #" to disable or comment out code
print("This will run.")

习题3 数字和数学计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
print("I wll now count my chickens:")#print函数,打印语句

print("Hens", 25 + 30 / 6) #Hens 30(是30.0!)先算术运算符后比较运算符(包含<>才算比较)25+5
print("Roosters", 100 - 25 * 3 % 4) #Roosters 97 (%是取余/是除法)
print("Now I will count my eggs:")#print函数,打印语句

print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) #6.75 (/ 除出来多少就是多少)

print("Is it true that 3 + 2 < 5 - 7?")

print(3 + 2 < 5 - 7)#先算术运算符然后比较运算符 注意优先级 #0(结果是False/true)

print("What is 3 + 2?", 3 + 2) # 5 直接把语句(算术)的结果打印出来
print("what is 5 - 7?", 5 - 7) # -2 直接把算术语句的结果打印出来
print("Oh, that's why it's False") #print函数,打印语句

print("How about some more.") #print函数,打印语句

print("Is it greater?", 5 > -2) #1 直接把语句(比较)的结果打印出来true/false
print("Is it greater or equal?", 5 >= -2) #1
print("Is it less or equal?", 5 <= -2) #0

习题4 变量和命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cars = 100
space_in_a_car = 4.0 # 试试4.0!!!看看结果有什么不同
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_ocapacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print("There are", cars, "cars availabe.") # 100
print("There are only", drivers, "drivers availabe.") # 30
print("There will be ", cars_not_driven, "empty cars today.") # 70
print("We can transport", carpool_ocapacity, "people today.") #120.0
print("We have", passengers, "to carpool today.") # 90
print("We need to put about", average_passengers_per_car,"in each car.") # 3.0

习题5 更多的变量和打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name = "Zed A. Shaw" # 显然’和”都可以
age = 35
height = 74
weight = 180
eyes = 'Bule' #像这样的名称是字符串要用引号括起来
teeth ='White'
hair = 'Brown'

print(f"Let's talk about {name}.")#f就是format格式化字符串的意思
print(f"He's {weight} inches tall.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {teeth} depending on coffees.")

total = age + height + weight
print(f"If I add {age} , {height} , and {weight} I get {total}.")

习题6 字符串和文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
types_of_people = 10 #对变量赋值
x = f"There are {types_of_people} types of people" #对变量赋值,字符串就是”值”

binary = 'binary'
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."

print(x) #打印变量(字符串)
print(y)

print(f"I said: {x}") #f-string
print(f"I also said: '{y}'")

hilarious = False
joke_evalution = "Isn't that joke funny?! {}" #对变量赋值,

print(joke_evalution.format(hilarious))

w = "This is the left side of ..." #python可以用‘和“识别出来字符串
e = "a string with a right side."

print(w + e)

习题7 更多打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
print("Mary had a litte lamb.")
print("Its fleece was white as {}.".format('snow')) #{} .format()
print("And everywhere that Mary went.")
print("."* 10) #打印10次字符串里面的内容

end1 = 'c'
end2 = 'h'
end3 = 'e'
end4 = 'e'
end5 = 's'
end6 = 'e'
end7 = 'B'
end8 = 'u'
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print(end1 + end2 + end3 + end4 + end5 + end6, end = ' ') #end = ''结尾不换行;end = ’ ’结尾不换行且加一个空格
print(end7 + end8 + end9 + end10 + end11 + end12)

习题8 打印,打印

1
2
3
4
5
6
7
8
9
10
11
12
formatter = "{} {} {} {} "

print(formatter.format(1,2,3,4)) #赋值
print(formatter.format("one","two","three","four")) #赋字符串
print(formatter.format(True,False,False,True)) #赋值 相当于 1 0 0 1 #True 和 False是关键字 具有相应功能
print(formatter.format(formatter,formatter,formatter,formatter))
print(formatter.format(
"Try your",
"Own text here",
"Maybe a poam",
"Or a song about fear"
))#没有换行哦 --> “”“ ””“可以换行

习题9 打印,打印,打印

1
2
3
4
5
6
7
8
9
10
11
12
days = "Mon Tue Wen 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 there.
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.
''')

习题10 那是什么

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tabby_cat = "\tI'm tabbed in." #转义序列 \t 水平制表符
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* Fishes
\t* Catnip\n\t* Grass
'''

print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)