#算术运算:+、-、*、/、**(幂)、//(向下取整)、%(取余)
# a = int(input("请输入你购买产品的单价:"))
# b = int(input("请输入你购买商品的数量:"))
# print(a+b)
# print(a-b)
# print(a*b)
# print(a/b)
# print(a//b)
# print(a%b)
#赋值运算符:x+=1,x-=1
# c+=a
# print(c)
#比较运算符:x>y, x<y, x>=y, x<=y, x==y, x!=y => True/False
#逻辑运算符:x or y, x and y,not x
##对于包含and , not , or的表达式:
# (一)表达式从左至右进行运算,若or的左侧逻辑值为True,则短路or后所有的表达式(不管后面是and还是or)
# num = 3 or print("a") and not print("b") or print("c")
# print(num)
# (二)若and左侧的逻辑值为false,则短路后所有的and表达式,直到有or出现
# c = 0 or print("a") and not print("b") and not print("c") or print("d")
#成员关系运算:x in y, x not in y
#对象实例测试:x is y, x not is y
#位运算:x & y, x | y, x ^ y, x<<y、x>>y、~x(相当于:-x-1)#****二进制运算****
#一元运算:-x,+x,~x(按位取反)
# if (x>20 and x<30) or x<-100
# print(x)
##判断是否能构成三角形,计算三角形的面积
# import math
# a = int(input("请输入边长a:"))
# b = int(input("请输入边长b:"))
# c = int(input("请输入边长c:"))
# if a>0 and b>0 and c>0 and a+b>c and a-b<c:
# p = (a + b + c)/2
# s = math.sqrt(p*(p-a)*(p-b)*(p-c))
# print(f"面积:{s}")
# else:
# print("请重新输入边长!")
# if a > 0 and b > 0 and c > 0:
# if a+b>c and a-b<c:
# p = (a + b +c) / 2
# s = math.sqrt(p*(p-a)*(p-b)*(p-c))
# print(f"面积:{s}")
# else:
# print("两边之和大于第三边")
# else:
# print("输入不合法!")
# if a < 0 or b < 0 or c < 0:
# print("输入不合法!")
# elif a+b<=c or a-b>=c:
# print("两边之和不大于第三边!")
# else:
# p = (a + b + c) / 2
# s = math.sqrt(p*(p-a)*(p-b)*(p-c))
# print(f"面积:{s}")
##输出两个数之间较大的那个数
# a = 10
# b = 20
# #第一种方法
# c = a >= b and a or b
# print(c)
##第二种方法
# if a>=b:
# c = a
# else:
# c = b
#第三种方法: result = expressionl if condition else condition
# c = a if a >= b else b
# print(c)
# #如果字符串中存在”y",则输出yy;如果存在"x"则输出xx;否则输出zz
# a = "hello world zizi haha"
# str1 = "yy" if "y" in a else "xx" if "x" in a else "zz"
# print(str1)
#循环结构
#for 变量名 in 可迭代对象:
# for i in range(10):
# print(i)
# a = "abcEdfGH" #-->00010011 #如果为大写对应的位置就输出1 否则就输出0
# for i in a:
# print(i)
# ##如果字符串中的字母为大写对应的位置就输出1 否则就输出0
# #(一)
# str1 = input("请输入字符串:")
# for i in str1:
# print("1",end="") if i.isupper() else print("0", end="")
# #(二)
# result = ""
# for i in str1:
# result += "1" if i.isupper() else "0"
# print(result)
#设置一个有三次机会的登录系统,用户名错误直接结束,密码最多有三次尝试机会!
# username = input("please input your name:")
# if username == "root":
# for i in range(3):
# passwd = input("please input your password:")
# if passwd == "123456":
# print("登录成功!")
# break
# else:
# print(f"登录失败,还剩{3-i-1}次机会")
# else:
# print("请输入正确的用户名")
# for i in range(3):
# if i ==1:
# break
# print(i)
# else: #else for循环全部运行完 正常退出时 执行else里的内容
# print("this is else")
#pass:占位符,不做任何事情,仅仅只是保持语句结构的完成
# username = "root"
# if username == "root":
# pass
# else:
# print("error!")
#break: 终止当前循环
# while 1:
# s = input("enter word:")
# if s == "q":
# break
# for i in range(4):
# for j in range(3):
# print(i,j)
# if j == 1:
# break
#continue:结束本次循环,开启下一次循环
# for i in range(5):
# if i == 2:
# continue
# print(f"i is {i}")
#循环结构while!
#计算1-5和
# count = 0
# sum = 0
# while count<5:#当count>=5时,退出循环!
# count += 1
# sum += count
# print(sum)
#
#########登录 密码最多输入3次
# username = input("please input your name:")
# if username == "root":
# i = 0
# while i<3:
# i += 1
# passwd = input("please input your password:")
# if passwd == "123456":
# print("登录成功!")
# break
# else:
# print(f"登录失败,还剩{3-i}次机会")
# else:
# print("请输入正确的用户名")
python的运算符
最新推荐文章于 2023-03-12 15:51:17 发布