Sunday 27 December 2020

Python: not in: Check for non-existence of element in the list

‘not in’ keyword is return True, if given element is not in the list, else False.

 

Syntax

element not in list

 

Example

10 not in first_5_primes

 

Above statement return True, if 10 is not in the list first_5_primes, else False.

 

Example

10 not in first_5_primes

>>> first_5_primes = [2, 3, 5, 7, 11]
>>> 
>>> 10 not in first_5_primes
True
>>> 
>>> 11 not in first_5_primes
False
>>>

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment