1. Python的if语句基本语法
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
print("Your admission cost is $" + str(price) + ".")
- 支持 5 < age <20
- 支持not in 和 !=判断
- and和or为快速and和快速or。假如or语句的第一个判断返回为True,则不执行第二个判断语句
if 5 < age <20:
pass
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car != 'audi'? I predict False.")
print(car != 'audi')
- if语句没有变量作用域,也就是if语句下的变量可以在外部直接使用
age = 3
if 5 < age < 18:
price = 3
else:
price = 8
print(f"Your admission cost is ${price}.")
网友评论