将列表所有元素打印出来

cars = ['toyota', 'honda', 'mazda', 'nissan', 'mitsubishi', 'subaru', 'suzuki', 'isuzu']
for car in cars:
    new_car = car.title()
    print("Japan automobile brand: %s" % new_car)
print("\nAbove is Japan automobile brand !")

输出

Japan automobile brand: Toyota
Japan automobile brand: Honda
Japan automobile brand: Mazda
Japan automobile brand: Nissan
Japan automobile brand: Mitsubishi
Japan automobile brand: Subaru
Japan automobile brand: Suzuki
Japan automobile brand: Isuzu

Above is Japan automobile brand !
View Code

对列表元素进行判断

japan_cars = ['toyota', 'honda', 'mazda', 'nissan', 'mitsubishi', 'subaru', 'suzuki', 'isuzu', 'bmc']
germany_cars = 'bmc'
for car in japan_cars:
    if car != germany_cars:
        print("Japan automobile brand: " + car.title())
    else:
        print("Germany automobile brand: " + germany_cars.title())

输出

Japan automobile brand: Toyota
Japan automobile brand: Honda
Japan automobile brand: Mazda
Japan automobile brand: Nissan
Japan automobile brand: Mitsubishi
Japan automobile brand: Subaru
Japan automobile brand: Suzuki
Japan automobile brand: Isuzu
Germany automobile brand: Bmc
View Code
12-19 13:32