if statement:
bool
Data types: true and false, only two values, that is,True
andFalse
.if
Statement uses the syntax:if False: print('success') else: print('fail')
else
: Statement:if False: print('success') else: print('fail')
-
Comparison operators:
a==b
: A and b are equal.a = 1 b = 2 if a == b: print('equal') else: print('not equal')
a!=b
: A and b are not equal.username = input(u'请输入用户名:') if username != 'zhiliao': print('您的用户是没有被加入到黑名单,可以正常使用') else: print('您的用户被加入到黑名单,不能正常使用')
a>b
: A is greater than b.age = 17 if age > 17: print('您可以进入到网吧了') else: print('您年龄未满18岁,不能进入网吧')
a<b
: A is less than b.age = 17 if age < 18: print('您的年龄未满18岁,不能进入网吧') else: print('您可以进入到网吧了')
a>=b
: A not less than a b.age = 18 if age >= 18: print('您可以进入到网吧了') else: print('您的年龄未满18岁,不能进入网吧')
a<=b
: A is less than equal to b.age = 18 if age <= 17: print('您的年龄未满18岁,不能进入网吧') else: print('您可以进入到网吧了')
条件a and 条件b
: Only a condition and conditions are met b was set up:age = input('请您输入年龄:') age = int(age) if age >= 15 and age <= 24: print('您是一个青年人') else: print('您不是一个青年人')
条件a or 条件b
: As long as satisfying a condition or a condition in b, on the establishment:if age < 15 or age > 24: print('您不是一个青年人') else: print('您是一个青年人')
-
not 条件a
: If a condition is True, it returns False. If the condition b is False, then return True:person1 = '中国人' person2 = '南非' if not person2 == '中国人': print('不可以上战舰') else: print('可以上战舰')
elif statement:
index = input('请输入星期数字:')
index = int(index)
if index == 0:
print('星期天')
elif index == 1:
print('星期一')
elif index == 2:
print('星期二')
elif index == 3:
print('星期三')
elif index == 4:
print('星期四')
elif index == 5:
print('星期五')
else:
print('星期六')