fightstarr20
fightstarr20

Reputation: 12568

Python IF AND operators

I currently have two if statements that look for text in a string

if "element1" in htmlText:
    print("Element 1 Is Present")
    return

if "element2" in htmlText:
    print("Element 2 Is Present")
    return

These both work great, what I would now like to do is add an if statement that checks if element3 is present, but neither element1 or element2 are present

How do I chain these 3 checks together, is there an AND operator like in PHP?

Upvotes: 0

Views: 122

Answers (6)

dabhand
dabhand

Reputation: 517

I think this would be the most scalable solution:

elementsToCheck = ['element1','element2','element3']
for eIdx, eChk in enumerate(htmlText):
    if eChk in htmlText:
        print "Element {0} Is Present".format(eIdx)
        return

to answer the original question (though as has been pointed out before it is not needed to check against the other 2 elements):

if 'element3' in htmlText and not ('element1' in htmlText or 'element2' in htmlText):
   print "Element 3 Is Present"
   return

Upvotes: 0

dhke
dhke

Reputation: 15388

Early return (checking conditions in the right order, see given answers) is usually to be preferred performance wise.

If you cannot make use of early return, but instead need arbitrary conditions on the elements, remember that you have (list/dict) comprehensions.

For example

contains_matrix = [
   (element in htmlText)
   for element in ("element1", "element2", "element3")
]

will yield a list with True and False for each of the elements. The condition you mention in the question can then be formulated as

not contains_matrix[0] and not contains_matrix[1] and contains_matrix[2]

Let me repeat: the same result can be achieved by checking for "element3" last and returning early.

Dictionaries are even nicer (and more pythonic):

contains_dict = {
    element: (element in htmlText)
    for element in ("element1", "element2", "element3")
}

Evaluate with:

(
    not contains_dict['element1'] 
    and not contains_dict['element2'] 
    and contains_dict['element3']
)

or even

[element for element, contained in contains_dict.items() if contained]

which gives you all elements that are contained in the HTML.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191671

None of the other answers directly address this statement...

I would now like to do is add an if statement that checks if element3 is present, but neither element1 or element2 are present

Which can be written as

if "element3" in htmlText and not ("element2" in htmlText or "element1" in htmlText):

Upvotes: 0

pitaside
pitaside

Reputation: 678

Ofcourse in python there is and operator.

if "element1" in htmlText and "element2" in htmlText:
  do something 

OR you can still stick with your previous logic

if "element1" in htmlText :
    do...something
elif "element2" in htmlText :
    do something

elif "element3" in htmlText :
    do something 

else: 
   do other things

Upvotes: 1

jhoepken
jhoepken

Reputation: 1858

Try:

if "element1" in htmlText:
    print("Element 1 Is Present")
    return

elif "element2" in htmlText:
    print("Element 2 Is Present")
    return

elif "element3" in htmlText:
    print("Element 3 Is Present")
    return

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

Since return will return when a match was previously found, it's enough to append this code:

if "element3" in htmlText:
    print("Element 3 Is Present")
    return

Upvotes: 4

Related Questions