五、if语句和关系运算
5.1 if 语句
在Python中,if语句的条件表达式的值是True或False(注意首字母大写),如下:
con = True
if con:
print("真")
5.2 复杂的if-elif-else语句
con = 'ZhangFei'
if con =='LiuBei':
print("大哥")
elif con =='GuanYu':
print('二哥')
else :
print('三弟')
输出:
C:\Users\Administrator>python E:\test.py
三弟
同大多数语言一样,Python中也不要求if-else结构中必须有else代码块。
5.3 逻辑运算符
逻辑运算符:与(and)、或(or)、非(not):
if True and True:
print('与真')
if True and False:
print('与假')
if True or False:
print('或真')
if not False:
print('not False')
if not True:
print('not True')
输出:
C:\Users\Administrator>python E:\test.py
与真
或真
not False