关于使用if语句处理列表

使用if语句检查特殊元素!
1.0 检查单个特殊元素:

  1. names = [‘yushihao’,‘admin’,‘wangshilong’,‘lishiyi’]
  2. for name in names:
  3. if name == 'admin':
    
  4.     print('Hello ' + name + ' ,' + 'would you like to see a status report ?')
    

Hello admin ,would you like to see a status report ?
说明:1.先列出一个列表
2.使用for循环对name进行循环
3.使用if语句对name(我想要的对象为admin)进行选择,若是admin在names中,则进行if语句的缩进代码print()
2.0 检查多个特殊元素:
1.0 names = [‘yushihao’,‘admin’,‘wangshilong’,‘lishiyi’,‘zhouchaohong’,‘xuekunming’,‘wanglifan’] 2.0 namess = [‘admin’,‘xueligao’,‘zhouchaohong’]
3.0 for name in namess:
4.0 if name in names:
5.0 print(‘Hello ’ + name + ’ ,’ + ‘would you like to see a status report ?’)
Hello admin ,would you like to see a status report ?
Hello zhouchaohong ,would you like to see a status report ?
说明:
1.0 为全部人
2.0 为我身边的人
3.0 对namess进行for的循环,让符合条件if的namess中的元素进入4.0的if条件进行判定
5.0 如果namess中的一个或几个元素判定成功,则进行5.0的print()的代码

3.0 使用if语句对列表的是否有无个数进行判断
1.0 names = []
2.0 if names:
3.0 for name in names:
4.0 print(‘Hello ’ + name + ’ ,’ + ‘would you like to see a status report ?’)
5.0 else:
6.0 print(‘no one want to see a status report !’)
>no one want to see a status report !
说明:
1. 用for循环对namess中的元素name进行判断,判断如果为true,则进行if的缩进的代码,就代表names中是有元素的
2. 如果判断结果为false,则进行else后的缩进的代码,则说明names中是无元素的

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值