Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Python Sets
Creating Sets
1. Using curly braces {}:
This is the most straightforward method for creating a set with specific elements you define. You simply enclose the elements separated by commas within curly braces. Here's an example:
fruits = {"apple", "banana", "orange", "banana"} # Note: duplicate "banana" will be ignored print(fruits) # Output: {'apple', 'orange', 'banana'} (order may vary)
2. Converting from other data types (lists, tuples):
You can create a set from existing data structures like lists or tuples. However, keep in mind that duplicates will be removed during the conversion because sets only allow unique elements. Here's how it works:
Create a set from the list:
my_list = [1, 2, 2, 3, 4] numbers_set = set(my_list) print(numbers_set) # Output: {1, 2, 3, 4}
Create a set from the tuple:
my_tuple = ("apple", "mango", "apple") unique_fruits = set(my_tuple) print(unique_fruits) # Output: {'apple', 'mango'}
3. The set() function:
The built-in set() function provides another way to create a set. You can pass an iterable object (like a list, tuple, or string) as an argument to the set() function. Similar to converting from other data types, duplicates will be eliminated during creation. Here's an example:
colors_string = "red,green,blue,red" color_set = set(colors_string.split(",")) # Split the string by comma and create a set print(color_set) # Output: {'red', 'green', 'blue'}
These three methods offer flexibility in creating sets based on your needs. Choose the approach that best suits the data you're working with!
It's time to take a quiz!
Test your knowledge and see what you've just learned.
How do you create a set using curly braces?
ABy using square brackets.
BBy using curly braces.
CBy using parentheses.
DBy using angle brackets.
Check Answer
What happens to duplicates when creating a set from a list?
CThey are counted as one.
DThey cause an error.
AThey are removed.
BThey are kept as is.
Check Answer
How can you create a set from a list in Python?
AUsing the list() function.
BUsing the tuple() function.
CUsing the set() function.
DUsing the dict() function.
Check Answer
Can you create a set from a tuple?
COnly if the tuple contains unique elements.
DNo, tuples cannot be converted.
AYes, it removes duplicates.
BNo, it does not support tuples.
Check Answer
What is the purpose of the set() function?
ATo create a list from an iterable.
BTo create a set from an iterable.
CTo convert a string to a list.
DTo split a string into characters.
Check Answer
How can you create a set from a string of colors?
CBy converting it to a list first.
DBy using the str() function.
ABy splitting the string and using set().
BBy directly using curly braces.
Check Answer