May 8th

1.逻辑运算符

and  or  not

(1) and操作符左右的操作数同时为真时,结果为真。

(2) or操作符只需要左右两边任意一边为真,结果就为真。

(3) 如果 x 为假,表达式会返回 x 的值(0),否则它就会返回 y 的值。3 and 4 == 4 , 3 or 4 == 3

(4) 优先级:not > and > or

例题:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 = ?

  ==(not1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)

  ==0 or 0 or 4 or 6 or 9

  ==4

2.assert 的作用

'assert'叫“断言”,当这个关键字后面的的条件为假的时候,程序会自动崩溃并且抛出一个AssertionError的异常,所以当我们调试程序的时候,我们与其让错误的条件导致所有的程序完全崩溃,不如在错误条件出现的一瞬间实现“自爆”。

3.break和continue在循环中的作用

break语句是终止当前循环,跳出循环体。

continue语句是终止本轮循环并进行下一轮循环(在开始下一轮循环之前,会先测试循环条件)

4.列表可以存放什么东西?

在Python中,列表可以看做是一个打了激素的数组,如果把数组比喻成集装箱,那么列表就是一个大仓库。所以在列表中我们可以存放任何数据类型。

5.在列表里增加元素的方法有哪些?

(1) append(),是将参数作为一个元素增加到列表的末尾。

例如: >>>name = ['r','f','h']

    >>>name.append(['z','h','j'])

    >>>name

    ['r','f','h',['z','h','j']]

(2) extend(),是将参数作为一个列表去扩展列表的末尾。

例如: >>>name = ['r','f','h']

    >>>name.extend(['z','h','j'])

    >>>name

    ['r','f','h','z','h','j']

(3)insert(),可以往列表的任意位置插入元素。

例如: >>>name = ['r','f','h']

    >>>name.insert(1,'z')

    >>>name

    ['r','z','f','h']


猜你喜欢

转载自blog.csdn.net/weixin_42152081/article/details/80245513
8th