Python Crash Course-note4


if语句
示例程序

cars=['absore',bmw','core']
for car in cars:
    if car=='bmw':
        print(car.upper())
    else
        print(car.titile())
//输出
Absore
BMW
Core

循环检查当前car是否为bmw,是则全大写打印,否则首字母大写打印。

条件测试
python检查是否相等时区分大小写。
如果大小写无关紧要,而只想检查变量的值,可将变量的值转换为小写,在进行比较。

car='Audi'
if car.lower()=='audi':
    print("true")
//输出true

检查不相等
要检查不相等,使用不等运算符!=即可,用法与其他语言无异。

检查多个条件
:and—等价于C++的&&运算符
:or—等价于C++的||

age=19
if age>18 and age>17:
    print("true")
if age>18 or age>19:
    print("true")
 //output
 true
 true

检查特定值是否在列表中
关键字in:判断在列表中
关键中not in:判断不在列表中

al=['a','b','c','d']
A = 'a'
E= 'e'
if A in al:
    print("true")
if E not in al:
    print("true")
//output
true 
true

if-elif-else结构

age=19
if age<18:
    print("young")
elif age<28:
    print("28")
else:
    print("old")
//output    
28

列表判空

age=[]
if age:
    print("not empty")
else:
    print("empty")
//output
empty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值