Skip to main content

Access Items

A set has no indexes or keys, so you cannot ask for an item by position or name.

What you can do is walk through the items with a for loop, or use the in keyword to learn whether a value is there.

Example

Print every value in the set with a loop:

Prompt
Create a set with "apple", "banana", and "cherry". Loop through the set using a for loop and print each value.
Output: complete Python program

Example

Is "banana" in the set?

Prompt
Create a set with "apple", "banana", and "cherry". Check if "banana" is present in the set using the in keyword and print the result.
Output: complete Python program

Example

Test whether "banana" is absent from the set:

Prompt
Create a set with "apple", "banana", and "cherry". Check if "banana" is NOT present in the set using not in and print the result.
Output: complete Python program

Change Items

The items of an existing set cannot be edited, though new ones can still be added.