4、python的各种运算

字符串的运算:加法,成员运算,比较运算,逻辑运算

加法
x2 = "abcd" + "edf"
print(x2)  # x = "abcdedf"
x2 += "123"
print(x2)  # x = "abcdedf123"
x3 = "ABCD" * 3   #   x = "ABCDABCDABCD"
y3 = 2 * '123'   # y = '123123'
x4 = '123'
x4 *= 2   #  x = '123123'
y4 = 3
y4 *= 'ABC'  # y = 'ABCABCABC'
身份运算
x=10
y=10
m='a'
n='a'
p='A'
print(x is y)
print(m is n)
print(n is not p)
print(n is  p)
比较运算
print('A'=='a')
print('A'!='a')
print('A'<='a')
print('A' < 'B' )  #  True
print('AB' <= 'AC')  # True
print('abd' <= 'aca' ) # True
print('ab' < 'abc' ) # True
print('a'>'1')
print('A'<'a')
print('2'<'B'<'b')
成员运算
x1 = "welcome to beijing!"
print('to' in x1)  #  True
print('e t' in x1)   #  True
print('hello' in x1)   # False
print('hello' not in x1)  # True

比较运算
result=2>1
r1=5>4
print(id(r1))
print(r1)
print(r1==result)
print(id(result))
print(result)
逻辑运算
print(True and False)  # False
print(True and True)   #  True
print(True or False)   #  True
print("" or False)     #  False
print(None or True)   #True
print(None or False)   #  False
print(0 or False)   #  False
print(()or False)   #  False
print({} or False)   #  False
'''
逻辑操作符and 和or 也称作短路操作符(short-circuitlogic)或者惰性求值(lazy evaluation):它们的参数从左向右解析,一旦结果可以确定就停止。(与是所有都为true才会有结果,或只要有一个是true就有结果)
3 and 4    —>4   ,     3 or 4 —->3  
and运算符必须所有的运算数都是true才会把所有的运算数都解析,并且返回最后一个变量,即为4;改变一下顺序4 and 3 ,结果也不一样,即为3.
或逻辑(or),即只要有一个是true,即停止解析运算数,返回最近为true的变量,即 3 or 4,值为3;改变顺序4 or 3 即为4.
'''
print(3 and 4)   #4
print(4 and 3)    #3
print(3 or 4)   #3
print(4 or 3)    #4
print(not(4 or 3))  #False

print('a' and 'b')
print('b' and 'a')
print('a' or 'b')
print('b' or 'a')
print(not('a' or 'b'))

你可能感兴趣的:(4、python的各种运算)