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.
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
Recommended Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Read Also:
- Understanding Variables in Python
- How to Read a Text File in Python – Live Demo
- IndexError: list index out of range – How to Resolve it in Python
- SyntaxError: invalid syntax when using IF in Python – How to Resolve it
- How to Write a “Hello World” App in Visual C++
- How to Write a “Hello World” App in C#
- How to Get Started with SQL Server – First Steps
- Benefits of Primary Keys in Database Tables
- How to Rebuild All Indexes Online for a SQL Server Database
- What’s the Best Allocation Unit Size when Formatting a USB Flash Drive?
- Using Dynamic Memory Allocation in Java
Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)
© essentialDevTips.com
Rate this article:
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and {essentialDevTips.com}. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.