In Python, the keywords `in` and `not in` are used to check membership in sequences or collections, such as lists, tuples, strings, sets, or dictionaries. These keywords allow you to determine whether a specific value exists within a collection or not.
पायथन में, कीवर्ड 'इन' और 'नॉट इन' का उपयोग अनुक्रमों या संग्रहों, जैसे सूचियों, टपल्स, स्ट्रिंग्स, सेट या शब्दकोशों में सदस्यता की जांच करने के लिए किया जाता है। ये कीवर्ड आपको यह निर्धारित करने की अनुमति देते हैं कि किसी संग्रह में कोई विशिष्ट मान उपस्थित है या नहीं।
1)`in` Keyword:-
The `in` keyword is used to check if a value exists within a sequence or collection.
`इन` कीवर्ड का उपयोग यह जांचने के लिए किया जाता है कि किसी अनुक्रम या संग्रह में कोई मान उपस्थित है या नहीं।
If the `value` exists in the given `collection`, this expression returns `True`; otherwise, it returns `False`.
यदि दिए गए `संग्रह` में `मान` उपस्थित है, तब यह अभिव्यक्ति `सही` लौटाती है; अन्यथा, यह 'गलत' लौटाता है।
Syntax:-
value in collection
Example:-
a) Checking for an item in a list:-
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Banana is in the list.")
Output:-
Banana is in the list.
b) Checking for a character in a string:-
text = "hello"
if "e" in text:
print("The letter 'e' is in the text.")
Output:-
The letter 'e' is in the text.
c) Using `in` with a tuple:-
numbers = (1, 2, 3, 4, 5)
if 3 in numbers:
print("3 is in the tuple.")
Output:-
3 is in the tuple.
d) Checking for a key in a dictionary:-
my_dict = {"name": "Alice", "age": 25}
if "name" in my_dict:
print("The key 'name' is in the dictionary.")
Output:-
The key 'name' is in the dictionary.
e) Using `in` in a Loop:-
list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 6, 8]
for num in list1:
if num in list2:
print(f"{num} is in both list1 and list2.")
Output:-
2 is in both list1 and list2.
4 is in both list1 and list2
f) Using `in` with a String in a Loop:-
word = "programming"
vowels = "aeiou"
for char in word:
if char in vowels:
print(f"{char} is a vowel.")
Output:-
o is a vowel.
a is a vowel.
i is a vowel.
2.) `not in`Keyword:-
The `not in` keyword is the opposite of `in`. It is used to check if a value does not exist within a sequence or collection.
`नॉट इन` कीवर्ड `इन` के विपरीत है। इसका उपयोग यह जांचने के लिए किया जाता है कि किसी अनुक्रम या संग्रह में कोई मान उपस्थित नहीं है।
If the `value` does **not** exist in the `collection`, this expression returns `True`; otherwise, it returns `False`.
यदि `मान` `संग्रह` में उपस्थित नहीं है, तब यह अभिव्यक्ति `सही` लौटाती है; अन्यथा 'गलत' लौटाती है।
Syntax:-
value not in collection
Example:-
a) Checking for an item not in a list:-
fruits = ["apple", "banana", "cherry"]
if "orange" not in fruits:
print("Orange is not in the list.")
Output:-
Orange is not in the list.
b) Checking for a character not in a string:-
text = "hello"
if "z" not in text:
print("The letter 'z' is not in the text.")
Output:-
The letter 'z' is not in the text.
c) Using `not in` with a tuple:-
numbers = (1, 2, 3, 4, 5)
if 7 not in numbers:
print("7 is not in the tuple.")
Output:-
7 is not in the tuple.
d) Checking for a key not in a dictionary:-
my_dict = {"name": "Alice", "age": 25}
if "address" not in my_dict:
print("The key 'address' is not in the dictionary.")
Output:-
The key 'address' is not in the dictionary.
e) Using `not in` in a `for` Loop:-
list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 6, 8]
for num in list1:
if num not in list2:
print(f"{num} is in list1 but not in list2.")
Output:-
1 is in list1 but not in list2.
3 is in list1 but not in list2.
5 is in list1 but not in list2.
f) Using `not in` with a String in a `for` Loop:-
word = "programming"
vowels = "aeiou"
for char in word:
if char not in vowels:
print(f"{char} is not a vowel.")
Output:-
p is not a vowel.
r is not a vowel.
g is not a vowel.
r is not a vowel.
m is not a vowel.
m is not a vowel.
n is not a vowel.
g is not a vowel.
Note:- Using `in` and `not in` with Sets:-
You can also use `in` and `not in` to check membership in sets.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
for num in set1:
if num in set2:
print(f"{num} is in both sets.")
if num not in set2:
print(f"{num} is only in set1.")
Output:-
1 is only in set1.
2 is only in set1.
3 is only in set1.
4 is in both sets.
5 is in both sets.
No comments:
Post a Comment