# 笨办法学 Python3 第五版（预览）（一）(2)

#### 常见学生问题

=（单等号）和 == （双等号）之间有什么区别？ =（单等号）将右侧的值赋给左侧的变量。==（双等号）测试两个值是否相同。你以后会学到这个。

“反向阅读文件（代码）”是什么意思？ 非常简单。想象你有一个有 16 行代码的文件。从第 16 行开始，将其与我在第 16 行的代码进行比较。然后再对第 15 行进行同样的操作，依此类推，直到你将所有代码都反向阅读完。

### 练习 5：更多变量和打印

1   my_name = 'Zed A. Shaw'
2   my_age = 35 # not a lie
3   my_height = 74 # inches
4   my_weight = 180 # lbs
5   my_eyes = 'Blue'
6   my_teeth = 'White'
7   my_hair = 'Brown'
8
10   print(f"He's {my_height} inches tall.")
11   print(f"He's {my_weight} pounds heavy.")
12   print("Actually that's not too heavy.")
13   print(f"He's got {my_eyes} eyes and {my_hair} hair.")
14   print(f"His teeth are usually {my_teeth} depending on the coffee.")
15
16   # this line is tricky, try to get it exactly right
17   total = my_age + my_height + my_weight
18   print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

#### 你应该看到的内容

1   Let's talk about Zed A. Shaw.
2   He's 74 inches tall.
3   He's 180 pounds heavy.
4   Actually that's not too heavy.
5   He's got Blue eyes and Brown hair.
6   His teeth are usually White depending on the coffee.
7   If I add 35, 74, and 180 I get 289.

#### 学习扩展

1. 将所有变量中的my_都去掉。确保你在所有地方都修改了名称，不仅仅是在使用=设置它们的地方。
2. 尝试编写一些变量，将英寸和磅转换为厘米和千克。不要只是输入测量值。在 Python 中进行数学计算。

### 练习 6：字符串和文本

f"some stuff here {avariable}"
f"some other stuff {anothervar}"

Python 有另一种使用.format()语法的格式化方式，你可以在第 17 行看到。有时候当我想对已经创建的字符串应用格式时，你会看到我使用它。我们稍后会更详细地讨论这个。

1   types_of_people = 10
2   x = f"There are {types_of_people} types of people."
3
4   binary = "binary"
5   do_not = "don't"
6   y = f"Those who know {binary} and those who {do_not}."
7
8   print(x)
9   print(y)
10
11   print(f"I said: {x}")
12   print(f"I also said: '{y}'")
13
14   hilarious = False
15   joke_evaluation = "Isn't that joke so funny?! {}"
16
17   print(joke_evaluation.format(hilarious))
18
19   w = "This is the left side of..."
20   e = "a string with a right side."
21
22   print(w + e)

#### 你应该看到的内容

1   There are 10 types of people.
2   Those who know binary and those who don't.
3   I said: There are 10 types of people.
4   I also said: 'Those who know binary and those who don't.'
5   Isn't that joke so funny?! False
6   This is the left side of...a string with a right side.

#### 学习扩展

1. 浏览这个程序，并在每行上面写一个注释，解释它。
2. 找出所有将字符串放在另一个字符串中的地方。
3. 你确定只有四个地方吗？你怎么知道？也许我喜欢说谎。
4. 解释为什么使用+将两个字符串we相加会得到一个更长的字符串。

### 练习 7：合并字符串

1   print("Mary had a little lamb.")
2   print("Its fleece was white as {}.".format('snow'))
3   print("And everywhere that Mary went.")
4   print("." * 10) # what'd that do?
5
6   end1 = "C"
7   end2 = "h"
8   end3 = "e"
9   end4 = "e"
10   end5 = "s"
11   end6 = "e"
12   end7 = "B"
13   end8 = "u"
14   end9 = "r"
15   end10 = "g"
16   end11 = "e"
17   end12 = "r"
18
19   # watch end = ' ' at the end. try removing it to see what happens
20   print(end1 + end2 + end3 + end4 + end5 + end6, end=' ')
21   print(end7 + end8 + end9 + end10 + end11 + end12)

#### 你应该看到的内容

1   Mary had a little lamb.
2   Its fleece was white as snow.
3   And everywhere that Mary went.
4   ..........
5   Cheese Burger

#### 练习

1. 回顾并对每一行写下注释。
2. 反向阅读或大声朗读每一行，找出你的错误。
3. 从现在开始，当你犯错时，记下你犯了什么样的错误。
4. 当你进行下一个练习时，看看你犯过的错误，并尽量不在新的练习中犯同样的错误。
5. 记住每个人都会犯错。程序员就像魔术师一样，让每个人都以为他们是完美的，从不犯错，但这都是表演。他们经常犯错。

### 练习 8：手动格式化字符串

1   formatter = "{} {} {} {}"
2
3   print(formatter.format(1, 2, 3, 4))
4   print(formatter.format("one", "two", "three", "four"))
5   print(formatter.format(True, False, False, True))
6   print(formatter.format(formatter, formatter, formatter, formatter))
7   print(formatter.format(
8       "Try your",
9       "Own text here",
10       "Maybe a poem",
11       "Or a song about fear"
12   ))

#### 你应该看到的内容

1   1 2 3 4
2   one two three four
3   True False False True
4   {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
5   Try your Own text here Maybe a poem Or a song about fear

1. 取出第 1 行定义的 formatter 字符串。
2. 调用它的 format 函数，类似于告诉它执行一个名为 format 的命令行命令。
3. 传递给 format 四个参数，这些参数与 formatter 变量中的四个 {} 匹配。这就像向命令行命令 format 传递参数一样。
4. formatter 上调用 format 的结果是一个新字符串，其中的 {} 被这四个变量替换。这就是 print 现在打印出来的内容。

### 练习 9：多行字符串

1   # Here's some new strange stuff, remember type it exactly.
2
3   days = "Mon Tue Wed Thu Fri Sat Sun"
4   months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
5
6   print("Here are the days: ", days)
7   print("Here are the months: ", months)
8
9   print("""
10   There's something going on here.
11   With the three double-quotes.
12   We'll be able to type as much as we like.
13   Even 4 lines if we want, or 5, or 6.
14   """)

#### 你应该看到的内容

1   Here are the days: Mon Tue Wed Thu Fri Sat Sun
2   Here are the months: Jan
3   Feb
4   Mar
5   Apr
6   May
7   Jun
8   Jul
9   Aug
10
11   There's something going on here.
12   With the three double-quotes.
13   We'll be able to type as much as we like.
14   Even 4 lines if we want, or 5, or 6.

### 第 10 题. 字符串中的转义代码

"I am 6'2\" tall." # escape double-quote inside string
'I am 6\'2" tall.' # escape single-quote inside string

1   tabby_cat = "\tI'm tabbed in."
2   persian_cat = "I'm split\non a line."
3   backslash_cat = "I'm \\ a \\ cat."
4
5   fat_cat = """
6   I'll do a list:
7   \t* Cat food
8   \t* Fishies
9   \t* Catnip\n\t* Grass
10   """
11
12   print(tabby_cat)
13   print(persian_cat)
14   print(backslash_cat)
15   print(fat_cat)

#### 你应该看到的内容

1           I'm tabbed in.
2   I'm split
3   on a line.
4   I'm \ a \ cat.
5
6   I'll do a list:
7           * Cat food
8           * Fishies
9           * Catnip
10           * Grass

|
9天前
|

26 1
|
9天前
|

26 0
|
9天前
|

40 1
|
9天前
|

49 0
|
9天前
|

67 1
|
6月前
|
Web App开发 前端开发 JavaScript
Python Selenium 浏览器打印预览
Python Selenium 浏览器打印预览
146 0
|
Rust 网络协议 IDE
Python语言学习路线及技术汇总预览
Rust语言是一种新型的系统编程语
84 0
|

Python语言学习路线及技术汇总预览
Python语言学习路线及技术汇总预览
123 0
|
2天前
|

Python编程实验六：面向对象应用
Python编程实验六：面向对象应用
19 1