Python基础知识(五)

逻辑

逻辑术语

在 python 中我们会用到下面的术语(字符或者词汇)来定义事物的真(True)或者假(False)。计算机的逻辑就是在程序的某个位置检查这些字符或者变量组合在一起表达的结果是真是假。

and 与
or 或
not 非
!= (not equal) 不等于
== (equal) 等于
= (greater-than-equal) 大于等于
<= (less-than-equal) 小于等于
True 真
False 假

真值表

我们将使用下面这些字符来创建你需要记住的真值表:

NOT	        TRUE
not False	True
not True	False
OR	        TRUE?
True or False	True
True or True	True
False or True	True
False or False	False
AND	        TRUE
True and False	False
True and True	True
False and True	False
False and False	False
NOT OR	                TRUE
not (True or False)	False
not (True or True)	False
not (False or True)	False
not (False or False)	True
NOT AND	                TRUE
not (True and False)	True
not (True and True)	False
not (False and True)	True
not (False and False)	True
!=	TRUE
1 != 0	True
1 != 1	False
0 != 1	True
0 != 0	False
==	TRUE
1 == 0	False
1 == 1	True
0 == 1	False
0 == 0	True

布尔逻辑表达式

先为下面的每一个逻辑问题写出你认为的答案,每一题的答案要么为True要么为False。写完以后,你需要将python运行起来,把这些逻辑语句输入进去,确认你写的答案是否正确。

True and True
False and True
1 == 1 and 2 == 1
"test" == "test"
1 == 1 or 2 != 1
True and 1 == 1
False and 0 != 0
True or 1 == 1
"test" == "testing"
1 != 0 and 2 == 1
"test" != "testing"
"test" == 1
not (True and False)
not (1 == 1 and 0 != 1)
not (10 == 1 or 1000 == 1000)
not (1 != 10 or 3 == 4)
not ("testing" == "testing" and "Zed" == "Cool Guy")
1 == 1 and (not ("testing" == 1 or 1 == 0))
"chunky" == "bacon" and (not (3 == 4 or 3 == 3))
3 == 3 and (not ("testing" == "testing" or "Python" == "Fun"))

结果

True
False
False
True
True
True
False
True
False
False
True
False
True
False
False
False
True
True
False
False

所有的布尔逻辑表达式都可以用下面的简单流程得到结果:

  • 找到相等判断的部分 (== 或者 !=),将其改写为其最终值 (True 或 False)。
  • 找到括号里的 and/or,先算出它们的值。
  • 找到每一个not,算出他们反过来的值。
  • 找到剩下的 and/or,解出它们的值。
  • 等你都做完后,剩下的结果应该就是 True 或者 False 了。

常见问题
Q: 为什么and 返回 "test"以及and 返回 1 而不是 True?

python和很多语言可以返回布尔表达式中的一个操作数,而不仅仅是真或假。这意味着如果你计算False and 1 你会得到表达式的第一个操作数 (False) ,但是如果你计算True and 1的时候,你得到它的第二个操作数(1)。试一试吧。

Q:!=和 <>有什么不同吗?

Python已经声明赞成使用!=而弃用<>所以尽量使用!=吧。其他的应该没有区别了。

Q:有没有捷径去判断布尔表达式的值?

有的。任何的and表达式包含一个False结果就是False,任何or表达式有一个True结果就是True,你就可以在此处得到结果,但要确保你能处理整个表达式,因为后面这是一个很有用的技能。

IF 语句

people = 20
cats = 30
dogs = 15

if people < cats:
    print ("Too many cats! The world is doomed!")

if people > cats:
    print ("Not many cats! The world is saved!")

if people < dogs:
    print ("The world is drooled on!")

if people > dogs:
    print ("The world is dry!")

dogs += 5

if people >= dogs:
    print ("People are greater than or equal to dogs.")

if people <= dogs:
    print ("People are less than or equal to dogs.")

if people == dogs:
    print ("People are dogs.")

结果

Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

常见问题
Q: +=表示什么意思?

代码x += 1和x = x + 1 实现的是一样的功能,但是可以少输入一些字符。你可以称之为“增量”操作符。-= 也是相同的。

Else 和 If

把下面这段写下来,并让它运行起来:

people = 30
cars = 40
trucks = 15

if cars > people:
    print ("We should take the cars.")
elif cars < people:
    print ("We should not take the cars.")
else:
    print ("We can't decide.")

if trucks > cars:
    print ("That's too many trucks.")
elif trucks < cars:
    print ("Maybe we could take the trucks.")
else:
    print ("We still can't decide.")

if people > trucks:
    print ("Alright, let's just take the trucks.")
else:
    print ("Fine, let's stay home then.")

结果

We should take the cars.
Maybe we could take the trucks.
Alright, let's just take the trucks.

常见问题
Q: 如果多个elif块为真,会怎样?

Python的启动和运行只会针对第一个为真的代码块,所以你说的那种情况,只会执行第一块。

嵌套if elif

print ("You enter a dark room with two doors.  Do you go through door #1 or door #2?")

door = input("> ")

if door == "1":
    print ("There's a giant bear here eating a cheese cake.  What do you do?")
    print ("1. Take the cake.")
    print ("2. Scream at the bear.")

    bear = input("> ")

    if bear == "1":
        print ("The bear eats your face off.  Good job!")
    elif bear == "2":
        print ("The bear eats your legs off.  Good job!")
    else:
        print ("Well, doing %s is probably better.  Bear runs away." % bear)

elif door == "2":
    print ("You stare into the endless abyss at Cthulhu's retina.")
    print ("1. Blueberries.")
    print ("2. Yellow jacket clothespins.")
    print ("3. Understanding revolvers yelling melodies.")

    insanity = input("> ")

    if insanity == "1" or insanity == "2":
        print ("Your body survives powered by a mind of jello.  Good job!")
    else:
        print ("The insanity rots your eyes into a pool of muck.  Good job!")

else:
    print ("You stumble around and fall on a knife and die.  Good job!")

结果

You enter a dark room with two doors.  Do you go through door #1 or door #2?
> 1
There's a giant bear here eating a cheese cake.  What do you do?
1. Take the cake.
2. Scream at the bear.
> 1
The bear eats your face off.  Good job!
You enter a dark room with two doors.  Do you go through door #1 or door #2?
> 1
There's a giant bear here eating a cheese cake.  What do you do?
1. Take the cake.
2. Scream at the bear.
> 2
The bear eats your legs off.  Good job!
You enter a dark room with two doors.  Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu's retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 1
Your body survives powered by a mind of jello.  Good job!

You enter a dark room with two doors.  Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthulhu's retina.
1. Blueberries.
2. Yellow jacket clothespins.
3. Understanding revolvers yelling melodies.
> 3
The insanity rots your eyes into a pool of muck.  Good job!

常见问题

Q: 可以用if-else替换elif 吗?

某些情况下可以, 但是这个也依赖于每一个if/else是怎么写的 。这也意味着, Python会检查每个if-else的组合,而不是只检查if-elif-else组合中的第一个为假的分支,尝试用两种方式多编写一些代码,以找出他们的不同点。

Q:我怎么知道一个数字是在一个数字范围之间?

有两种方法: 一种经典的方式是使用0 < x < 10 或者 1 <= x < 10,另一中方式是使用x in range(1, 10)。

Q: 怎样才能在if-elif-else代码块中增加更多的选择?

为每一个可能的选择增加一个elif 代码块。

猜你喜欢

转载自blog.csdn.net/weixin_43093289/article/details/83579285