Python03_运算符和随机数

"""
1.身份运算符
    is 和 is not
is 与 == 区别:
is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。
"""
a = 10
b = 10

if a == b:
    print(a, "==", b)
    pass

if a is b:
    print(a, "is", b)
    pass

"""
2.成员运算符
    in 和 not in
"""
if 11 not in range(1, 10):
    print("11 not in range(1,10)")
else:
    print("false")

"""
3.海象运算符 := ,可在表达式内部为变量赋值。
"""
list1 = [1, 2, 3, 4, 5]
if (n := len(list1)) < 10:
    print("n: ", n)

"""
4. and运算符 比 or运算符 优先级更高
"""
if True or False and False:
    print("and have higher priority compared with or")

"""
5. 在交互模式中,最后被输出的表达式结果被赋值给变量 _ 

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
"""

"""
随机数字
"""
import random
rand1 = random.choice(range(10))     # 从0到9中随机挑选一个整数
print(rand1)                         # 7

rand2 = random.randint(1000, 9999)   # 指定范围
print(rand2)                         # 5234

lst = random.sample('abcd1234', 4)   # 随机取样4个
print(''.join(lst))                  # 412c

# round()   4舍6入5看齐,奇进偶不进
print(round(10.5))      # 10
print(round(11.5))      # 12

你可能感兴趣的:(Python03_运算符和随机数)