DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 47 of 77
←PreviousPrevNextNext→

Access Set Items

Cannot Access by Index

Sets are unordered collections, so their elements do not have a fixed position. This means you cannot use indexing or slicing like you would with lists or tuples.

To work with sets safely, you can check if an element exists using the 'in' keyword.

Example
# Checking if item exists
fruits = {"apple", "banana", "cherry"}

print("banana" in fruits)
print("orange" in fruits)
Output
True
False

Iterating Through Sets

Although sets do not support indexing, you can iterate through all elements using a for loop. The order of items when iterating is not guaranteed and may change.

Example
# Looping through a set
fruits = {"apple", "banana", "cherry"}

for fruit in fruits:
    print(fruit)
Output
apple
banana
cherry
Test your knowledge: Access Set Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 47 of 77
←PreviousPrevNextNext→