Wednesday, March 20, 2019

Shyama Sankar Vellore: Python Sets: Cheat Sheet

A cheat sheet for sets in Python. What is the 'set' datatype in Python? How is it used?

Jump to the cheat sheet

Key points

  • Unlike lists and tuples, a set is an unordered collection of unique elements.
  • A set can be created using the set() constructor. It takes an optional iterable as input.
  • Sets with some elements can also be initialized using the curly braces (e.g., {1, 2, 3}).
  • However, curly braces cannot be used to create an empty set. Using empty curly braces, i.e., {}, creates a dictionary datatype in Python.
  • 'in' operator can be used to check the presence of an element in a set.
  • Sets support the mathematical set operations like union, intersection, difference, symmetric difference, etc.
  • Sets are mutable, we can add or remove elements from them.

Cheat sheet

Useful resources and references



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...