Is there a logical difference between 'not ==' and '!= (without is)

后端 未结 3 565
太阳男子
太阳男子 2020-12-20 23:49

Is there a substantial difference in Python 3.x between:

for each_line in data_file:
    if each_line.find(\":\") != -1:
        #placeholder for code
               


        
3条回答
  •  攒了一身酷
    2020-12-21 00:25

    Your first example is how you should be testing the result of find.

    Your second example is doing too much. It is additionally performing a boolean not on the result of the each_line.find(":") == -1 expression.

    In this context, where you want to use not is when you have something that you can test for truthiness or falsiness.
    For example, the empty string '' evaluates to False:

    s = ''
    if not s:
        print('s is the empty string')
    

    You seem to be conflating a little bit the identity-test expressions is and is not with the boolean not.

    An example of how you'd perform an identity test:

    result_of_comparison = each_line.find(":") == -1
    if result_of_comparison is False: # alternatively: is not True
        pass
    

提交回复
热议问题