Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the add() Method: Adding Single Elements to a Set | Mastering Python Sets
Python Data Structures
course content

Kursinnhold

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the add() Method: Adding Single Elements to a Set

You can also add elements to a set. To do this, there's a specific add() method. It's important to note that with this method, you can add only one element at a time. So, if you want to add 4 elements, for example, you'll need to use this method 4 times. Let's check out an example.

1234567891011
# Creating an empty set favorite_movies = set() print("Initial set:", favorite_movies) # Output - Initial set: set() # Adding movies to the set favorite_movies.add("Inception") favorite_movies.add("Interstellar") favorite_movies.add("Tenet") favorite_movies.add("Dunkirk") print("Updated set:", favorite_movies) # Output - Updated set: {'Interstellar', 'Dunkirk', 'Inception', 'Tenet'}
copy

The add() method is best used for adding single elements.

Oppgave

Swipe to start coding

You are given the set tarantino_movies.

  • Add the movie "Reservoir Dogs" to this set.
  • Add the movie "Once Upon a Time in Hollywood" to this set.
  • Use the add() method to accomplish this.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
toggle bottom row

book
Using the add() Method: Adding Single Elements to a Set

You can also add elements to a set. To do this, there's a specific add() method. It's important to note that with this method, you can add only one element at a time. So, if you want to add 4 elements, for example, you'll need to use this method 4 times. Let's check out an example.

1234567891011
# Creating an empty set favorite_movies = set() print("Initial set:", favorite_movies) # Output - Initial set: set() # Adding movies to the set favorite_movies.add("Inception") favorite_movies.add("Interstellar") favorite_movies.add("Tenet") favorite_movies.add("Dunkirk") print("Updated set:", favorite_movies) # Output - Updated set: {'Interstellar', 'Dunkirk', 'Inception', 'Tenet'}
copy

The add() method is best used for adding single elements.

Oppgave

Swipe to start coding

You are given the set tarantino_movies.

  • Add the movie "Reservoir Dogs" to this set.
  • Add the movie "Once Upon a Time in Hollywood" to this set.
  • Use the add() method to accomplish this.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt