Course
Access Set Items
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Access Set Items
Since set is not a sequence data type, its items cannot be accessed individually as they do not have a positional index (as in list or tuple). Set items do not have a key either (as in dictionary) to access. You can only traverse the set items using a
for
loop.Example 1
langs = {"C", "C++", "Java", "Python"}for lang in langs: print (lang)
It will produce the following output
PythonCC++Java
Example 2
Python's membership operators let you check if a certain item is available in the set. Take a look at the following example
langs = {"C", "C++", "Java", "Python"}print ("PHP" in langs)print ("Java" in langs)
It will produce the following output
FalseTrue
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.