美文网首页
第五章 if语句

第五章 if语句

作者: 仙境源地 | 来源:发表于2019-11-29 11:19 被阅读0次

5.1

关键字:True,False(和java不一样,首字母大写了)
每个if语句的值为True或False的表达式
==,!=,>=,<=,>,<,not

5.2.6检查特定值是否包含在列表中

可以使用关键字in,not in

5.4.2确定列表不是空的

如果列表是空的,则 if cars 返回False,否则返回True

# encoding = utf-8
cars = ['car', 'bmw', 'subaru', 'toyoa']
my_car = 'toyoa'
for car in cars:
    if car == my_car:
        print(car.upper())
    else:
        print(car)
    if car != 'bmw':
        print('welcome ' + car)
print(cars)

age = 18
if age >= 18:
    print("now , you can fly ," + str(age))
is_male = True
if age > 18 and is_male:
    print("come male")
age = 4
is_male = False
if age > 18 and not is_male:
    print("come female")
if age < 18 or age > 60:
    print("you need to be protected")

if age < 5:
    print('your age is too little,' + str(age))
elif age > 60:
    print('your age is too old,' + str(age))
else:
    print('your age is suitable,' + str(age))

age = 17
if age < 5:
    print('your age is too little,' + str(age))
elif age < 18:
    print('sorry,your age have not enough,' + str(age))
elif age > 60:
    print('your age is too old,' + str(age))
else:
    print('your age is suitable,' + str(age))

if my_car in cars:
    print('my car have exist' + my_car)
if my_car not in cars:
    print('my car not exist ' + my_car)

empty_cars = cars[:]
empty_cars.clear()
# 确定列表是不是空的
if empty_cars:
    print('not empty:' + str(empty_cars))
if not empty_cars:
    print('empty:' + str(empty_cars))
for empty_car in empty_cars:
    print(empty_car)
car
welcome car
bmw
subaru
welcome subaru
TOYOA
welcome toyoa
['car', 'bmw', 'subaru', 'toyoa']
now , you can fly ,18
you need to be protected
your age is too little,4
sorry,your age have not enough,17
my car have existtoyoa
empty:[]

相关文章

网友评论

      本文标题:第五章 if语句

      本文链接:https://www.haomeiwen.com/subject/tvmwwctx.html