DATA STRUCTURES IN PYTHON
You can never be an efficient programmer without mastering data structures and algorithms. Problem solving in programming is impossible without the basics. Regardless of which aspect of python programming you want to focus on, you can’t ignore data, its organization and the algorithms you need to find solutions to their manipulation and data analysis.
Data structures are a way of organizing data in such a way that you can store collections of data, relate the, and perform operations on them. Data structures are derived from the term: data abstraction which is the use of data structures to solve problems by focusing on the main picture without getting lost in the details of meta-data and data access. They ease the process of problem-solving and ensure efficiency.
In other words, data structures are an actual implementation of the Abstract Data Types (ADT). This implementation requires you to have a physical view (how data collections are stored and organized) in a particular programming language’s constructs and basic data types.
There are two ways of grouping data structures in python:
(i) Built-in and User-Defined Data Structures
Primitive Data Structures
- Integers : Used to represent numeric data, more specifically whole numbers from negative infinity to infinity, like 2,3, -100000, 10000.
- Float : ‘Float’ stands for floating point number. It is used for rational numbers usually ending with decimal figure such as 1.1,2.3,9.3 etc.
String : Strings are a collection of alphabets, words or other characters. In Python, strings are created by enclosing a sequence of characters within a pair of double or single quotes. x= “Hello World”. Explore the different string operations here
Boolean : It is a built-in data type that can take the values TRUE or FALSE
Non-Primitive Data Structures
This section covers some of the primitive (and built-in) Python Data Structures.
Stores indexed elements that are changeable and can contain duplicate items.
Mutable, ordered series, traditionally of the same type of object.
Advantages: Mutable and ordered. Easy to understand. Relatively efficient memory usage.
Disadvantages: Searching is O(n).
To create a list, use square brackets:
mylist = [ ]
mylist = [1,2,3]
mylist = ['a', 'b', 'c', [1,2,3] ] # 4 elements
Stores indexed, unchangeable elements that can have duplicate copies.
Immutable, ordered series traditionally containing different objects
Advantages: Immutable and ordered. Relatively efficient memory usage (more than lists).
Disadvantages: Searching is O(n). Hard to understand for many Python newcomers.
Mutable, unordered pairs (keys and values) of objects. Keys must be hashable.
Advantages: O(1) searching for keys. Makes it easy to create trees and other hierarchical data structures. Can be used to create self-documenting code. Many problems can be described in terms of key-value pairs.
Disadvantages: Only lookup by key. Uses more memory than lists and tuples. Keys must be hashable.
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
Once a set is created, you cannot change its items, but you can add new items.
Advantages:Relatively efficient memory usage (more than lists).
Disadvantages: Sets are unordered, so you cannot be sure in which order the items will appear.Searching is O(n).
We will cover each of the data structures in the next tutorials. Thank you for reading!