Top 3 Python Built-in Data Structures To Know About!
Updated: Sep 7, 2022
In the last post, we discussed how to install python on your machine and get started.
Here, We will discuss the top 3 built-in data structures python provide us to make our programming easy.
We will discuss:
List
Tuples
Dictionary
Data structures provide us with an organized way to store data. It is very important to think about how we will be going to store the data and variables that our program is using.
This helps us to access those data easily when we need them. Every program should be organized and follow a certain pattern or path to store its variables and data.
Let's understand this with an example, Suppose we create a program to store the latest movie titles.
Now, this statement 'to store the latest movie' tells us that anything which arrives new should be at the top of the list.
Now, what data structure should we use then? that will allow us to fetch say top 10 latest movie titles.
Stack is the answer! What's a stack? Do you see an old pile of books you have in your house, the Pile always has recently used books right? That's the concept of our stack!
List In Python
>>>l = []
>>> l = [1,2,3,4]
>>>l
>>> [1,2,3,4]
List is probably the most common data structure you will use in python, It is similar to arrays in other programming languages.
List uses the concept of indexing to access its elements:
>>>l[0]
>>>1
>>> l[-1]
>>>4
Unlike other programming language, python also supports negative indexing as you can see in above example.
Indexing in python starts with 0 in positive, and starts with -1 in negative indexing.
Tuples In Python
Tuples in Python are a special data structure. They are immutable after they are created. immutable means you can not change them, once you create them.
They are linear data structures like lists, but they are immutable.
>>> t = (1,2,3,4)
>>> t
(1,2,3,4)
Dictionary In Python
Dictionaries in python allow us to store variables in the form of key: value pairs. To access any variable within the data structure you need to pass its key.
>>> d = {"hey":"Hello", 1: 2}
>>> d["hey"]
'Hello'
>>>d[1]
2
These three are built-in data structures python provides us. With these built-in data structures, comes a handful of built-in python functions that will help us implement and play with these structures at ease.
We will dive deep into each of these data structures and their built-in functions with examples and theories. For now, let's wind up here. See you in the next post!
Recent Posts
See AllTechnology is enhancing at a very fast pace, everyday developers, futurists, and entrepreneurs are launching their new idea for a better...
Comments