6 数据应用

标签:tuple   class   ==   div   一个   code   运算符   nbsp   逻辑运算符   

1 四则运算:
a = 9
b = 2
print(a + b)
print(a - b)
print(a * b)
print(a / b)    # 正常除法
print(a // b)   # 向下取整
print(a % b)    # 取模,即除法的余数
print(a ** b)   # 幂,a的b次幂

结果:

11
7
18
4.5
4
1
81

2 比较运算:

a = 9
b = 2
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

结果:

False
True
True
False
True
False

3 赋值运算符:

a = 10
a += 1     # a = a + 1
print(a)
a -= 1      # a = a - 1
print(a)
a *= 3      # a = a * 3
print(a)
a /= 3      # a = a / 3
print(a)
a %= 4      # a = a % 4
print(a)
a **= 2      # a = a ** 2
print(a)
a //= 2      # a = a // 2
print(a)

结果:

11
10
30
10.0
2.0
4.0
2.0

4 逻辑运算符:

a = 9
b = 2
c = None

if(a>5 and b>6):
    print(true)
else:
    print(false)

if(a>5 or b>5):
    print(true)
else:
    print(false)

if(c is not None):
    print(true)
else:
    print(false)  

结果:

false
true
false

5 成员运算符:

my_tuple = (1, 2, 3, 4, 5)

if(1 in my_tuple):
    print(true)
else:
    print(false)

if(10 not in my_tuple):
    print(true)
else:
    print(false)

结果:

true
true

6 身份运算符

a = [10, 20, 30]
b = [10, 20, 30]

if(a is b):
    print(true)
else:
    print(false)
print(id(a))    # 内存地址
print(id(b))

结果:

false
44016968
43987272

is 用于判断两个变量是否同一个内存地址, == 用于判断变量的值是否相等。

6 数据应用

标签:tuple   class   ==   div   一个   code   运算符   nbsp   逻辑运算符   

原文地址:https://www.cnblogs.com/lizitestdev/p/14295182.html

版权声明:完美者 发表于 2021-01-19 12:17:21。
转载请注明:6 数据应用 | 完美导航

暂无评论

暂无评论...