Main Data Structures in Python

Hi friends! In this blog post, we will be discussing about the main data structures in Python and see some relevant examples.

 

What is a Data Structure?

A data structure, is a construct/container that is used for storing, managing and organizing data. Each programming language, offers its own set of constructs to be used as data structures.

It is with data structures that you work with any form of data in every programming language, therefore they are considered a significant part of every programming language.

 

Main Python Data Structures

Below, we will see some of the commonly used data structures in the Python programming language, that can be used for storing and processing collections of data, which are also known as non-primitive data structures.

The list of data structures that we will be examining below, is not the full list, since Python provides many other data structures as well.

 

List

The “List” data structure in Python, allows you to store multiple values in a single variable.

One of the main characteristics of Python Lists, is that they are mutable. That means that you can change the values of any element in the list at any moment.

Once defined, you can then access and modify any value, using the proper index.

Let’s see a simple example of working with the List data type in Python:

# List Example in Python
# Define List
vehiclesList = ["car", "motorbike", "train"]

# Print Full List
print (vehiclesList)

# Print a Specific Element from the List
print(vehiclesList[0])

 

Tuple

Like the “List” data structure, “Tuple” allows you to store multiple values in a single variable.

Its main difference from the “List” data structure, is that the “Tuple” data structure is immutable. That means that you cannot change the values of any element after defining the tuple.

Let’s see a simple example of working with the Tuple data type in Python:

# Tuple Example in Python
# Define Tuple
vehiclesTuple = ("car", "motorbike", "train")

# Print Full Tuple
print (vehiclesTuple)

# Print a Specific Element from the Tuple
print(vehiclesTuple[0])

 

Dictionaries

The “Dictionary” data structure in Python, is used for storing data in a key-value pair format.

When defining a Dictionary construct in Python, the key needs to be a single element, and the value can be any type such as: List, Tuple, float, int, etc.

Let’s see a simple example of working with the Dictionary data type in Python:

# Dictionary Example in Python
# Define Dictionary
vehiclesDictionary = {"Type": "Car", "Color" : "Red"}

# Print Dictionary
print (vehiclesDictionary)

 

Sets

The “Set” data structure in Python, is used for storing collections of unique, unorder and immutable elements.

To this end, one its main characteristics, is that you cannot have duplicate values and as of that, if you include a duplicate value in the set’s definition, then it will be automatically removed from the set during runtime.

Let’s see a relevant example of using the Set data structure, where I will be intentionally including a duplicate value, in order to see how the Set data structure will handle it:

# Set Example in Python
# Define Set
vehiclesSet = {"car", "motorbike", "train", "train"}

# Print Set
print (vehiclesSet)

If you run the above code in Python, you will see that it will only print the following: {'train', 'motorbike', 'car'}

The reason for that, is because the Set data structure will automatically track the duplicate value “train” and remove it.

 

Other Python Data Structures

There are many other data structures in Python, that can be used for storing single values, in contrast to the above four data structures which can be used for storing collections of data.

Examples of such data structures include: integers, floats, strings, Booleans and more.

The above data structures, are also known as primitive data structures.

 

 

Learn more about Programming – Enroll to our Course!

Enroll to our course on Udemy, titled  “Introduction to Computer Programming for Beginners” and get the help you need for getting started with C++, C, Python, SQL, Java, C# and the main phases of the Software Development Lifecycle.

By the time you complete this course:

  • You will know what is the required skill set in order to become a great Computer Programmer.
  • You will know more about the Programmer’s mindset.
  • You will know the main programming principles and fundamentals.
  • You will know the main phases of the Software Development Life Cycle (SDLC).
  • You will be able to start working with the following programming languages and write simple programs: C#, SQL, Java, C, C++ and Python.
Introduction to Computer Programming for Beginners - Enroll to the Course
(Lifetime access, downloadable resources, course completion certificate and more!)

Enroll to the Course

 

Learn how to work with Python and SQL Server – Enroll to our course!

Enroll to our online course “Working with Python on Windows and SQL Server Databases” with an exclusive discount, and get started with Python data access programming for SQL Server databases, fast and easy!

By the time you complete the course:

  • You will know how to install Python on Windows and set your development environment with Visual Studio Code and the proper extensions
  • You will know how to connect your Python programs to SQL Server instances and databases
  • You will know how to run different types of T-SQL queries against your SQL Server databases, from within your Python Programs
    • Execute and process SELECT, INSERT, UPDATE and DELETE T-SQL statements
    • Call Dynamic Management Views (DMVs)
    • Call aggregation functions
    • Call SQL Server global system variables
  • You will know how to execute SQL Server functions and stored procedures, from within your Python Programs
  • You will know how to use parameters and exception handling for all the above database operations within your Python code
Working with Python on Windows and SQL Server Databases - Online Course
(Lifetime access, downloadable resources, course completion certificate and more!)

Enroll to the Course

 

Recommended Online Courses:

 

Read Also:

 

Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)

© essentialDevTips.com

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)

Loading...