So you’re interested in learning Python. One of the first things to learn about are the four built-in data structures. Check out the examples below for a quick overview!
- LISTS
Example: l = [1, 2, “a”]
Contents: ordered sequence of objects
Mutable/Immutable: mutable — can add or remove elements
Similar to: array
Built with: enclosed with brackets - TUPLES
Example: tup = (1, 2, “a”)
Contents: ordered sequence of objects
Mutable/Immutable: immutable — cannot add or remove elements
Similar to: array
Built with: enclosed with parentheses(optional)
Note: Faster and consumes less memory that lists - DICTIONARIES
Example: dict = {“a”:1, “b”:2}
Contents: unordered; have keys(unique) and values
Mutable/Immutable: immutable — cannot add or remove elements
Similar to: JS object or Ruby hash
Built with: curly brackets - SETS
Example: countries = set([‘brazil’, ‘germany’, ‘india’])
Contents: unordered, must be unique and immutable
Mutable/Immutable: mutable (frozensets are sets that are immutable)
Built with: set() built-in function
Note: think Venn Diagram/set theory