python3(五) if

age = 20
if age >= 18:
    print('your age is', age)
    print('adult')
# 如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做。
# else else:
age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')
    print('hello')
# if elif else
age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')
# 只要x是非零数值、非空字符串、非空list等,就判断为True,否则为False。
x = 5
if x:
    print('True')
# input 默认返回的是 str 不能和整数做比较
# birth = input('birth: ')
# if birth < 2000:
#     print('00前')
# else:
#     print('00后')
# 修改如下
s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')

 补充---

and 运算符:
- 如果两个操作数都为真,则条件为真。
- 如果一个操作数为假,条件为假。
例如:
python
age > 0 and age < 18      # 如果 age 大于 0 小于 18,条件为真
age > 0 and age > 65     # 如果 age 大于 0 但是不小于 65,条件为假  
or 运算符:
- 如果两个操作数中有一个为真,则条件为真。
- 如果两个操作数都为假,则条件为假。
例如:
python
age < 18 or age > 65     # 如果 age 小于 18 或大于 65,条件为真  
age < 0 or age > 100    # 如果 age 不在 0 到 100 之间,条件为假
not 运算符:
- 用于反转操作数的真值。
例如: 
python 
not age > 18            # 如果 age 不大于 18,条件为真 
not age < 0             # 如果 age 不小于 0,条件为真

 

posted @ 2019-09-18 16:44  ~清风煮酒~  阅读(375)  评论(0编辑  收藏  举报