Python in Keyword

In programming, a keyword is a “reserved word” by the language which conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Python language also reserves some of the keywords that convey special meaning. In Python, keywords are case sensitive.

“in” Keyword:

The in keyword has two purposes:

  • To check if a value is present in a list, tuple, range, string, etc.
  • To iterate through a sequence in a for loop.

Syntax:

# Using if statement
if ele in seq:
    statement(s)

# Using for statement
for ele in seq:
    statement(s)

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# in keyword
  
  
# Create a lits
animals = ["dog", "lion", "cat"]
  
# Check if lion in list or not
if "lion" in animals:
    print("Yes")

chevron_right


Output:

Yes

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# in keyword
  
  
# Create a string
s = "GeeksforGeeks"
  
# Iterating through the string
for i in s:
      
    # if f occurs in the string
    # break the loop
    if i == 'f':
        break
      
    print(i)

chevron_right


Output:

G
e
e
k
s
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.