V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Yvette
V2EX  ›  Python

初学 Python,有个 if 语句一直不执行,后来发现问题出在这,越想越不对劲

  •  
  •   Yvette · 2015-02-09 06:19:09 +08:00 · 8437 次点击
    这是一个创建于 3371 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先想到是不是把and& 的用混了,可是查到一个解释说:
    and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.
    但是110<122110>97明明都是True啊
    而且为什么把第一个110换成100就是正常的。
    我觉得是不是我的Python有问题……

    image

    第 1 条附言  ·  2015-02-09 11:03:50 +08:00
    学习时间不长,第一次接触到按位与,去年学校学C的的时候&和&&的区别一直没弄明白,今天学习了,谢谢各位解答~
    14 条回复    2015-02-09 10:09:56 +08:00
    imlonghao
        1
    imlonghao  
       2015-02-09 06:24:55 +08:00 via Android   ❤️ 1
    运算的顺序可能并不是我们所认为的

    先比较两边,然后再比较中间

    >>> 110<122 & 110>97
    False
    >>> (110<122) & (110>97)
    True
    Valyrian
        2
    Valyrian  
       2015-02-09 06:26:44 +08:00   ❤️ 2
    & = bitwise and, like '&' in C
    and = logical and, like '&&' in C
    imlonghao
        3
    imlonghao  
       2015-02-09 06:36:12 +08:00 via Android   ❤️ 1
    and is a logical operator which is used to compare two values, IE:

    > 2 > 1 and 2 > 3
    True

    & is a bitwise operator that is used to perform a bitwise AND operation:

    > 255 & 1
    1

    http://stackoverflow.com/questions/16696065/is-there-a-difference-between-and-and-with-respect-to-python-sets/16696134#16696134
    liubiantao
        4
    liubiantao  
       2015-02-09 06:39:31 +08:00   ❤️ 2
    没学过 Python ,我刚才查了一下文档,又试验了一下,猜测如下。

    & 是按位与的意思。

    >>> 122 & 110
    106

    所以 100<122 & 110>97 相当于 100<106>97,显然是 True
    而 110<122 & 110>97 相当于  110<106>97,显然是 False

    >>> (110<122) & (110>97)
    True

    加上括号为什么对呢,引用你查到的解释。
    and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.
    注意while & (when used with True/False values),也就是说只有当两边都是布尔值的时候,& 才起到 and 的作用,否则 & 优先是按位与的意思。

    不准确之处欢迎之处。
    yyfearth
        5
    yyfearth  
       2015-02-09 06:50:24 +08:00   ❤️ 1
    一般来说都是位运算的优先级比较高
    laoyuan
        6
    laoyuan  
       2015-02-09 07:36:22 +08:00   ❤️ 1
    用 && 就没这事了,&& == and
    Tink
        7
    Tink  
       2015-02-09 07:38:04 +08:00 via iPhone   ❤️ 1
    带括号,一般为了防止运算顺序错误都会习惯性带括号
    cassyfar
        8
    cassyfar  
       2015-02-09 08:00:48 +08:00   ❤️ 1
    一个是logic operation 一个是bit operation
    Delbert
        9
    Delbert  
       2015-02-09 08:12:41 +08:00   ❤️ 1
    @liubiantao “所以 100<122 & 110>97 相当于 100<106>97,显然是 True
    而 110<122 & 110>97 相当于 110<106>97,显然是 False”……
    写错了吧?
    lebowskis
        10
    lebowskis  
       2015-02-09 09:21:48 +08:00   ❤️ 1
    楼主犯了两个错误,一个是对符号理解的错误,&不等同于and,&是按位与的意思,其次,&的优先级比<>高,所以你用&之后的表达式是:110 < (122 & 110) > 97
    110 = 0x6e = 0b01101110
    122 = 0x70 = 0b01111010
    97 = 0x61 = 0b01100001
    (122 & 110) = 0b01101010 = 0x6a = 106

    即是你的表达式等价于 110<106>97,明显为False
    而100<122 & 110>97等价于 100 <106 >97,明显为True
    你的python没任何问题。
    Osake
        11
    Osake  
       2015-02-09 09:30:09 +08:00   ❤️ 1
    乡亲们回答的好 赞一个
    linuxzpf
        12
    linuxzpf  
       2015-02-09 09:35:59 +08:00   ❤️ 1
    LZ要区分一下 “按位与”和“逻辑与”是不一样的哦。
    heaton_nobu
        13
    heaton_nobu  
       2015-02-09 09:45:51 +08:00   ❤️ 1
    &&
    hfli
        14
    hfli  
       2015-02-09 10:09:56 +08:00   ❤️ 1
    楼主其实不是把and和&搞混了,而是把"&"和"&&"搞混了 :-)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   872 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.