***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***
***Use tabs for Content***

POSTS

    Finding Items in Lists with the in Operator

    The in operator is used in Python to determine whether an item is contained in a list.

    Here is the general format of an expression written with the in operator to search for an item in a list:

     item in list

    In the general format, item is the item for which you are searching, and list is a list. The expression returns true if item is found in the list or false otherwise.

    # This program demonstrates the in operator used with a list.

    def main():

    # Create a list of product numbers.

    prod_nums = ['V400', 'F900', 'Q143', 'R600']

    # Get a product number to search for.

    search = input('Enter a product number: ')

    # Determine whether the product number is in the list.

    if search in prod_nums:

    print(search, 'was found in the list.')

    else:

    print(search, 'was not found in the list.')

    # Call the main function.

    main()