In this post, we are going to learn Python, not in operator with examples. python not in with a list, dictionary, tuples, string.,
Python in operator
The in operator checks whether value or element exists in any sequence string, tuple, list, array, in case of dictionary key is checked. It returns a boolean value(true,false). It returns true when a value exists in sequence false when not existing.
Program code Example
mylist = ['C#','lang','Go','Data']
mytuple = (12,13,14,15,16)
mydict = {'C#':1,'lang':2,'Go':3,'Data':4}
Greeting = "Bonjour"
val = 'C#'
print(val in mylist)
print(val in mytuple)
print(val in mydict)
print('Bon'in mydict)
Output
True
False
True
False
Python not in operator
The not operator inverts the value of the boolean result. It is rollover true to false and mostly used to check value that does not exist in a given sequence. So the not operator reverses the value returns by in operator.
The not in the operator returns a boolean value(true, false). The not in operator works opposite of in operator while finding value in the sequence. It returns false when a value exists in sequence true when does not exist.
1. Python while not in operator
In this example, we are using a not in operator with a while loop. We want to run the loop till the condition is true. But not-operator returning False because a value exists in the list. So nothing is appended in the new list.
Python code Example
mylist = ['C#','lang','Go','Data']
val = 'C#'
res_list = []
while val not in mylist:
res_list.append(val)
print(res_list)
//Output :[]
2. Python not in range
In this example, we are using not in operator with range. The range is used to generate a range of sequence numbers.
How does it work
The in operator will return true because a value “4” exists in range. But not-operator invert the result mean changing true to false. So this expression ” if Val not in range(10) “returns false and else the statement will execute.
We are using if statement with not in operator. The value 4 exist in a range not in the operator returning false and else the statement is executing.
Python Code Example
val = 4
if val not in range(10):
print('value not exist:')
else:
print('value exist:')
Output
value exist: 4
3.Python not in List
In this example, we are using not in operator with the list. The if statement using wit not in operator to check value does not exist in the list. The value ‘C#’ exists in the list, not in the operator returning false, so else the statement is executing.
How does it work
- The in operator will return true because the value ‘C#’ exists in the list. But not-operator invert the result means it change true to false.
- So this expression “if Val not in mylist:” will return false and else the statement will execute.
Python code example
mylist = ['C#','lang','Go','Data']
val = 'C#'
if val not in mylist:
print('value exist:',val not in mylist)
else:
print('value exist:',val not in mylist)
Output
value exist: False
4. Python not in with list comprehesion
In this example, we are using the not in operator with list comprehension to find sublist element that not exist in mylist.
Python code Example
mylist = [1,2,3,5,6,7]
sublist = [1, 3, 5]
result = [item for item in mylist if item not in sublist]
print(result)
Output
[2, 6, 7]
5. Python not in string
In this example, we are using not in operator with string. The if statement with not in operator to execute when the result is true otherwise else statement will execute.
How does it work
- The in operator will return true because the The string ‘Bon’ exists in the original string. But not-operator invert the result means it change true to false.
- This expression “if Val not in mylist:” will return false and else the statement will execute.
Python code example
Greeting = "Bonjour"
val = 'Bon'
if val not in Greeting:
print('value exist:',val not in Greeting)
else:
print('value exist:',val not in Greeting)
Output
value exist: False
6. Python not in Dictionary
In this example we are using not in operator with dictionary.The if statement using with not in operator to check string not exist in string. The key ‘Net does’ not exist in the dictionary, so not in the operator returning True, So if statement is executing.
Python code Example
mydict = {'C#':1,'lang':2,'Go':3,'Data':4}
val = 'Net'
if val not in mydict:
print('value exist:',val not in mydict)
else:
print('value exist:',val not in mydict)
Output
value exist: True
7. Python not in with Dictionary compreshesion
In this example, we are finding the key which is not n%2 ==0 and multiplying the key to itself.
Python Code Example
dict = {1:1,3:2,6:3,8:5}
dict_result = {item:item*item for item in dict if not item%2 == 0}
print(dict_result)
Output
{1: 1, 3: 9}
8. Python not in Set
In this example we are using not in operator with Set .The if statement using with not in operator to check string not exist in string. The value ‘Net’ not exists in Set, so not in the operator returning True,So if statement is executing.
Python code Example
first_set = {'evening','Hi',5,6,7,'Good'}
val = 'Net'
if val not in first_set:
print('value exist:',val not in first_set)
else:
print('value exist:',val not in first_set)
Output
value exist: True
Summary
In this post, we have learned Python not in operator with examples.