Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Sets | Data Structures
Introduction to Python with AI

bookSets

A set is a collection of unique, unordered values. It doesn't allow duplicates and doesn't keep order.

Sets are useful for removing repeats, checking membership, or comparing groups with operations like union and intersection.

Note
Note
  • How do you define a set in Python? Give code example with curly braces and set function both.
  • What makes sets different from lists in Python? Give code example that shows these differences.
  • How do you add and remove items in a set? Give code example using add, remove and discard.
  • What are basic set operations in Python? Give code example for union, intersection, and difference.

Creating a Set

Create a set with curly braces, without key-value pairs: fruits = {'apple', 'banana', 'cherry'}

Or use set() to convert another collection, like a list, which also removes duplicates automatically.

Key Properties of Sets

  • Unordered: elements have no fixed order;
  • No duplicates: repeated items are ignored;
  • Mutable: you can add or remove items;
  • Immutable items only: allowed types include numbers, strings, tuples;
  • No indexing: elements can't be accessed by position.

Sets are optimized for fast membership tests with the in keyword.

Adding and Removing Items

  • .add(): to insert a new item into a set;
  • .remove(): to remove an item, it raises an error if the item doesn't exist;
  • .discard(): also removes item, but quietly skips if the item's not found.

Set Operations

Python sets support:

  • Union (| or .union()): combine elements from both sets;
  • Intersection (& or .intersection()): keep only common elements;
  • Difference (- or .difference()): elements in one set but not the other.

These operations are handy for comparing roles, flags, or datasets.

Summary

  • Sets are unordered collections of unique values;
  • They automatically remove duplicates;
  • You can add or remove items, but can't access by position;
  • Use sets for fast comparisons, membership checks, and when you don't care about order.

Try It Yourself

  1. Create two sets:
    • One with "apple", "banana", "cherry";
    • Another with "banana", "kiwi", "grape".
  2. Print their union, intersection, and difference;
  3. Then add "grape" to the first set, and discard "kiwi" from the second;
  4. Print their union, intersection, and difference again.
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about how sets handle duplicates?

What are some real-world examples where sets are useful?

How do I choose between using a set and a list?

Awesome!

Completion rate improved to 5

bookSets

Glissez pour afficher le menu

A set is a collection of unique, unordered values. It doesn't allow duplicates and doesn't keep order.

Sets are useful for removing repeats, checking membership, or comparing groups with operations like union and intersection.

Note
Note
  • How do you define a set in Python? Give code example with curly braces and set function both.
  • What makes sets different from lists in Python? Give code example that shows these differences.
  • How do you add and remove items in a set? Give code example using add, remove and discard.
  • What are basic set operations in Python? Give code example for union, intersection, and difference.

Creating a Set

Create a set with curly braces, without key-value pairs: fruits = {'apple', 'banana', 'cherry'}

Or use set() to convert another collection, like a list, which also removes duplicates automatically.

Key Properties of Sets

  • Unordered: elements have no fixed order;
  • No duplicates: repeated items are ignored;
  • Mutable: you can add or remove items;
  • Immutable items only: allowed types include numbers, strings, tuples;
  • No indexing: elements can't be accessed by position.

Sets are optimized for fast membership tests with the in keyword.

Adding and Removing Items

  • .add(): to insert a new item into a set;
  • .remove(): to remove an item, it raises an error if the item doesn't exist;
  • .discard(): also removes item, but quietly skips if the item's not found.

Set Operations

Python sets support:

  • Union (| or .union()): combine elements from both sets;
  • Intersection (& or .intersection()): keep only common elements;
  • Difference (- or .difference()): elements in one set but not the other.

These operations are handy for comparing roles, flags, or datasets.

Summary

  • Sets are unordered collections of unique values;
  • They automatically remove duplicates;
  • You can add or remove items, but can't access by position;
  • Use sets for fast comparisons, membership checks, and when you don't care about order.

Try It Yourself

  1. Create two sets:
    • One with "apple", "banana", "cherry";
    • Another with "banana", "kiwi", "grape".
  2. Print their union, intersection, and difference;
  3. Then add "grape" to the first set, and discard "kiwi" from the second;
  4. Print their union, intersection, and difference again.
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
some-alt