Python if语法——5

#!/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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wolves_liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值