Search code examples
pythonif-statementconditional-statementsoperators

How to use not equal to operator in if condition?


In other languages, we can use following code:

if(!False):
  print('not equal to operator worked.')

However, if I try to implement it in python, I am getting 'invalid syntax' error. Some people might say write True instead of False, but in my use case it's not possible. My use case is as following:

 def get_wrong_dtype_rows(col_name, data_type):
     arr = []
     for i,val in enumerate(df[col_name]):
        if !(type(val) is data_type):
            arr.append(i)
     return arr

I wanted to know, is their alternative way to solve this issue?


Solution

  • In Python,

    not is equal to !

    So, your code might be

     def get_wrong_dtype_rows(col_name, data_type):
         arr = []
         for i,val in enumerate(df[col_name]):
            if not type(val) is data_type:
                arr.append(i)
         return arr