优先级从高到低
print("2 ** 3 = %d" % 2 ** 3) 2 ** 3 = 8print("7 % 2 = {}".format(7 % 2)) 7 % 2 = 1print(‘7 %% 2 = %d‘ % (7 % 2)) 7 % 2 = 1print("7 // 2 = %d" % (7 // 2)) 7 // 2 = 3print("7 / 2 = %s" % (7 / 2)) 7 / 2 = 3.5print("2 * 3 = %d" % (2 * 3)) 2 * 3 = 6print("2 * 3 = %d" % 2 * 3) 2 * 3 = 22 * 3 = 22 * 3 = 2print("3 - 2 = %d" % (3 - 2)) 3 - 2 = 1print("3 + 2 = %d" % (3 + 2)) 3 + 2 = 5 优先级的重要性,操作符的执行顺序从左到右
没有加括号,执行结果是不一样的
print("2 * 3 = %d" % 2 * 3) 2 * 3 = 22 * 3 = 22 * 3 = 2
print("2 * 3 = %d" % (2 * 3)) 2 * 3 = 6 +、-、*、/、//、%和**是数学操作符,+和*是字符串操作符
原文地址:https://www.cnblogs.com/xinjing-jingxin/p/8605789.html
时间: 2024-10-09 20:52:27