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

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं

Python is a versatile and popular programming language known for its simplicity and readability. It has a wide range of features that make it suitable for various types of projects. Here are some key features of the Python programming language:- पायथन एक बहुमुखी और लोकप्रिय प्रोग्रामिंग भाषा है जो अपनी सरलता और पठनीयता के लिए जानी जाती है। इसमें कई प्रकार की विशेषताएं हैं जो इसे विभिन्न प्रकार की परियोजनाओं के लिए उपयुक्त बनाती हैं। पायथन प्रोग्रामिंग भाषा की कुछ प्रमुख विशेषताएं निम्न हैं:- 1. Open Source ओपन सोर्स:-   Python is an open-source language, which encourages collaboration and allows developers to contribute to its ongoing development. पायथन एक ओपन-सोर्स भाषा है, जो सहयोग को प्रोत्साहित करती है और डेवलपर्स को इसके चल रहे विकास में योगदान करने की अनुमति देती है। 2. Integrated Development & Learning Environments एकीकृत विकास एवं अध्ययन वातावरण  (IDLEs/IDEs):-  Python has several powerful IDLEs/IDEs, such as PyCharm, VSCode, and Jupyter, that provide too...