用python比较大小

1.比较’ax’<'xa’的大小

代码为:

print('ax'<'xa')	#结果为True  
print(ord('a'))
print(ord('x'))    #字符串是通过ASCII表来进行顺次为比较大小

在这里插入图片描述

2.is与==的区别?

print(1 is True)   #为False
print(1==True)     #为True
print(id(1))
print(id(True))    # is 是通过比较id的编号来进行比较
                   #==  就是比较数值的大小

在这里插入图片描述

3.比较三个数的a,b,c的大小

代码一:

a,b,c=eval(input("输入a,b,c的值求最大值"))
print("max a=",a) if a>b and a >c  else print("不执行")
print("max b=",b) if b>a and b >c  else print("不执行")
print("max c=",c) if c>a and c >b  else print("不执行")

在这里插入图片描述

a,b,c=eval(input("输入a,b,c的值求最大值"))
print("max a=",a) if a>b and a >c  else   print("max b=",b) if b>a and b >c else print("max c=",c) if c>a and c >b else print("错误")

在这里插入图片描述

代码二:

a = int(input('请输入第一个数a:'))
b = int(input('请输入第二个数b:'))
c = int(input('请输入第三个数c:'))
if a>b and a>c:
    print("a的值最大,为",a)
    print('b=',b)
    print('c=',c)
else:
    if b>a and b>c:
        print("b的值最大,为",b)
        print('a=',a)
        print('c=',c)
    else:
        print("c的值最大,为",c)
        print('a=', a)
        print('b=', b)

运行为:
在这里插入图片描述
在这里插入图片描述
代码三:

a,b,c=eval(input('分别输入a,b,c:'))
if a>b:
    pass
    if a>c:
        print("max=",a)
    else:
        print("max=",c)
else:
    if b>c:
        print("max=",b)
    else:
        print("max=",c)

在这里插入图片描述
在这里插入图片描述
代码四:

a,b,c=eval(input('分别输入a,b,c:'))
print("max=",max(a,b,c))      #导入max函数求最大值

在这里插入图片描述

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值