理解 Python 中的逻辑(《Learn Python the Hard Way》的练习 27)

发布于 2024-10-07 19:38:01 字数 234 浏览 2 评论 0原文

有人可以向我解释《学习Python的艰难之路》中的练习27吗?

这是一张显示我不明白的部分的图像。 alt text

是不是说假设表格左列为真,那么答案是否为真?

例如,如果 x = y 不为假,则 x=y 为真吗?是的。

但是……如果 x = y 为假且为真,那么 x=y 为真吗?不??

Could someone explain exercise 27 of learn python the hard way to me?

Here is an image showing the section i don't understand.
alt text

Is it saying that assuming the left column of the table is true, is the answer true?

eg if x = y is not false, is x=y true? yes.

but then.. if x = y is false and true, is x=y true? no??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

挽清梦 2024-10-14 19:38:01

对于每个表,如果输入(左侧列中的表达式),则结果为(右侧列中的对应值);表头显示正在解释的运算符。因此,例如,如果您输入“not True”作为表达式,则结果将为 False。

编辑:

天啊路易斯 - 看,这真的很简单。例如,AND 运算符表:

If a is True  and b is True,  then a AND b is True
If a is True  and b is False, then a AND b is False
If a is False and b is True,  then a AND b is False
If a is False and b is False, then a AND b is False

这就是该表试图告诉您的全部内容 - 这就是 AND 运算符的工作原理。它接受两个真或假值,并根据它们的组合方式返回结果。该表列出了每种可能的输入组合以及每个组合的结果 - 它完整地描述了操作员可以执行的所有操作。这就是它想说的全部。

对于其他运算符也类似:

NOT is "the opposite of": if a is False, then Not a is True
AND is true if "both of" a and b are true
OR is true if "at least one of" a or b is true
== is "equal to", true if a and b have the same value
!= is "not equal to", true if a and b have different values

等等。

Edit2:

如果您将其中一些运算符视为电路,可能会更容易(事实上,这个类比正是您的计算机的构建方式!)。

AND 是一个时序电路 - 接通开关 A 的电线,将 B 切换至灯泡。仅当开关 A 打开且开关 B 也打开时,灯泡才准确亮起。

OR 是并联电路 - 电线将 A 切换到灯泡,并将 B 切换到灯泡。如果其中一个开关打开(或两个开关都打开),则灯泡亮起。

== 是一对双向开关 - 仅当两个开关都向上或两个开关都向下时灯才会亮。

这有帮助吗?

For each table, if you enter (expression in left-hand column) the result is (corresponding value in right-hand column); the table header shows the operator being explained. So, for example, if you enter "not True" as an expression, the result will be False.

Edit:

Geeze Louise - look, it's really simple. For example, the AND operator table:

If a is True  and b is True,  then a AND b is True
If a is True  and b is False, then a AND b is False
If a is False and b is True,  then a AND b is False
If a is False and b is False, then a AND b is False

This is all the table is trying to tell you - this is how the AND operator works. It takes two true-or-false values and returns a result based on how they combine. The table lists every possible combination of inputs and the result for each - it completely describes everything the operator can do. That's ALL it's trying to say.

Similarly for the other operators:

NOT is "the opposite of": if a is False, then Not a is True
AND is true if "both of" a and b are true
OR is true if "at least one of" a or b is true
== is "equal to", true if a and b have the same value
!= is "not equal to", true if a and b have different values

etc.

Edit2:

It might be easier if you think of some of these operators as electrical circuits (indeed, this analogy is precisely how your computer is built!).

AND is a sequential circuit - power on a wire running to switch A to switch B to a lightbulb. The lightbulb is on exactly and only when switch A is on and switch B is also on.

OR is a parallel circuit - the wire runs to switch A to the lighbulb, and also to switch B to the lightbulb. If either switch is on (or if both are on), the lightbulb is on.

== is a pair of two-way switches - the light is on only if both switches are up or if both switches are down.

Does that help?

明媚殇 2024-10-14 19:38:01

这是一个真值表:描述的操作(==)都可以被视为仅适用于TrueFalse。在这种情况下,要完整地描述运算符,您只需列出所有可能的输入。

因此,例如,运算符or定义为:

(True or True) is True
(True or False) is True
(False or True) is True
(False or False) is False

这完全解释了or对布尔值的作用。


如果您感兴趣,该 wiki 页面实际上列出了所有可能的布尔二元运算符:

0. Opq, false, Contradiction
1. Xpq, NOR, Logical NOR
2. Mpq, Converse nonimplication
3. Fpq, ¬p, Negation
4. Lpq, Material nonimplication
5. Gpq, ¬q, Negation
6. Jpq, XOR, Exclusive disjunction
7. Dpq, NAND, Logical NAND
8. Kpq, AND, Logical conjunction
9. Epq, XNOR, If and only if, Logical biconditional
10. Hpq, q, Projection function
11. Cpq, if/then, Logical implication
12. Ipq, p, Projection function
13. Bpq, then/if, Converse implication
14. Apq, OR, Logical disjunction
15. Vpq, true, Tautology

It's a truth table: the operations described (or, and, ==) can all be considered as applying just to True and False. In that case, to describe the operator completely you merely need to list all the possible inputs.

So, for instance, the operator or is defined as :

(True or True) is True
(True or False) is True
(False or True) is True
(False or False) is False

That completely explains what or does to boolean values.


If you're interested, that wiki page actually lists all the possible boolean binary operators:

0. Opq, false, Contradiction
1. Xpq, NOR, Logical NOR
2. Mpq, Converse nonimplication
3. Fpq, ¬p, Negation
4. Lpq, Material nonimplication
5. Gpq, ¬q, Negation
6. Jpq, XOR, Exclusive disjunction
7. Dpq, NAND, Logical NAND
8. Kpq, AND, Logical conjunction
9. Epq, XNOR, If and only if, Logical biconditional
10. Hpq, q, Projection function
11. Cpq, if/then, Logical implication
12. Ipq, p, Projection function
13. Bpq, then/if, Converse implication
14. Apq, OR, Logical disjunction
15. Vpq, true, Tautology
鸩远一方 2024-10-14 19:38:01

我找到了“真的吗?”标题有点误导。它应该是“值”或“结果”或类似的。因此,基本上 True 或 False 的值(或结果)是 True (第二个表),就像 3 + 5< 的值(或结果)一样/code> 是 8。这就像算术,仅此而已。

I find the "True?" heading a bit misleading. It should be "Value" or "Result" or similar. So, basically the value (or result) of True or False is True (second table), just like the value (or result) of 3 + 5 is 8. It's like arithmetic, nothing more.

摘星┃星的人 2024-10-14 19:38:01

真值表就像乘法表一样。它们使您能够根据输入找到逻辑运算的结果。在类比中,乘法被合取(and)、析取(or)等所取代。

它可能会帮助你思考真值表方法的一些潜在态度:

  1. “和”和“或”的含义不会根据上下文而改变。这与自然英语截然不同,在自然英语中,诸如“再说一遍,我会打断你的腿”和“你要吃掉那个还是把它扔进垃圾桶?”等陈述,“和”和“或”的含义“受到周围环境的影响。

  2. 涉及“与”、“或”、“非”等的逻辑语句的真假,仅取决于组成语句的真假。陈述的任何其他含义或含义之间的关系都是无关紧要的。因此,“1=1,否则你就是愚蠢的”是简单、真实且没有问题的,而在正常对话中它可能会引起一些反对。

基于这两个假设,仅使用有关个体(“A”、“B”)是否为真的信息,就可以明确地说“A 或 B”形式的任何陈述是否为真。这就是真值表的作用。

希望这有帮助。

Truth tables are just like times tables. They empower you to find the result of a logical operation based on the inputs. In the analogy, multiplication gets replaced with conjunction (and), disjunction (or), etc.

It may help you to think about some underlying attitudes of the truth-table approach:

  1. The meaning of "and" and "or" does not change based on context. This is radically different from natural English, where such statements as "say that again and I'll break your legs," and "Are you gonna eat that or throw it in the trash?", the meaning of "and" and "or" is influenced by their surroundings.

  2. A logical statement involving and, or, not, etc., is true or false, depending only on the truth or falsehood of the component statements. Any other meaning of the statements, or relationship between meanings, is irrelevant. Thus "1=1 or you're stupid" is simple, true, and unproblematic, whereas in normal conversation it might provoke some objection.

Based on these two presumptions, it's possible to say categorically whether any statement of the form "A or B" is true, using only information about whether the individuals ("A", "B") are true. This is what the truth table does.

Hope this helps.

赠意 2024-10-14 19:38:01

这并不难理解。它是一个介词。

假设有人对我说:“他一只手上有一个苹果并且另一只手上有一个苹果”,但我一只手上只有一个苹果,那么那个人刚才的陈述是错误的。

因此,介词:a AND b 表示a 和b 都为真,因此如果a 为假,则该介词为假。

更棘手的是“或”。 OR 介词表示 2 项中至少有一项必须为真,但如果它们都为真,则该陈述仍然为真,因为“2 项中至少有 1 项为真”

,就像有人说:“我知道那个人他至少一只手上有一个苹果”即使我每只手上都有一个苹果,他们也不会撒谎,对吗?因此他们的说法是正确的。

this isn't that hard to understand. its a preposition.

lets say someone says about me: "He has an apple in one hand AND an apple in the other hand" but I only have an apple in one hand then the statement that person just made is false.

Therefore the preposition: a AND b is saying that a and b are both true, so if a is false for example then that preposition is false.

the trickier one is OR. The OR preposition says that at least one of the 2 must be true but if they are both true then that statement is still true because "at least 1 of the 2 are true"

so that would be like someone saying: "I know that guy has an apple in at least 1 of his hands" they wouldn't be lieing even if i had an apple in each hand right? therefore their statement is true.

花之痕靓丽 2024-10-14 19:38:01

左边是问题,右边是答案的真相。

  • 真 == 真 |真
  • 真 == 假 |错误的

Left side question, right side truth of answer.

  • true == true | true
  • true == false | false
丿*梦醉红颜 2024-10-14 19:38:01

你为什么要努力记住这些?

(例如)1 == 0 不是真的,这对你来说不符合逻辑吗?

Why are you trying to memorize these?

Doesn't it make logical sense to you that (for example) 1 == 0 is not true?

喜你已久 2024-10-14 19:38:01

在上面的所有标题中,对“True?”应用白色。标题,并写在“结果”中。那么就清楚多了。

In all of the heading above, apply whiteout to the "True?" heading, and write in "Result". Then it's much clearer.

起风了 2024-10-14 19:38:01

从前有一个人,名叫 Boole。他有兴趣用真值“计算”,就像其他人用自然数计算一样。作为一名数学家,他知道必须做什么来定义新的“微积分”或“代数”:找到一组,并找到一组运算 基于这些值,使得对某些值应用运算再次产生基集的值。

他的新代数的值集仅包含两个:TrueFalse。然后他发明了运算“与”、“或”和“非”。为了与代数的数学规则保持一致,运算必须对值集中的任何输入(无论它们有两个还是一个)进行运算,并从值集中产生一个结果。由于在这种情况下没有更好的方法来实现这一点,因此对于每个操作,他列出了所有可能的参数值并为它们分配了一个结果。这就是您在真值表中看到的内容。

现在,正如您所发现的,结果的分配有点随意。有些可以被常识所激励,有些则不能。 (其他海报试图激发你的一些“红点”的结果)。不要为此挣扎太多。如果你能在常识逻辑中找到动机,那么很好,如果没有,也别担心。布尔必须以某种方式修补他的桌子,以使他的操作“完整”。将“3+4”的结果命名为“7”也没有真正的逻辑。这就是为什么我们要记住这些基本的东西:)。

There once was a guy named Boole. He was interested to "calculate" with truth values, like others calculated with natural number. As he was a mathematician, he knew what he had to do to define a new "calculus" or "algebra": Find a set of values, and find a set of operations upon these values, such that applying an operation to some value(s) yields again a value of the base set.

The set of values for his new algebra just consisted of two, True and False. Then he invented the operations, "and", "or", and "not". To be consistent with mathematical rules of algebra, the operations had to work on any input from the value set (whether they have two inputs or one), and produce a result from the value set. And as there are no better means to achieve this in this case, for each operation he listed all the possible argument values and assigned them a result. That's what you see in the truth tables.

Now, as you have found out, the assignment of the results is a bit arbitrary. Some can be motivated by common sense, some can't. (Other posters have tried to motivate the results of some of your "red dots"). Don't struggle too hard with it. If you can find a motivation in common sense logic, then fine, if not, don't worry. Boole had to patch up his tables somehow, to make his operations "total". There is also no real logic behind giving the outcome of "3+4" the name "7". That's why we memorize these basic things :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文