Python_If语句

1. if语句
# _*_ coding:utf8 _*_

# if 语句
age = 19
if age >= 18:
    print("you are my choose")

# if-else语句
age = 17
if age >= 18:
    print("you are my choose")
    print(" have you already register?")
else:
    print("you are not my choose")
    print("sorry,")

# if-elif-else结构
age = 12
if age <= 4:
    print("free")
elif 18 >= age > 4:
    print("five")
else:
    print("ten")

# 使用多个 elif语句判断

age = 10

if age <= 4:
    print(" fighting,free")
elif 13 >= age > 4:
    print("two")
elif 18 >= age > 13:
    print("ten")
else:
    print("bad,bad")

# 省略else 语句 Python中 if-elif结构中并不要求必须写else语句,在有些情况下可以不用else

age = 29
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
elif age > 65:
    price = 5
print(" your price is " + str(price))

# 如果你想执行一个代码块,就是使用 if-elif-else 结构,如果你都想使用那么,你就使用多个if结构的

2. if操作列表

# _*_ coding:utf8 _*_
# if语句操作列表
# 操作特殊元素
request_toppings = ['you', 'he', 'her', 'she', 'my']

for request_topping in request_toppings:
    if request_topping == 'she':
        print('sorry ' + request_topping)
    else:
        print(request_topping)

# 确定列表不是为空的
# 当列表的长度大于1的时候 用if判断就会返回 True  为空就会返回False

request_toppings = []
if request_toppings:
    print(" sorry is null")
else:
    print(" is not null")

# 使用多个列表 循环嵌套 条件判断

one = ['one','two','three','four','five','six']
two = ['five','eight','ten','seven']

for value in one:
    if value in two:
        print(value)
    else:
        print('sorry')

猜你喜欢

转载自blog.csdn.net/dashingqi/article/details/80931116
今日推荐