IF statement (checking for a string in a list) behave weirdly [duplicate]

2024/7/5 11:10:20

This will probably be a dumb question, but why does this piece of code behave like this?

>>> test = ['aaa','bbb','ccc']
>>> if 'ddd' or 'eee' in test:
...     print True
... 
True
>>> 

I was expecting nothing printed on the stdio, because none of the strings in the IF statement are in the list.

Am I missing something?

Answer

if 'ddd' or 'eee' in test

is evaluated as:

if ('ddd') or ('eee' in test):

as a non-empty string is always True, so or operations short-circuits and returns True.

>>> bool('ddd')
True

To solve this you can use either:

if 'ddd' in test or 'eee' in test:

or any:

if any(x in test for x in ('ddd', 'eee')):

https://en.xdnf.cn/q/120645.html

Related Q&A

filteration in txt file in python

I have too many lines like this:>ENSG00000100206|ENST00000216024|DMC1|2371|38568257;38570043|38568289;38570286 CTCAGACGTCGGGCCGACGCAAGGCCACGCGCGCGAACACACAGGTGCGGCCCCGGGCCA CACGCACACCGTACAC >ENSG0…

Count the number of times elements in a numpy array consecutively satisfy a condition

I have a numpy array as follows:import numpy as np a = np.array([1, 4, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8])and a constant number b=6I am searching for a number c which is defined by t…

Finding the index of the first repeating item in an array in python

I was solving an arrays problem from GFG, where we need to find the index of the first repeating element. The question is as follows: Given an array arr[] of size n, find the first repeating element. T…

how to check str int list and tuple? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to take out numbers and add them together in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Populate mysql table with random data in python

How can create a mysql table in python and then populate it with random data.I want around 10000 rows and integer values will do work.

I want to change a tuple into string without joining it in python. How could I do that? [duplicate]

This question already has answers here:Convert a list to a string without join(5 answers)Closed 3 years ago.For Example: I want to change t=(a, b, c) to s=a, b, c

Convert several YAML files to CSV

I am very new to Python and have several YAML files that I need to convert into csv. These are notes, comments and emails that came from our CRM (Highrise). I ONLY need the Notes and Comments, not the …

Python split unicode characters and words

Im running a data science project and i need your help.My string is:string = 🎁Testand I expect that output:s1 = 🎁s2 = Test

How to run two Flask discord.py bots in the same repl.it project?

I am using repl.it to run two bots in the same repl and Im using imports as a I saw in other stackoverflow questions. print(This will be the page where all bots will be made.) import os import bot1 imp…