Search code examples
pythonif-statementfor-in-loop

Python 'in' operator with 'for' loop and with 'if' statement


I am using python in operator with for loop and with if statement. My question is how is in implemented, that it behaves differently in this two cases: it iterates when using for loop and it checks if some element exists when using with if statement? Does this depend on implementation of for and if?

    for i in x:
        #iterates

    if i in x:
        #checks weather i in x or not 

Solution

  • The keyword "in" in python solves different purposes based on "for" and "if". please look at this related link in stack overflow for more clarity