0

Data Types and Structures in Python

Introduction to Data Types


The popularity of Python, a general-purpose programming language, is growing in the field of data science. We must first comprehend Python's data types in order to learn it.


What is a Data Type?


Python Data Types are used to specify the types of variables. It describes the type of data that will be stored in a variable. Data of several types can be stored in memory.

Data types in Python.

Numbers (Integer)

String

List

Tuple

Dictionary


Number (Integer)


Numeric values are stored as a number. When a variable is given a variety, Python produces Number of objects.

For Ex:

A = 3

B = 5. Here A and B are number objects in which we pass 3 and 5.

Python supports 4 types of numeric data


Int


(Signed integers like 13, 2, 29, etc.)


Long


(Long integers used for a higher range of values like 90809080L, -0x192929L, etc.)


Float


(Float is used for floating-point numbers like 1.9, 9.902, 15.2, etc.)


complex


(Complex numbers like 2.14j, 2.0 + 2.3j, etc.)


String


The string is the collection of characters that is encapsulated within quotation marks. In Python, we will utilise single, double, or triple quotations to define a string. ("")


List

There could be a variety of data kinds on the list. The items in the list are enclosed in square brackets and are seperated by commas (,). We can use the slice [:] operators to get the data from the list. Similar to how they did on strings, the repetition operator (*) and concatenation operator (+) both work on lists.


Tuple

In many ways, a list and a tuple are similar. Like lists, tuples contain a collection of elements of various data types. Commas (,) and parentheses are used to separate the tuple's components ().

The dimensions and values of the components won't be changed, hence a tuple can be a read-only arrangement.


Dictionary

An ordered collection of key-value pairs is referred to as a dictionary. Similar to a hash table or associative array, each key stores a specific value. The key can be any primitive data type, and any Python object can be the value.

The entries in the dictionary are separated by commas, and they are enclosed in curly braces.

Data Structures in Python


What is Data Structure?


Data structure is the term used to describe the computational storage of data for useful usage. It saves the data in a format that makes it easy to retrieve and change. It refers to the values of the data, their connections, and the possible operations on the data as a whole. The usage of data structures in the development of computer programmes makes them important. Because computer programmes heavily rely on data, it is essential that it be organised appropriately for easy access. For each programme or piece of software, this is essential.

The four main functions of a data structure are:


To input information

To process the information

To maintain the information

To retrieve the information

Types of Data structures in Python


A variety of data structures are supported by Python for simple data access and storage. Python data structures and types can be divided into primitive and non-primitive. Examples of the first data type include Booleans, Strings, Floats, Integers, and Floats. Arrays, lists, tuples, dictionaries, sets, and files are included in the second group. Because of this, Python has both built-in and user-defined data structures. The built-in data structure is also known as the non-primitive data structure.

Built-in Data structures


In Python, there are several data structures that act as places to store various kinds of data. Examples of Python data structures include lists, dictionaries, triples, and sets.

User-defined data structures


It is possible to programme Python's built-in data structures to carry out the same task as these data structures. User-defined data structures include hash maps, linked lists, stacks, queues, trees, graphs, and stacked arrays.

List of in-build Data structures and explanation


List


A list has items that are sequentially organized and contain various types of data. Each item of information is assigned an address, known as the index. The index value, which began with a 0, terminates in the last element. A positive index is what it is called. There is also a negative index that can be used to retrieve the items in reverse. Negative indexing is the term for it.

Dictionary


Instead of storing single elements, a type of data structure known as a dictionary holds key-value pairs. The idea can be demonstrated using a phone directory, which includes both a person's phone number and all of their other numbers. Name and phone number so act as the "key" constant values, and each person's name and phone number serve as the values for that key. By analysing the key, one can access all the values stored there. In Python, a defined key-value structure is known as a dictionary.

 Tuple


The only difference between tuples and other lists of data storage is that a tuple's data cannot be modified. Then and only then is it possible to change the data that makes up a tuple.Tuples can be created through the tuple () function.

Set


The set data structure and the arithmetic sets are similar. It consists primarily of a collection of distinctive qualities. If the data is repeatedly repeated, sets might consider only including that part once. Just sending values to it within floral braces will generate a set.

List of user-defined data structures and explanation


 Stacks


First in Last Out (FIFO) or Last in First Out (LIFO) are two different types of linear structures that can be used in stacks (LIFO). Push and Pop are the stack's two primary operations. While Push signifies the addition of an element to the top of the list, Pop signifies the removal of an element from the bottom of the stack.

Queue

 

Similar to stacks, a queue is a linear structure that allows for both the addition and removal of pieces at either end. The two operations are referred to as "enqueue" and "dequeue," respectively. The most recent addition is removed first, just like stacks. One of the queue's main goals is the quick processing of things as they arrive.

Tree


Trees are hierarchical, non-linear data structures composed of nodes connected by edges. The Python tree data structure is made up of the nodes root, parent, and child. A data structure's topmost element is called the root. In a binary tree, an element can only have two child nodes.

                                   Graph

Another non-linear data structure in the Python programming language is the graph, which has nodes and edges. On a graphic level, it depicts a group of items, some of which are connected. The vertices are connected objects, whereas the links are referred to as edges. With the key representing the vertices and the values representing the edges, a graph can be represented using the Python dictionary data structure.

 Hash map


Hash maps, a type of Python data structure, are useful for storing key-value pairs. To access the data stored in hash maps, keys that are generated using a hash function are used. These kinds of data structures are useful for keeping track of client and student information, among other things. A good example of a hash map is a dictionary in Python.

Linked list


There are linear data structures of this kind. It is essentially a set of Python connections connecting various data objects. The pointers in a linked list join the components together. The header and tail nodes of this data structure are its beginning and last nodes, respectively. As a result, a linked list is composed of nodes that are connected to one another by pointers and values.




Comments

Leave a comment