PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

Python Sets

← All stacks

Theory

35/268

Python Sets

A set stores unique items with no guaranteed order. {1, 2, 2, 3} becomes {1, 2, 3}. Membership in is fast on average. Union |, intersection &, difference -. Empty set is set() — {} is an empty dict. That {} trap fails more interviews than the algebra.

frozenset is the immutable version. Use a set when duplicates must disappear: unique emails, unique roll numbers. Don’t use a set when order matters — print order can surprise you.

Python Sets — output — {1, 2, 3}, then union {1,2,3,4}, then intersection {3}.

Diagram
{1, 2, 2, 3}
         │ unique
         ▼
      {1, 2, 3}
Exam tip

{} is dict; set() is empty set.

Example

# Sets
a = {1, 2, 2, 3}
b = {3, 4}
print(a)
print(a | b)
print(a & b)

Python Sets — output: {1, 2, 3}, then union {1,2,3,4}, then intersection {3}.

Short notes

  • DefUnique, unordered.
  • RuleEmpty = set(), not {}.
  • Remember| & -.

Questions

1

Explain Sets as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with Sets?

Previous← List vs Tuple in PythonNextPython Set Methods →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.