#!/usr/bin/env python
#-- coding:utf-8 --
1.基本语法
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == "bmw": # 注意:if后面不要括号,别忘了冒号
print(car.upper())
else:
print(car.title())
2.and检查多个条件
a = 10
b = 12
if a > 2 and b > 20:
print("ture")
else:
print('fulse')
2.or检查多个条件
a = 10
b = 12
if a > 2 or b > 20:
print("ture")
else:
print('fulse')
3.not in 检查特定值是否不包含在列表中
users_black = ['a', 'b', 'c', 'd']
user = 'a'
if user not in users_black:
print(user + ' could talk!')
4.in 检查特定值是否包含在列表中
users_black = ['a', 'b', 'c', 'd']
user = 'a'
if user in users_black:
print("ture")
else:
print("F")
5.bool表达式
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
6.简单的if语句
age = 10
if age >= 18:
print('you own power of vote')
if age < 18:
print('you have no power of vote')
6.if-else 语句
age = 17
if age >= 18:
print('you own power of vote')
else:
print('no power')
7.if-elif-else
age = 17
if age < 4:
print('free')
elif age <= 17:
print('5$')
else:
print('10$')
8.多个if-elif-else,最后的else可以省略。
age = 27
if age < 4:
price = 0
elif age <= 17:
price = 3
elif age < 50:
price = 30
else:
price = 50
print('The price is', price)