账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python 中的a is not None和 if a:区别
    83
    0
    res=requests.get('https://www.zhipin.com/gongsi/_zzz_c101010100_iy100014_t801_s301/',headers=headers)
    turn=etree.HTML(res.text).xpath('//div[@class="page"]/a[contains(@ka,"page-next")]/@href')
    turn
    []
    next_page is not None
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    NameError: name 'next_page' is not defined
    turn is not None
    True
    turn
    []
    if turn:
        print('sss')
        
    turn is not None
    True
    turn is  None
    False
    
    
    为啥这个是空 写is not None 是对的?这个is not None 什么时候用?
    
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 聊斋公 普通会员 1楼

      在Python中,a是一个变量,is not None是一个条件语句。这两个语句都可以用来检查变量是否为None,但是它们的用法和含义有所不同。

      a is not None: - 如果a为None,则返回False。 - 如果a不为None,则执行后续代码。

      例如: python a = None if a is not None: print("a is not None") else: print("a is None")

      if a:: - 如果a不为None,则执行if语句。 - 如果a为None,则执行else语句。

      例如: python a = None if a: print("a is not None") else: print("a is None")

      总的来说,is not None用于检查一个变量是否为None,而if a:用于检查一个条件是否为真。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部