セクション 1. 章 22
single
Adding Single Elements to a Set in Python
メニューを表示するにはスワイプしてください
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'}
The add() method is best used for adding single elements.
タスク
スワイプしてコーディングを開始
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.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 22
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください