Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
2. The "in" operator
Sorting – sort()
Sorting – sorted()
Sorting – reverse() and reversed()
Summary

Instruction

Let's start with search algorithms. The search problem consists of finding out whether a given element is present in a data structure, such as a list or tuple. Python provides the very convenient in operator for such cases. You're already familiar with it:

names = ['Paul', 'John', 'Mark', 'Mike']
if 'John' in names:
  ...

The in operator simply checks if a given element is contained in a data structure. This is the search problem we mentioned.

Exercise

You are given a list of all prime numbers up to 100. Ask the user to enter a number.

Enter an integer from 1 to 100: {x}

Then, check if the user's number is in the list and print:

{x} is prime.

Or:

{x} is not prime.

Stuck? Here's a hint!

Use the input() function to get a number from the user. Remember that the number you'll get will be a string, so use int() to convert it to an integer.