Validate a user id in a list

EasyIf-Else Programs · 10 of 64

Write a Python program to check whether a user id is present in a list of user ids.

Advertisement
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

  1. Use the in operator to test membership.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs