Python Tutorial Python Advanced Python References Python Libraries

Python in Keyword



The Python in keyword is a membership operator which validates membership of element in the sequence like list, tuple, set, dictionary etc. It returns true when the element is present in the sequence, otherwise it returns false.

Syntax

(element in sequence)          

Example:

In the example below, in keyword is used to check the membership of variable x in the given list called days.

days = ['MON','TUE','WED','THU','FRI']
x = 'SUN'
if x in days:
  print('yes,', x ,'is present in the list.')
else:
  print('No,', x ,'is not present in the list.')

The output of the above code will be:

No, SUN is not present in the list.

Example 2:

In the example below, in keyword is used to print all members of the given list called days.

days = ['MON','TUE','WED','THU','FRI']
for x in days:
  print(x)

The output of the above code will be:

MON
TUE
WED
THU
FRI

❮ Python Keywords