Python运算符
- 分类:
算数运算符、赋值运算符、比较运算符、逻辑运算符、位运算符。
除法运算符中/和//的区别:
/为浮点类型,//为整数类型
* 赋值运算符:
age += 1 》》 age + 1 = age
age = 18
age += 1
print(age)
age -= 1
print(age)
age *= 2
print(age)
age /= 3
print(age)
输出:19
18
36
12.0
- 比较运算符:
python = 95\ english = 92\ c = 89\ print("python=",python,"english=",english,"c=",c) print("python < english: ",python < english) print("python > english: ",python > english) print("python == english: ",python == english) print("python != english: ",python != english) print("python <= english: ",python <= english) print("english >= c",english >= c)
输出:
python= 95 english= 92 c= 89
python < english: False
python > english: True
python == english: False
python != english: True
python <= english: False
english >= c True
进程已结束,退出代码0
- 逻辑运算符
print("\n手机店打折活动进行中……")
strWeek = input("请输入中文星期(如星期一): ") #输入星期
intTime = int(input("请输入时间中的小时(范围:0~23): ")) #输入时间
if (strWeek == "星期二" and (intTime >= 10 and intTime <= 11)) or (strWeek == "星期五" and (intTime >= 10 and intTime <= 11)):
print("恭喜您!")
else:
print("对不起,您来晚一步!")
输出:
手机店打折活动进行中……
请输入中文星期(如星期一): 星期二
请输入时间中的小时(范围:0~23): 18
对不起,您来晚一步!
进程已结束,退出代码0
-
-
- TypeError: '>=' not supported between instances of 'str' and 'int' ***
整型与字符串类型不能一起计算,前边定义一下int
intTime = int(input("请输入时间中的小时(范围:0~23): ")) #输入时间
- TypeError: '>=' not supported between instances of 'str' and 'int' ***
-
-
位运算符
位异或运算
pwd = input("请输入密码: ")
print("原密码: ",pwd)
key = input("请输入秘钥: ")
password = int(pwd) ^ int(key)
print("加密后: ",password) #加密
print("解密后: ",password ^ int(key)) #解密
输出:
请输入密码: 123456
原密码: 123456
请输入秘钥: 44
加密后: 123500
解密后: 123456
进程已结束,退出代码0
- 位运算符
位运算符
number = 32
print("左移一位: ",number << 1)
print("右移一位: ",number >> 1)
输出:
左移一位: 64
右移一位: 16
进程已结束,退出代码0