Python Practice › If-Else Programs › Program 10
Validate a user id in a list
Write a Python program to check whether a user id is present in a list of user ids.
Input
ids = [101, 102, 205, 309]; check 205 and 404
Output
205 is a valid user id 404 is not a valid user id
Approach
- Use the
inoperator to test membership.
Solution
user_ids = [101, 102, 205, 309] for uid in [205, 404]: if uid in user_ids: print(uid, "is a valid user id") else: print(uid, "is not a valid user id")Output
205 is a valid user id 404 is not a valid user id
Line by line
Line 4
in checks every item of the list and returns True or False.
Learn the concept
New to this topic? Read the lesson, then come back.