codescracker
python

Python in Keyword



The in keyword in Python is used when we need either:

  1. to check whether a particular value is available in a sequence such as list, tuple etc. or not
  2. or to traverse a sequence in for loop

For example:

x = [12, 23, 65, 87]
if 23 in x:
    print("23 is available in the list 'x'")
else:
    print("23 is not available in the list 'x'")

print("\nAll elements of list 'x':")
for e in x:
    print(e)

The snapshot given below shows the sample output produced by above program, demonstrating the in keyword in Python:

python in keyword

Note - Python is a case-sensitive language, therefore keep both the letter of in in small letter.

Python in Keyword Example

Here is an example of in keyword in Python:

x = (134, 54, 65, 67, 0, 23)
print("Enter a Number: ", end="")
num = int(input())
if num in x:
    print("\nThe number is available")
else:
    print("\nThe number is not available")

print("\nEnter a String: ", end="")
str = input()
print("\nCharacters of given string are:")
for c in str:
    print(c)

Sample run with user input 66 as number, Python as string is shown in the snapshot given below:

python in keyword example

Python Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker