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

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

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...