Back

Activity 1.22: Checking Membership

divider

Activity 1.22

Checking Membership

Key Concepts

The in Operator

.index()

Is It in the List?

The in operator checks whether a value exists somewhere in a list, and hands back a boolean — no loop required.

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" in fruits)

Is It in the List?

Terminal window
True
False

Using in Inside an if

Since in produces True or False, it drops directly into a condition.

guess = input("Guess a fruit: ").lower()
if guess in fruits:
print("Yes, that's on the list!")
else:
print("Nope, try again.")

Finding the Position With .index()

in only tells you yes or no. When you also need where a value is, use .index().

fruits = ["apple", "banana", "cherry"]
print(fruits.index("banana"))

Finding the Position With .index()

Terminal window
1

.index() crashes if the value isn't in the list at all — that's why you check with in first.

Today's Objectives

  • Checking membership with the in operator
  • Finding a value's position with .index()
  • Guarding .index() with an in check first

Key Terms

in Operator
Checks whether a value exists in a list, returning True or False.
.index()
Returns the position of a value in a list — crashes if the value isn't found.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-22-checking-membership.


Task 1: Roster Check

  • Check whether a name the user enters is on the roster, using in.
Checking Membership
roster = ["avery", "blake", "carter", "drew", "elliot"]
name = input("Enter a name: ").lower()
if name in roster:
print(f"{name} is on the roster.")
else:
print(f"{name} is not on the roster.")

Task 2: Find the Position

  • If the name is on the roster, also report its position with .index().
Checking Membership
roster = ["avery", "blake", "carter", "drew", "elliot"]
name = input("Enter a name: ").lower()
if name in roster:
position = roster.index(name)
print(f"{name} is on the roster at position {position}.")
else:
print(f"{name} is not on the roster.")

Task 3: Check In Multiple Students

  • Loop until the user types "done", checking each name against the roster and counting how many were found.
Checking Membership
roster = ["avery", "blake", "carter", "drew", "elliot"]
name = ""
present_count = 0
while name != "done":
name = input("Enter a name (or 'done' to finish): ").lower()
if name == "done":
print(f"Checked in {present_count} students.")
elif name in roster:
position = roster.index(name)
print(f"{name} is on the roster at position {position}.")
present_count = present_count + 1
else:
print(f"{name} is not on the roster.")
divider

Checkpoint

Verify your program works correctly.

Example Output
Enter a name (or 'done' to finish): blake [Enter]
blake is on the roster at position 1.
Enter a name (or 'done' to finish): morgan [Enter]
morgan is not on the roster.
Enter a name (or 'done' to finish): elliot [Enter]
elliot is on the roster at position 4.
Enter a name (or 'done' to finish): done [Enter]
Checked in 2 students.
divider

Reflection

Answer the following questions before submitting your work.

  1. What does the in operator return, and what data type is that?
  2. Why did Task 2 need to check in before calling .index()?
  3. How did Task 3 combine the in operator with the accumulator pattern from Activity 1.21?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete