Exercise 3 Numbers and Math

Numbers and Math

数学与数字

Every programming language has some kind of way of doing numbers and math. Do not worry:

每个编程语言有各种形式在数字和数学方面上,别担心:

programmers frequently lie about being math geniuses when they really aren’t. If they were math geniuses, they would be doing math, not writing buggy web frameworks so they can drive race cars.

程序员们常常不是一个数学天才。如果他们是,他们将会去做数学,而不是写有问题的WEB框架以至于他们能够开车赛跑。

This exercise has lots of math symbols. Let’s name them right away so you know what they are called.

这种练习有大量的数学特征。让我们一起用正确的方式去命名以便你知道如何称呼他们。

As you type this one in, say the name. When saying them feels boring you can stop saying them. Here are the names:

输入此名称时,说出名称。 说他们感到无聊时,您可以停止说他们。 下面是名称:

• +: plus
• -: minus
• /: slash (斜杠)
• *: asterisk (星号)
• %: percent (百分号)
• <: less-than
• >: greater-than
• <=: less-than-equal
• >=: greater-than-equal

Notice how the operations are missing? After you type in the code for this exercise, go back and figure out what each of these does and complete the table. For example, + does addition.

注意:如何?在这次的代码练习之后,回顾并解决并总结成一张表。例如: + 就是 加法

这里我做了一张表,放在文章的最后,书中没有。

ex3.py

1 print("I will now count my chickens:") 
2
3 print("Hens", 25 + 30 / 6) 
4 print("Roosters", 100 - 25 * 3 % 4) 
5
6 print("Now I will count the eggs:") 
7
8 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) 
9
10 print("Is it true that 3 + 2 < 5 - 7?")
11
12 print(3 + 2 < 5 - 7)
13
14 print("What is 3 + 2?", 3 + 2)
15 print("What is 5 - 7?", 5 - 7)
16
17 print("Oh, that's why it's False.")
18
19 print("How about some more.")
20
21 print("Is it greater?", 5 > -2)
22 print("Is it greater or equal?", 5 >= -2)
23 print("Is it less or equal?", 5 <= -2)
  • 代码块中的行标仅供描述方便,复制到时候注意删除,建议代码自己手打,学习效果更好
What You Should See
你应该看到的
$ python3.6 ex3.py
I will now count my chickens:
Hens 30.0
Roosters 97
Now I will count the eggs:
6.75
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

解释:

# 数一数我的鸡:
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)
# 3 + 2 < 5 - 7 是对的吗?
print("Is it true that 3 + 2 < 5 - 7?")
# 输出结果,对的返回True,反之返回False
print(3 + 2 < 5 - 7)
# 算算 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)

Make sure you type this exactly before you run it. Compare each line of your file to my file.

在运行之前,请确保键入这些内容。将文件中的每一行与我的进行比较。

Study Drills
深入学习
  1. Above each line, use the # to write a comment to yourself explaining what the line does.

上面的每一行,使用 # 写下注释,使得自己能够理解这些代码

  1. Remember in Exercise 0 when you started python3.6? Start python3.6 this way again and, using the math operators, use Python as a calculator.

还记得在练习 0 中,当你开始用python 3.6 的时候吗?开始使用python 3.6 这次,使用数学运算符,用 pyhton 作为计算器。

  1. Find something you need to calculate and write a new .py file that does it.

找到一些你需要计算和写一个 .py 文件

  1. Rewrite ex3.py to use floating point numbers so it’s more accurate. 20.0 is floating point.

重写 ex3.py 使用浮点数以便更精确。20.0就是浮点数。

Common Student Questions
学生们的共同问题

Why is the % character a “modulus” and not a “percent”? Mostly that’s just how the designers chose
to use that symbol. In normal writing you are correct to read it as a “percent.” In programming this calculation is typically done with simple division and the / operator. The % modulus is a different operation that just happens to use the % symbol.

为什么% 是一个取模运算而不是百分号? 多数情况下,这就是设计师选择的方式使用该符号。一般当你读或写百分号的时候。 在编程中,此计算通常使用简单的除法和 / 运算符完成。 %模运算是一种恰好使用 符号的操作。

How does % work? Another way to say it is, “X divided by Y with J remaining.” For example, “100 divided by 16 with 4 remaining.” The result of % is the J part, or the remaining part.

如何使用% 另一种说法是,x 除以 y 余数为 J。例如,100 除以 16 余数为 4 .% 结果返回是 J 部分,或者是余数部分。

What is the order of operations? In the United States we use an acronym called PEMDAS which stands for Parentheses Exponents Multiplication Division Addition Subtraction. That’s the order Python follows as well. The mistake people make with PEMDAS is to think this is a strict order, as in “Do P, then E, then M, then D, then A, then S.” The actual order is you do the multiplication and division (M&D) in one step, from left to right, then you do the addition and subtraction in one step from left to right. So, you could rewrite PEMDAS as PE(M&D)(A&S).

操作符的优先级? 在美国,我们使用首字母缩略词:PEMDAS,就是 括号指数乘法除法加法减法。这也是 python 中遵循的顺序。人们使用 PEMDAS 犯的错误是认为这是一个严格的命令,例如先括号,然后是乘法,除法,然后是加法,减法。事实上乘除是一个优先级,从左到右,然后你再作加减运算,从左至右。所以,你应该将 PEMDAS 改写为 PE(M&D)(A&S),即 括号 指数(幂运算) (乘除)(加减)

算术运算符:迷你小结

符号名称例子结果备注
+加法1 + 23
-减法1 - 2-1
*乘法1 * 22
/除法1 / 20.5结果返回浮点数
//求商1 // 20结果返回整数商
%取余1 % 21被除数小于除数,返回前者,反之亦然
**求幂2 ** 242的平方
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值