Skip to main content

in and not in keywords of Python पाइथन में इन एवं नॉट इन कीवर्ड

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.

Comments

Popular posts from this blog

Python IDLE पायथन आईडीएलई (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट)

IDLE (Integrated Development and Learning Environment) is a graphical user interface (GUI) for the Python programming language. It includes the Python standard library and provides a convenient environment for writing, running, and debugging Python code. IDLE (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट) पायथन प्रोग्रामिंग भाषा के लिए एक ग्राफिकल यूजर इंटरफेस (GUI) है। इसमें पायथन मानक लाइब्रेरी शामिल है और पायथन कोड लिखने, चलाने और डीबग करने के लिए एक सुविधाजनक वातावरण प्रदान करता है।  Key features of IDLE are:- आईडीएलई की मुख्य विशेषताएं हैं:-  1. Interactive Shell:- IDLE provides an interactive Python shell, allowing you to execute Python commands and see the immediate results. 1. इंटरएक्टिव शेल:- आईडीएलई एक इंटरैक्टिव पायथन शेल प्रदान करता है, जो आपको पायथन कमांड निष्पादित करने और तत्काल परिणाम देखने की अनुमति देता है।  2. Script Editor:- It includes a text editor for writing Python scripts. You can create, edit, and save Python files with a ".py" extension. 2. स्क्...

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...