集合
重複しない要素のコレクションについて学びます。
「集合」はCoddyKit上の無料Python For Kidsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPython For Kids学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Python For Kidsコースには全4レッスンが含まれています。
集合の概要
集合
データ構造のレッスンへようこそ!今日は、要素を重複なく集めて保存できるPythonの集合について学びます。

集合とは
集合は、重複しない要素の集まりです。リストとは異なり、集合では重複する要素を含めることができません。すべての要素を一意にしたい場合に便利です。
- 一意な要素:集合では重複が許可されません。
- 順序なし:集合では要素の順序は保持されません。
集合を作成する
集合は、波かっこ({})の中に要素をカンマ区切りで記述するか、set()関数を使って作成できます。
例:
# Creates a set of fruits using curly braces
fruits = {"apple", "banana", "cherry"}
# Creates a set of numbers using the set() function
numbers = set([1, 2, 3, 4, 5])集合の要素にアクセスする
集合には順序がないため、インデックスで要素にアクセスすることはできません。代わりに、ループを使って集合を反復処理できます。
例:
# Iterates through each fruit in the set and prints it
for fruit in fruits:
print(fruit)
集合に要素を追加する
add()メソッドを使うと、集合に要素を追加できます。
例:
# Adds a new fruit to the set
fruits.add("orange")
# Attempts to add a duplicate fruit (will have no effect)
fruits.add("apple")
# Prints the updated set
print(fruits) # Output might vary due to unordered nature
集合から要素を削除する
remove()メソッドまたはdiscard()メソッドを使うと、集合から要素を削除できます。
例:
# Removes 'banana' from the set
fruits.remove("banana")
# Removes 'grape' if it exists; does nothing otherwise
fruits.discard("grape")
# Prints the updated set
print(fruits) # Output might vary集合の演算
集合では、和集合、積集合、差集合などのさまざまな演算を行えます。これらを使って集合を結合したり比較したりできます。
- 和集合:両方の集合に含まれる一意な要素をすべてまとめます。
- 積集合:両方の集合に共通する要素を取り出します。
- 差集合:一方の集合には含まれるものの、もう一方には含まれない要素を取り出します。
例:
# Creates two sets of numbers
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
# Union of set_a and set_b
union_set = set_a.union(set_b)
print("Union:", union_set) # Output: {1, 2, 3, 4, 5, 6}
# Intersection of set_a and set_b
intersection_set = set_a.intersection(set_b)
print("Intersection:", intersection_set) # Output: {3, 4}
# Difference of set_a from set_b
difference_set = set_a.difference(set_b)
print("Difference:", difference_set) # Output: {1, 2}
サンプルプログラム:ユニーク訪問者
集合を使って、Webサイトへのユニーク訪問者を記録するプログラムを作成しましょう。
コード:
# Initializes an empty set to store unique visitor names
unique_visitors = set()
# List of visitors (including duplicates)
visitors = ["Alice", "Bob", "Charlie", "Alice", "Diana", "Bob"]
# Adds each visitor to the set
for visitor in visitors:
unique_visitors.add(visitor) # Adds the visitor name to the set
# Prints the total number of unique visitors
print("Total Unique Visitors:", len(unique_visitors)) # Output: 4
# Prints the names of unique visitors
print("Unique Visitors:", unique_visitors) # Output: {'Alice', 'Bob', 'Charlie', 'Diana'}
# Creates a set of numbers
numbers = {1, 2, 3, 4, 4, 5}
# Adds a new number to the set
numbers.add(6)
# Attempts to add a duplicate number
numbers.add(2)
# Prints the set
print(numbers)
よくできました!
Pythonで集合を使い、重複しない要素の集まりを保存する方法を学びました。集合は、一意性が重要なデータを管理するための強力なツールです。自分で集合を作成し、その演算を試しながら練習を続けてください。

よくある質問
「集合」レッスンは無料ですか?
はい。「集合」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Python For Kidsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Python For Kidsコースには全4レッスンが含まれています。
「集合」で何を学びますか?
重複しない要素のコレクションについて学びます。 ブラウザで直接実行するハンズオンコードでPython For Kidsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Python For Kidsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのPython For Kidsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「集合」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このPython For Kidsレッスンでコードを書いて実行できますか?
はい。すべてのPython For Kidsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。