Files
2025PY/day05/01-条件测试.py
2025-05-22 16:50:44 +08:00

60 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 一、条件测试
# 1、检查是否相等==
n1=10
n2=10
n3=20
# print(n1=n2) #报错 赋值关系
print(n1==n2) #True
print(n1==n3) #False
# 2、如何在检查是否相等的时候忽略大写
str1='hello'
str2='Hello'
print(str1==str2) #False
print(str1==str2.lower()) #True
# 3、检查是否不等!=
n1=10
n2=10
n3=20
print(n1!=n2) #False
print(n1!=n3) #True
# 4、使用and或者&)检查多个条件同时满足 否则输出False
n1=5
n2=10
n3=15
n4=5
print(n1==n2 and n3!=n4) #False
print(n1==n4 and n1!=n2) #True
# 5、使用or或者|)检查多个条件 有一个满足则输出True
n1=5
n2=10
n3=15
n4=5
print(n1!=n2 or n2==n3) #True
print(n1==n2 or n2==n3) #False
# 6、检查特定的值是否在列表中in
str='hello'
print('e' in str)
print('he' in str)
print('ho' in str) #False 连续的字符
# 7、检查特定的值是否不在列表中not in
str='hello'
print('hehe' not in str)
print('he' not in str)
# 8、布尔表达式 True / False
# 9、总结
"""
总结:
== 相当于数学中的等号, = 赋值符号 != 不等号
and(和) 多条件同时每个条件都要满足True,结果才满足True
or(或) 多条件有一个条件满足True,结果满足True
in(包含某个字符和子串)
not in(不包含某个字符和子串)
"""