Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Set | Set
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set
5. For deleting

bookCreating a Set

In Python, there are several ways to create sets.

  1. You can use the built-in set() function and pass the desired elements to it;
  2. You can place a sequence of elements inside curly braces and separate them with commas.

Here are some key points about sets:

  • They are mutable;
  • Sets only contain unique elements. If you add duplicate elements when creating a set, they'll be removed in the final set;
  • The order of elements in a set is not guaranteed;
  • Set elements can be of various data types.

Let's dive into an example. First, we'll create a set using the set() function.

123
# Creating a set which contains strings set_ = set("Python") print(set_)
copy

Next, we'll create sets using curly braces.

123456
# Creating a set which contains strings set_1 = {'hello', 'world'} print(set_1) # Creating a set which contains numbers set_2 = {10, 5, 6, 2, 8} print(set_2)
copy

Task

Follow these steps:

  1. Make a set with strings as follows:
    'pineapple', 'pear', 'cherry' ;
  2. Construct a set with a mix of data types like this:
    'pear', 45, None, 'car', 'Joe', 12, 5.

Note

Please write values in the exact order given in the task instructions.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
toggle bottom row

bookCreating a Set

In Python, there are several ways to create sets.

  1. You can use the built-in set() function and pass the desired elements to it;
  2. You can place a sequence of elements inside curly braces and separate them with commas.

Here are some key points about sets:

  • They are mutable;
  • Sets only contain unique elements. If you add duplicate elements when creating a set, they'll be removed in the final set;
  • The order of elements in a set is not guaranteed;
  • Set elements can be of various data types.

Let's dive into an example. First, we'll create a set using the set() function.

123
# Creating a set which contains strings set_ = set("Python") print(set_)
copy

Next, we'll create sets using curly braces.

123456
# Creating a set which contains strings set_1 = {'hello', 'world'} print(set_1) # Creating a set which contains numbers set_2 = {10, 5, 6, 2, 8} print(set_2)
copy

Task

Follow these steps:

  1. Make a set with strings as follows:
    'pineapple', 'pear', 'cherry' ;
  2. Construct a set with a mix of data types like this:
    'pear', 45, None, 'car', 'Joe', 12, 5.

Note

Please write values in the exact order given in the task instructions.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
toggle bottom row

bookCreating a Set

In Python, there are several ways to create sets.

  1. You can use the built-in set() function and pass the desired elements to it;
  2. You can place a sequence of elements inside curly braces and separate them with commas.

Here are some key points about sets:

  • They are mutable;
  • Sets only contain unique elements. If you add duplicate elements when creating a set, they'll be removed in the final set;
  • The order of elements in a set is not guaranteed;
  • Set elements can be of various data types.

Let's dive into an example. First, we'll create a set using the set() function.

123
# Creating a set which contains strings set_ = set("Python") print(set_)
copy

Next, we'll create sets using curly braces.

123456
# Creating a set which contains strings set_1 = {'hello', 'world'} print(set_1) # Creating a set which contains numbers set_2 = {10, 5, 6, 2, 8} print(set_2)
copy

Task

Follow these steps:

  1. Make a set with strings as follows:
    'pineapple', 'pear', 'cherry' ;
  2. Construct a set with a mix of data types like this:
    'pear', 45, None, 'car', 'Joe', 12, 5.

Note

Please write values in the exact order given in the task instructions.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In Python, there are several ways to create sets.

  1. You can use the built-in set() function and pass the desired elements to it;
  2. You can place a sequence of elements inside curly braces and separate them with commas.

Here are some key points about sets:

  • They are mutable;
  • Sets only contain unique elements. If you add duplicate elements when creating a set, they'll be removed in the final set;
  • The order of elements in a set is not guaranteed;
  • Set elements can be of various data types.

Let's dive into an example. First, we'll create a set using the set() function.

123
# Creating a set which contains strings set_ = set("Python") print(set_)
copy

Next, we'll create sets using curly braces.

123456
# Creating a set which contains strings set_1 = {'hello', 'world'} print(set_1) # Creating a set which contains numbers set_2 = {10, 5, 6, 2, 8} print(set_2)
copy

Task

Follow these steps:

  1. Make a set with strings as follows:
    'pineapple', 'pear', 'cherry' ;
  2. Construct a set with a mix of data types like this:
    'pear', 45, None, 'car', 'Joe', 12, 5.

Note

Please write values in the exact order given in the task instructions.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt