Day 14 Python Data Types and Data Structures for DevOps

Day 14 Python Data Types and Data Structures for DevOps

Data Types :-

  • Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

  • Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string,lists, tuples), Boolean, Set, Dictionaries, etc

Python Data Structures:-

Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

  • Lists Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type

  • Tuple Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.

  • Dictionary Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized

  1. Lists: Lists are ordered collections of items, which can be of any data type. They are mutable, meaning their elements can be changed after the list is created. Lists are defined using square brackets [].
my_list = [1, 2, 3, 4, 5]
  1. Tuples: Tuples are similar to lists, but they are immutable, meaning their elements cannot be changed after creation. Tuples are defined using parentheses ().
my_tuple = (1, 2, 3, 4, 5)
  1. Sets: Sets are unordered collections of unique items. They are mutable, but their elements must be immutable (e.g., integers, strings, tuples). Sets are defined using curly braces {} or the set() constructor.
my_set = {1, 2, 3, 4, 5}
  1. Dictionaries: Dictionaries are unordered collections of key-value pairs. Each key must be unique, and it is used to access its corresponding value. Dictionaries are defined using curly braces {} and colons : to separate keys and values.
my_dict = {'a': 1, 'b': 2, 'c': 3}
  1. Arrays: Arrays are homogeneous collections of items, typically used for numerical computations. They can be created using the array module in Python.
import array
my_array = array.array('i', [1, 2, 3, 4, 5])  # 'i' denotes integer type
  1. Collections: The collections module provides specialized data structures such as deque, Counter, OrderedDict, defaultdict, etc., which offer additional functionality over the built-in data structures.
from collections import deque
my_deque = deque([1, 2, 3, 4, 5])

These are some of the fundamental data structures in Python. Depending on your specific use case, you may choose one over the other for better performance or functionality.