In this post, we are going to learn about Python in and not in operator in sequence list, dictionary, tuple, string, and dictionary.
1.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
2.Python not in operator
The not operator inverts the value of the boolean result. It rollovers true to false and is 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.
2.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.2 Python not in range
In this example, we are using if not in operator with range. The range is used to generate a range of sequence numbers. In this below example
How does it work
The in operator will return true because a value “4” exists in range. But not-operator inverse the result. So this expression ” if Val not in range(10) “returns false and else the statement will execute.
Python Code Example
val = 4
if val not in range(10):
print('value not exist:')
else:
print('value exist:')
Output
value exist: 4
2.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
2.4 Python not in with list comprehesion
In this example, we are using the not in operator with a list comprehension to find sublist elements that do 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]
2.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
2.6 Python not in Dictionary
In this example, we are using not in operator with a 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, and if the 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
2.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}
Summary
In this post, we have learned Python in and not in operator with examples.
- Python in operator
- Python not in operator