'in' keyword in python

in_keyword_python

Skill - ‘in’ keyword in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision

in keyword in python can be used to

  • check if an item is present in a collection like list, tuple, range, string
  • iterate over a sequence like list, tuple, range etc using a for loop

‘in’ with if condition

names = ['john', 'smith', 'tom', 'Harry']

if 'smith' in names:
	print('smith is in the list!')

isTomPresent = 'tom' in names
print(isTomPresent) # True

isHPresent = 'H' in 'Harry'
print(isTomPresent) # True

‘in’ with for loop

names = ['john', 'smith', 'tom', 'Harry']
for k in names:
	print('The name is {0}'.format(k))

for n in range(5):
	print(n)

Video

The video for this post can be seen here


Online Interpreter

You can run these codes online at https://www.programiz.com/python-programming/online-compiler/

References


Table of Contents

Comments