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

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...

Inbuilt Functions in Python पाइथन में अंतर्निहित फ़ंक्शन्स

Python is a powerful programming language that provides many inbuilt functions to make coding simple and efficient. These functions help programmers perform common tasks like finding length, converting characters, or checking memory addresses without writing extra code. पायथन एक शक्तिशाली प्रोग्रामिंग भाषा है जो कई अंतर्निहित (inbuilt) फ़ंक्शन्स प्रदान करती है, जिससे कोडिंग आसान और प्रभावी हो जाती है। ये फ़ंक्शन प्रोग्रामर को सामान्य कार्य करने में मदद करते हैं जैसे लंबाई निकालना, अक्षरों का रूपांतरण करना या मेमोरी एड्रेस जांचना, और इसके लिए अतिरिक्त कोड लिखने की आवश्यकता नहीं होती। (a)  id()  – Unique Identifier of an Object  किसी वस्तु का अद्वितीय पहचानकर्ता The  id()  function returns the unique memory address of an object. Every variable or object in Python is stored in memory, and  id()  helps us know where exactly it is stored. id()  फ़ंक्शन किसी ऑब्जेक्ट का अद्वितीय मेमोरी एड्रेस लौटाता है। पायथन में हर वेरिएबल या ऑब्जेक्ट मेमोरी में संग्रह...

Conditional statements in Python if, if-else, if-elif-else पायथन में सशर्त कथन

  In Python, conditional statements help you make decisions in the code. There are three main types of conditionals:- पायथन में, सशर्त कथन आपको कोड में निर्णय लेने में मदद करते हैं। सशर्त के तीन मुख्य प्रकार हैं:-  1.) Conditional `if` statement:- An `if` statement executes the code block only if the condition evaluates to `True`. 1.) सशर्त `if` कथन:- एक `if` स्टेटमेंट कोड ब्लॉक को केवल तभी निष्पादित करता है जब स्थिति सही पर मूल्यांकन करती है। Syntax:- if condition:    Statement(s) Example:- x = 10 if x > 5:     print("x is greater than 5") 2.) Alternative `if-else` statement:  An `if-else` statement checks a condition and executes a block of code('if' block), if the condition is true otherwise provides an alternative block of code ('else' block), if the condition is false. 2.) वैकल्पिक `if- else` कथन:- एक `if-else` स्टेटमेंट एक शर्त की जाँच करता है और कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है, यदि स्थिति सही है अन्यथा कोड का एक वैकल्पिक ब्ल...