一,比较运算符是什么?
用于比较两个值的运算符。
作用: 对两个值进行比较,并返回一个布尔值(True或False)作为比较的结果
1,比较运算符有哪些
主要有六种比较运算符:
小于(<)
小于等于(<=)
大于(>)
大于等于(>=)
等于(==)
不等于(!=)
2,字符串的比较规则:
按字母的顺序比较:
顺序越靠前则值越小,顺序越靠后则值越大
大写字母的顺序比小写字母靠前
二,各个比较运算符的例子:
1,小于:
1 2 3 4 5 6 7 8 |
# 小于
print ( "5 < 7 值:" , 5 < 7 ) # 输出: True
print ( "7 < 5 值:" , 7 < 5 ) # 输出: False
# 小于 字符串比较
print ( "'apple' < 'orange' 值:" , 'apple' < 'orange' ) # 输出: True
print ( "'banana' < 'apple' 值:" , 'banana' < 'apple' ) # 输出: False
print ( "'Banana' < 'apple' 值:" , 'Banana' < 'apple' ) # 输出: True
|
运行结果:
5 < 7 值: True
7 < 5 值: False
'apple' < 'orange' 值: True
'banana' < 'apple' 值: False
'Banana' < 'apple' 值: True
2,大于
1 2 3 4 5 6 7 8 |
# 大于
print ( "5 > 7 值:" , 5 > 7 ) # 输出: False
print ( "7 > 5 值:" , 7 > 5 ) # 输出: True
print ( "5 > 5 值:" , 5 > 5 ) # 输出: False
# 大于 字符串比较
print ( "'apple' > 'orange' 值:" , 'apple' > 'orange' ) # 输出: False
print ( "'orange' > 'apple' 值:" , 'orange' > 'apple' ) # 输出: True
|
运行结果:
5 > 7 值: False
7 > 5 值: True
5 > 5 值: False
'apple' > 'orange' 值: False
'orange' > 'apple' 值: True
3,小于等于
1 2 3 4 5 6 7 8 9 |
# 小于等于
print ( "5 <= 7 值:" , 5 < = 7 ) # 输出: True
print ( "7 <= 5 值:" , 7 < = 5 ) # 输出: False
print ( "5 <= 5 值:" , 5 < = 5 ) # 输出: True
# 小于等于 字符串比较
print ( "'apple' <= 'orange' 值:" , 'apple' < = 'orange' ) # 输出: True
print ( "'banana' <= 'apple' 值:" , 'banana' < = 'apple' ) # 输出: False
print ( "'apple' <= 'apple' 值:" , 'apple' < = 'apple' ) # 输出: True
|
运行结果:
5 <= 7 值: True
7 <= 5 值: False
5 <= 5 值: True
'apple' <= 'orange' 值: True
'banana' <= 'apple' 值: False
'apple' <= 'apple' 值: True
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/14/python-di-shi-ba-zhang-bi-jiao-yun-suan-fu/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
4,大于等于:
1 2 3 4 5 6 7 8 9 |
# 大于等于
print ( "5 >= 7 值:" , 5 > = 7 ) # 输出: False
print ( "7 >= 5 值:" , 7 > = 5 ) # 输出: True
print ( "5 >= 5 值:" , 5 > = 5 ) # 输出: True
# 大于等于 字符串比较
print ( "'apple' >= 'apple' 值:" , 'apple' > = 'apple' ) # 输出: True
print ( "'apple' >= 'orange' 值:" , 'apple' > = 'orange' ) # 输出: False
print ( "'orange' >= 'apple' 值:" , 'orange' > = 'apple' ) # 输出: True
|
运行结果:
5 >= 7 值: False
7 >= 5 值: True
5 >= 5 值: True
'apple' >= 'apple' 值: True
'apple' >= 'orange' 值: False
'orange' >= 'apple' 值: True
5,等于:
1 2 3 4 5 6 7 |
# 等于
print ( "7 == 5 值:" , 7 = = 5 ) # 输出: False
print ( "7 == 7 值:" , 7 = = 7 ) # 输出: True
# 等于 字符串比较
print ( "'apple' == 'apple' 值:" , 'apple' = = 'apple' ) # 输出: True
print ( "'apple' == 'orange' 值:" , 'apple' = = 'orange' ) # 输出: False
|
运行结果:
7 == 5 值: False
7 == 7 值: True
'apple' == 'apple' 值: True
'apple' == 'orange' 值: False
6,不等于
1 2 3 4 5 6 7 |
# 不等于
print ( "7 != 5 值:" , 7 ! = 5 ) # 输出: True
print ( "7 != 7 值:" , 7 ! = 7 ) # 输出: False
# 不等于 字符串比较
print ( "'apple' != 'apple' 值:" , 'apple' ! = 'apple' ) # 输出: False
print ( "'apple' != 'orange' 值:" , 'apple' ! = 'orange' ) # 输出: True
|
运行结果:
7 != 5 值: True
7 != 7 值: False
'apple' != 'apple' 值: False
'apple' != 'orange' 值: True
标签:输出,False,apple,orange,python,运算符,第十八章,print,True
From: https://www.cnblogs.com/architectforest/p/17840044.html