Python入门第10课——条件判断初步(只读课堂)

    科技2022-07-16  108

    In [1]: #欢迎来到“只读课堂”!

     

    In [2]: #今天终于到了程序结构必备的——条件判断!

     

    In [3]: #在任何程序中,我们都会遇到条件判断。就像在生活中选择:比如说,去超市买东西,买单时服务员问,有会员卡吗;比如说,“没时间解释了,快上车”,司机却问你,有车票没。

     

    In [4]: #————————————————————————————————————————————————————————————————

     

    In [5]: #伪代码

     

    In [6]: #并不能真正地运行,但是能表达我们的意思。

     

    In [7]: if condition:

       ...: do something

    File "<ipython-input-7-a1f4069a9516>", line 2

    do something

    ^

    SyntaxError: invalid syntax

     

     

    In [8]: else:

    File "<ipython-input-8-ad2808a68c00>", line 1

    else:

    ^

    SyntaxError: invalid syntax

     

     

    In [9]: do somthing

    File "<ipython-input-9-59ebcb6cb733>", line 1

    do somthing

    ^

    SyntaxError: invalid syntax

     

     

    In [10]: #这里报的错先不管啊,它的意思就是说:如果怎样,就怎样,否则......

     

    In [11]: #举个例子:

     

    In [12]: #应用题:有一个人买水果,合计金额为32.5元,水果店搞活动,满30打九折,求这个人的实际花费。

     

    In [13]: #先用个变量

     

    In [14]: total_cost = 32.5

     

    In [15]: if total_cost > 30:

        ...: discount = 0.9

        ...: else:

    File "<ipython-input-15-2e2f0e6ae667>", line 3

    else:

    ^

    SyntaxError: invalid syntax

     

     

    In [16]: #上面的不管啊,现在我的python演示程序下,每行没打1就视为运行。我们重新写

     

    In [17]: total_cost = 32.5

     

    In [18]: if total_cost > 30:

        ...: discount = 0.9

        ...: else:

    File "<ipython-input-18-2e2f0e6ae667>", line 3

    else:

    ^

    SyntaxError: invalid syntax

     

     

    In [19]: if total_cost > 30:

        ...: discount = 0.9

        ...: else:

        ...: discount = 1

    File "<ipython-input-19-dc16ae646d70>", line 3

    else:

    ^

    SyntaxError: invalid syntax

     

     

    In [20]: #我不知道为什么会这样啊,因为Spider上运行是可以的。

     

    In [21]: total_cost *= discount

    ---------------------------------------------------------------------------

    NameError Traceback (most recent call last)

    <ipython-input-21-8ca0879c0e7d> in <module>()

    ----> 1 total_cost *= discount

     

    NameError: name 'discount' is not defined

     

    In [22]: #...

     

    In [23]: print('实际花费{}'.format(total_cost))

    实际花费32.5

     

    In [24]: #那就:

     

    In [25]: #如果购买超过30元,打九折,超过50元,打八折,求实际花费。

     

    In [26]: total_cost = 30

     

    In [27]: if total_cost > 32.5:

        ...: discount = 0.8

        ...: elif total_cost > 30:

        ...: discount = 0.9

        ...: else:

        ...: discount = 1

        ...:

     

    In [28]: total_cost *= discount

     

    In [29]: print("实际花费为:{}元".format(total_cost))

    实际花费为:30元

     

    In [30]: #今天的有点问题

     

    In [31]: total_cost = 32.5

     

    In [32]: if total_cost > 50:

        ...: discount = 0.8

        ...: elif total_cost > 30:

        ...: discount = 0.9

        ...: else:

        ...: discount = 1

        ...:

     

    In [33]: total_cost *= discount

     

    In [34]: print("实际花费为:{}元".format(total_cost))

    实际花费为:29.25元

     

    In [35]: #这样就成功了!

     

    In [36]: #————————————————————————————————————————————————————————————————

     

    In [37]: #重点:

     

    In [38]: #1.条件判断可以任意组合

     

    In [39]: #第一层意思:elif可以有0到任意多个,else可有可无

     

    In [40]: #第二层意思:条件判断可以进行嵌套

     

    In [41]: #2.着重来看一下contition,就是条件判断

     

    In [42]: #————————————————————————————————————————————————————————————————

     

    In [43]: #condition

     

    In [44]: bool(''),bool({}),bool([])

    Out[44]: (False, False, False)

     

    In [45]: condition = ''

     

    In [46]: if condition:

        ...: print('True')

        ...: else:

        ...: print('False')

        ...:

    False

     

    In [47]: #它输出了False,也就是说,condition是空字符的时候就是False

     

    In [48]: condition = 'bool'

     

    In [49]: if condition:

        ...: print('True')

        ...: else:

        ...: print('False')

        ...:

    True

     

    In [50]: #它更倾向于有,或者无的因素

     

    In [51]: #————————————————————————————————————————————————————————————————

     

    In [52]: #布尔(bool)值也是可以做and or not运算的

     

    In [53]: and or not

    File "<ipython-input-53-150e47ad425e>", line 1

    and or not

    ^

    SyntaxError: invalid syntax

     

     

    In [54]: #光直接写是不行的。

     

    In [55]: #下面先举个例子:布尔型变量做运算

     

    In [56]: a = True

     

    In [57]: b = False

     

    In [58]: print('a and b is {}'.format(a and b))

    a and b is False

     

    In [59]: #如果改成or:

     

    In [60]: print('a or b is {}'.format(a and b))

    a or b is False

     

    In [61]: #它是一样的

     

    In [62]: #————————————————————————————————————————————————————————————————

     

    In [63]: #非布尔(bool)型变量做and or not运算

     

    In [64]: a = 'hello world'

     

    In [65]: b = [1,2,3]

     

    In [66]: print('a and b is {}'.format(a and b))

    a and b is [1, 2, 3]

     

    In [67]: #它打印出来的内容是b的变量

     

    In [68]: print('a or b is {}'.format(a or b))

    a or b is hello world

     

    In [69]: #这样打印出来的内容就是a的变量

     

    In [70]: #它在and or not运算中跟别的变量不同,如果是and,就会返回b的内容;如果是or,就会返回a的内容。即使变量内容是空的或空列表。

     

    In [71]: #————————————————————————————————————————————————————————————————

     

    In [72]: #总结:

     

    In [73]: #非布尔型变量 and 运算

     

    In [74]: a = [1,2,3]

     

    In [75]: b = 10

     

    In [76]: print(b and a)

    [1, 2, 3]

     

    In [77]: #非布尔型变量 or 运算

     

    In [78]: a = 'nihao'

     

    In [79]: b = {'apple':100}

     

    In [80]: print(a or b)

    nihao

     

    In [81]: #非布尔型变量 not 运算

     

    In [82]: print(not b)

    False

     

    In [83]: #————————————————————————————————————————————————————————————————

     

    In [84]: #讲解 “ 总结 ”

     

    In [85]: #如果前面加一个not

     

    In [86]: not ''

    Out[86]: True

     

    In [87]: #这时候,not永远给我们返回的呢,就是True或False

     

    In [88]: not '2342'

    Out[88]: False

     

    In [89]: #如果有内容的话,它就会返回False

     

    In [90]: not []

    Out[90]: True

     

    In [91]: #如果是控列表,它就认为是False,not 之后就会返回相反的值。

     

    In [92]: #————————————————————————————————————————————————————————————————

     

    In [93]: #下面我们讲一个条件判断的“近亲”——断言

     

    In [94]: if not condition:

        ...: crash program

    File "<ipython-input-94-c29fd62d1cf3>", line 2

    crash program

    ^

    SyntaxError: invalid syntax

     

     

    In [95]: #它的意思是:我断言它肯定是这样的,如果不是这样,那就崩溃

     

    In [96]: #这里是个伪代码,肯定就崩溃。

     

    In [97]: age = 18

     

    In [98]: assert age == 18

     

    In [99]: #是真的,它就不输出结果;

     

    In [100]: age = 19

     

    In [101]: assert age == 18

    ---------------------------------------------------------------------------

    AssertionError Traceback (most recent call last)

    <ipython-input-101-c065aeca80a1> in <module>()

    ----> 1 assert age == 18

     

    AssertionError:

     

    In [102]: #不是真的,它就崩溃了。

     

    In [103]: #如果你想要备注,你就这样加一句:他竟然不是18岁

     

    In [104]: assert age == 18, '他竟然不是18岁'

    ---------------------------------------------------------------------------

    AssertionError Traceback (most recent call last)

    <ipython-input-104-274a5585407f> in <module>()

    ----> 1 assert age == 18, '他竟然不是18岁'

     

    AssertionError: 他竟然不是18岁

     

    In [105]: print("\n本次“只读课堂”的python教程就到这了,欢迎继续收看!\n")

     

    本次“只读课堂”的python教程就到这了,欢迎继续收看!

    Processed: 0.011, SQL: 8