Python Data Structures | List, Set, Tuple, Dictionary, Array, Linked Lists
Data Structures in Python
What is Data Structure (DS) ?
A data structure is a way of organizing and storing data in
a computer so that it can be accessed and used efficiently. In other words, a
data structure provides a systematic way of managing and manipulating data.
Data structures can be classified into two categories:
primitive data structures and non-primitive data structures.
Primitive data structures are the basic building blocks of
programming languages, and include data types such as integers, floats,
characters, and Boolean values. These data types are used to store simple
values, and are not typically used to organize larger sets of data.
Non-primitive data structures, on the other hand, are used
to organize and manage complex data sets. Examples of non-primitive data
structures include arrays, lists, stacks, queues, trees, graphs, and hash tables.
These data structures are designed to store and manipulate large sets of data
efficiently, and can be used to implement algorithms for solving complex
problems.
Here are some of the commonly used data structures in Python
1. Lists
Example:
2. Tuples:
A tuple is similar to a list, but once created, it cannot be modified. Tuples are usually used to store related pieces of data.
Example:
3. Dictionaries:
4. Sets:
A set is an unordered collection of unique items. Sets
are useful when you need to keep track of a collection of items, but you don't
care about their order.
Example:
my_set = set("FreeITCamp")
print(my_set) # Output: {'C', 'T', 'r', 'F', 'm', 'p', 'a', 'e', 'I'}
5. Arrays:
An array is a collection of items of the same data type. Arrays are used when you need to perform numerical operations on large amounts of data.
In Python, you need to define an array by importing the built-in array module and calling the array function with the following arguments:
Type code: A string that specifies the data type of the
array elements. This can be one of the following values:
'b' (signed char)
'B' (unsigned char)
'u' (Py_UNICODE)
'h' (signed short)
'H' (unsigned short)
'i' (signed int)
'I' (unsigned int)
'l' (signed long)
'L' (unsigned long)
'f' (float)
'd' (double)
Initial values: A sequence of values that will be used to
initialize the array.
Example:
6. Linked Lists:
A linked list is a data structure that
consists of a sequence of nodes, where each node contains a value and a
reference to the next node in the list.
Comments
Post a Comment