龙空技术网

Python语法基础(2)运算符

AI异构 128

前言:

当前各位老铁们对“pythonlist相减”都比较关切,我们都需要学习一些“pythonlist相减”的相关内容。那么小编在网络上网罗了一些有关“pythonlist相减””的相关知识,希望咱们能喜欢,你们一起来学习一下吧!

Python语言支持以下类型的运算符:

算术运算符比较(关系)运算符赋值运算符逻辑运算符位运算符成员运算符身份运算符算术运算符

# coding=utf-8#两个数字相加sumNumber=1+2print(sumNumber)      #输出结果:3#两个字符串相加sumString="Nice" + "work"print(sumString)      #输出结果:Nicework#两个数字相减subNumber=2-1print(subNumber)      #输出结果:1#两个数字相乘或者字符串重复multiplicationNumber=2*3print(multiplicationNumber)      #输出结果:6multiplicationString="hello"*2print(multiplicationString)      #输出结果:hellohello#两个数相除divisionNumber=9/2print(divisionNumber)      #输出结果:4divisionNumber=9.0/2print(divisionNumber)      #输出结果:4.5divisionNumber=9/2.0print(divisionNumber)      #输出结果:4.5#/---除数或被除数中有任意一个是小数的话,商也会保留小数,反之取整---/#除法运算// 返回商的整数部分,抛弃余数divisorNumber=10//3print(divisorNumber)        #输出结果:3#除法运算% 返回商的余数部分,抛弃商divisorNumber=10%3print(divisorNumber)        #输出结果:1divisorNumber=10%1print(divisorNumber)        #输出结果:0 /--没有余数则返回0--/divisorNumberx=10//3         #divisorNumberx是商的整数部分divisorNumbery=10%3         #divisorNumbery是余数divisorNumberz=3*divisorNumberx+divisorNumbery #divisorNumberz是除数乘以商的整数部分加上余数,得到的divisorNumberz的值就是被除数print(divisorNumberz)        #输出结果:10#求幂运算powerNumber=2**3 #相当于2的3次幂,就是2*2*2 关于幂运算大家应该在数学里都很熟悉了print(powerNumber)       #输出结果:8
3Nicework16hellohello4.54.54.5310108
比较(关系)运算符
#小于符号,返回值是bool值lessThan=1<2print(lessThan)        #输出结果:TruelessThan=1<1print(lessThan)        #输出结果:False#大于符号,返回值是bool值moreThan=2>1print(moreThan)        #输出结果:TruemoreThan=2>2print(moreThan)        #输出结果:False#不等于符号 返回值是Bool值notEqual=1!=2print(notEqual)        #输出结果:TruenotEqual=1!=1print(notEqual)        #输出结果:False
TrueFalseTrueFalseTrueFalse
赋值运算符
#!/usr/bin/python# -*- coding: UTF-8 -*-a = 21b = 10c = 0c = a + bprint("1 - c 的值为:", c)c += aprint("2 - c 的值为:", c)c *= aprint("3 - c 的值为:", c)c /= aprint("4 - c 的值为:", c)c = 2c %= aprint("5 - c 的值为:", c)c **= aprint("6 - c 的值为:", c)c //= aprint("7 - c 的值为:", c)
1 - c 的值为: 312 - c 的值为: 523 - c 的值为: 10924 - c 的值为: 52.05 - c 的值为: 26 - c 的值为: 20971527 - c 的值为: 99864
逻辑运算符
#逻辑非 notoperationx=Trueoperationy=not operationxprint(operationy)        #输出结果:Falseoperationz=Falseprint(not operationz)        #输出结果:True#逻辑与 andprint(True and True)        #输出结果:True#逻辑或 orprint(False or False)        #输出结果:False
FalseTrueTrueFalse
位运算符
#按位与运算&, 按位与是指一个数字转化为二进制,然后这些二进制的数按位来进行与运算operationNumber=7&18print(operationNumber)        #输出结果:2#按位或运算|, 按位或是指一个数字转化为二进制,然后这些二进制的数按位来进行或运算operationNumber=7|18print(operationNumber)        #输出结果:23   #结题思路和按位与运算的一样,可以参考按位与运算#按位异或operationNumber=7^18print(operationNumber)        #输出结果:21   #结题思路和按位与运算的一样,可以参考按位与运算#按位翻转 ~   按位翻转公式: ~x= - (x+1)operationNumber=~12  #~12=- (12+1) = -13print(operationNumber)        #输出结果:-13   #结题思路和按位与运算的一样,可以参考按位与运算#左移<<operationNumber=12<<1print(operationNumber)        #输出结果:24operationNumber=3<<3print(operationNumber)        #输出结果:24#右移>>operationNumber=12>>1print(operationNumber)        #输出结果:6operationNumber=12>>2print(operationNumber)        #输出结果:3
22321-13242463
成员运算符
#!/usr/bin/python# -*- coding: UTF-8 -*-a = 10b = 20list = [1, 2, 3, 4, 5 ];if ( a in list ):    print("1 - 变量 a 在给定的列表中 list 中")else:    print("1 - 变量 a 不在给定的列表中 list 中")if ( b not in list ):    print("2 - 变量 b 不在给定的列表中 list 中")else:    print("2 - 变量 b 在给定的列表中 list 中")# 修改变量 a 的值a = 2if ( a in list ):    print("3 - 变量 a 在给定的列表中 list 中")else:    print("3 - 变量 a 不在给定的列表中 list 中")
1 - 变量 a 不在给定的列表中 list 中2 - 变量 b 不在给定的列表中 list 中3 - 变量 a 在给定的列表中 list 中
身份运算符
#!/usr/bin/python# -*- coding: UTF-8 -*-a = 20b = 20if ( a is b ):    print("1 - a 和 b 有相同的标识")else:    print("1 - a 和 b 没有相同的标识")if( a is not b ):    print("2 - a 和 b 没有相同的标识")else:    print("2 - a 和 b 有相同的标识")# 修改变量 b 的值b = 30if ( a is b ):    print("3 - a 和 b 有相同的标识")else:    print("3 - a 和 b 没有相同的标识")if ( a is not b ):    print("4 - a 和 b 没有相同的标识")else:    print("4 - a 和 b 有相同的标识")
1 - a 和 b 有相同的标识2 - a 和 b 有相同的标识3 - a 和 b 没有相同的标识4 - a 和 b 没有相同的标识
运算符优先级

标签: #pythonlist相减