运算和运算符
运算:一个或者一个以上的数据经过变化得到一个新值的过程。运算符:运算的符号就是运算符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| # + 加法运算符 result = 88 + 77 print(result)
# - 减法运算 result = 88 - 77 print(result)
# * 乘法运算符 result = 9 * 11 print(result)
# / 除法运算符(浮点类型) result = 100 / 5 print(result)
# ** 幂运算 result = 5 ** 4#result = 5 * 5 * 5 * 5 print(result)
# // 地板除运算符 result = 47 // 10 print(result)
# % 取余运算符 result = 47 % 10 print(result)
#python 没有自增和自减运算符号
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #1. > 大于运算符 result = 77 > 155 print(result)
#2. < 小于运算符 result = 14 < 19 print(result)
#3. >= 大于等于运算符 result = 15 >= 54 print(result)
#4. <= 小于等于运算 result = 45 <= 16 print(result)
#5. == 等于运算符 (六大类型不相同就不等于) result = 55 == '55' print(result)
#6. != 不等于运算符 result = 55 != 55 print(result)
#扩展:连续的比较运算 result = 44 < 55 < 33 # -> 44 < 55 并且 55 < 33 print(result)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #1.普通赋值运算 var = 250 print(var)
#2.加法赋值运算 var = 50 var += 10#var = var + 10 print(var)
#3.减法赋值运算 var = 100 var -= 88#var = var - 88 print(var)
#4.乘法赋值运算 var = 25 var *= 4#var = var * 4 print(var)
#5.除法赋值运算 var = 99 var /= 11#var = var / 11 print(var)
#6.取余赋值运算 var = 26 var %= 5#var = var % 5 print(var)
#7.取商赋值运算 var = 33 var //= 6#var = var // 6 print(var)
#8.幂赋值运算 var = 5 var **= 3#var = var ** 3 print(var)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #1.逻辑与运算(二目运算)-》有假则假 var1 = False var2 = False result = var1 and var2 print(result) ''' True and True = True False and True = False True and False = False False and False = False '''
#2.逻辑或运算 -》 有真则真 var1 = False var2 = False result = var1 or var2 print(result) ''' True or True = True False or True = True True or False = True False or False = False '''
#3.逻辑非运算(单目运算)-》真变假假变真 var = False result = not var print(result)
python不支持逻辑异或运算
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #1,按位与运算 var1 = 25#11001 var2 = 12#01100 result = var1 & var2 print(result)
#2.按位或运算 var1 = 25 var2 = 12 result = var1 | var2 print(result)
#3.按位非运算 var = 8 #1000 result = ~var print(result)
#4.按位异或运算 var1 = 25 var2 = 12 result = var1 ^ var2 print(result)
#5.左移运算 相当于乘以2的运算 var = 48 result = var << 2 print(result)
#6.右移运算 相当于地板除2 var = 36 result = var >> 3 print(result)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #id标识 系统为数据分配的编号 #is 检测是同一个 var1 = 'daxigua' var2 = 'daxigua' result = var1 is var2 print(result) #查看id标识 print(id(var1),id(var2)) #注意:3.7以上版本 整数浮点数和字符串只要值一样,id标识就一样
#is not 检测是否不是同一个 var1 = 99 var2 = 98 result = var1 is not var2 print(result) #查看id标识 print(id(var1),id(var2))
#其他数据类型 列表,元组,字典,集合 数据相同,id标识也不同 var1 = [1,2,3] var2 = [1,2,3] result = var1 is var2 print(result) #查看id标识 print(id(var1),id(var2))
#连续赋值的列表,元组,字典集合id标识相同 var1 = var2 = [2,3] result = var1 is var2 print(result)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #成员检测运算 #in 检测是否存在于容器中 #列表 #IT = ['PHP','JAVA','PYTHON','C++','C#','C'] #元组 #IT = ('PHP','JAVA','PYTHON','C++','C#','C') #集合 #IT = {'PHP','JAVA','PYTHON','C++','C#','C'} #字符串 #IT = 'PHPJAVAPYTHONC++C#C' #字典(字典的成员运算仅仅进行键的检测) IT = {'a':'PHP','b':'JAVA','c':'PYTHON','d':'C++','e':'C#','f':'C'} result = 'a' in IT print(result)
#not in 检测是否不再容器当中 IT = {'PHP','JAVA','PYTHON','C++','C#','C'} result = 'lua' not in IT print(result)
|