那些你不知道python知识点

三元运算符

【例子】

x, y = 4, 5
if x < y:
    small = x
else:
    small = y

print(small)  # 4

有了这个三元操作符的条件表达式,你可以使用一条语句来完成以上的条件判断和赋值操作。

【例子】

x, y = 4, 5
small = x if x < y else y
print(small)  # 4

其他运算符

操作符名称示例
in存在'A' in ['A', 'B', 'C']
not in不存在'h' not in ['A', 'B', 'C']
is"hello" is "hello"
not is不是"hello" is not "hello"

【例子】

letters = ['A', 'B', 'C']
if 'A' in letters:
    print('A' + ' exists')
if 'h' not in letters:
    print('h' + ' not exists')

# A exists
# h not exists

【例子】比较的两个变量均指向不可变类型。

a = "hello"
b = "hello"
print(a is b, a == b)  # True True
print(a is not b, a != b)  # False False

这里解释一下,字符串是不可变类型。参照java语言,操作系统内会有一个常量池,专门保存一些常量,当第一个“hello”被创建时,解释器会查询常量池,若存在,则直接引用。若不存在则创建。所以a和b实际的内存地址是一样的。

详情参照—java数据类型浅析(一)

我们可以通过id()查看变量内存地址

【例子】比较的两个变量均指向可变类型。

a = ["hello"]
b = ["hello"]
print(a is b, a == b)  # False True
print(a is not b, a != b)  # True False

对于变量来说,解释器会在堆中分别创建a、b的引用。自然内存地址不一样

注意:

  • is, is not 对比的是两个变量的内存地址
  • ==, != 对比的是两个变量的值
  • 比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。
  • 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。

运算符的优先级

运算符描述
**指数(最高优先级)
按位翻转,一元加号和减号
* / % //乘,除,取模和取整除)
+ -加法减法
>> <<右移,左移运算符
&位‘AND’
^|位运算符
<=<>>=比较运算符
<>==!=等于运算符
=%=/=//=-=+=*=**=赋值运算符
is is not身份运算符
in not in成员运算符
not and or逻辑运算符

布尔型

布尔 (boolean) 型变量只能取两个值,TrueFalse。当把布尔型变量用在数字运算中,用 10 代表 TrueFalse

【例子】

print(True + True)  # 2
print(True + False)  # 1
print(True * False)  # 0

除了直接给变量赋值 TrueFalse,还可以用 bool(X) 来创建变量,其中 X 可以是

  • 基本类型:整型、浮点型、布尔型
  • 容器类型:字符串、元组、列表、字典和集合

【例子】bool 作用在基本类型变量:X 只要不是整型 0、浮点型 0.0,布尔型、Falsebool(X) 就是 True,其余就是 False

print(type(0), bool(0), bool(1))
# <class 'int'> False True

print(type(10.31), bool(0.00), bool(10.31))
# <class 'float'> False True

print(type(True), bool(False), bool(True))
# <class 'bool'> False True

【例子】bool 作用在容器类型变量:X 只要不是空的变量,bool(X) 就是 True,其余就是 False

print(type(''), bool(''), bool('python'))
# <class 'str'> False True

print(type(()), bool(()), bool((10,)))
# <class 'tuple'> False True

print(type([]), bool([]), bool([1, 2]))
# <class 'list'> False True

print(type({}), bool({}), bool({'a': 1, 'b': 2}))
# <class 'dict'> False True

print(type(set()), bool(set()), bool({1, 2}))
# <class 'set'> False True

while - else 循环

while 布尔表达式:
    代码块
else:
    代码块

while循环正常执行完的情况下,执行else输出,如果while循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容。

【例子】

count = 0
while count < 5:
    print("%d is  less than 5" % count)
    count = 6
    break
else:
    print("%d is not less than 5" % count)

# 0 is  less than 5

for - else 循环

for 迭代变量 in 可迭代对象:
    代码块
else:
    代码块

for循环正常执行完的情况下,执行else输出,如果for循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容,与while - else语句一样。

for num in range(10, 20):  # 迭代 10 到 20 之间的数字
    for i in range(2, num):  # 根据因子迭代
        if num % i == 0:  # 确定第一个因子
            j = num / i  # 计算第二个因子
            print('%d 等于 %d * %d' % (num, i, j))
            break  # 跳出当前循环
    else:  # 循环的 else 部分
        print(num, '是一个质数')

# 10 等于 2 * 5
# 11 是一个质数
# 12 等于 2 * 6
# 13 是一个质数
# 14 等于 2 * 7
# 15 等于 3 * 5
# 16 等于 2 * 8
# 17 是一个质数
# 18 等于 2 * 9
# 19 是一个质数

推导式

除了熟知发的列表推导式外还有字典、元组、集合推导式

列表推导式
[ expr for value in collection [if condition] ]

【例子】

x = [-4, -2, 0, 2, 4]
y = [a * 2 for a in x]
print(y)
# [-8, -4, 0, 4, 8]
元组推导式
( expr for value in collection [if condition] )

【例子】

a = (x for x in range(10))
print(a)

# <generator object <genexpr> at 0x0000025BE511CC48>

print(tuple(a))

# (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
字典推导式
{ key_expr: value_expr for value in collection [if condition] }

【例子】

b = {i: i % 2 == 0 for i in range(10) if i % 3 == 0}
print(b)
# {0: True, 3: False, 6: True, 9: False}
集合推导式
{ expr for value in collection [if condition] }

【例子】

c = {i for i in [1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}
print(c)
# {1, 2, 3, 4, 5, 6}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值