Python Data Structure

Python Data Structure

Day 14 of 90daysofdevops

Data Type

  • A data type is a characteristic that tells the compiler(or interpreter) how a programmer intends to use the data. There are two general categories of data types, differing in whether the data is changeable after definition.

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

  • The built-in data types are Numeric(Integer, Complex, Float),

    Sequential(String, Lists, Tuples), Boolean, Set, Dictionaries.

Data Structures

  • A data structure is a professional way of storing, organizing, processing, and retrieving data. Data structure organizes the information in a way that is easily understandable by both machines and humans. This makes it easy for users to have access to the data they need.

  • Python data structure is a systematized arrangement of data that enables us to store and access it efficiently and effectively. The goal of using data structures is to reduce the time and space required to execute operations on the data.

In Python, the data structure is divided into 2 types, Primitive and Non-Primitive.

Primitive DS vs Non-primitive DS

  • In Python, primitive data structures are basic data types that are built into the language itself. These include integers, floats, boolean, and characters. They are simple and immutable, meaning their values cannot be changed once assigned.

  • Non-primitive data structures, on the other hand, are more complex and can hold multiple values or objects. Examples of non-primitive data structures in Python include lists, tuples, dictionaries, and sets. Non-primitive data structures are mutable and offer more flexibility for storing and manipulating data.

Primitive DS


  1. Integer

    Integers represent numeric data – they represent whole numbers from negative infinity to infinity.

     integer_1 = 100
     integer_2 = 50
    
     # various operations on integers
     print (integer_1*integer_2)
     print (integer_1+integer_2)
     print (integer_1-integer_2)
     print (integer_1/integer_2)
    

  2. Float

    Floating points are used for rational numbers that usually end with a decimal figure.

     float_1 = 9.8
     float_2 = 3.14
    
     # various operations on floats
     print (float_1*float_2)
     print (float_1+float_2)
     print (float_1-float_2)
     print (float_1/float_2)
    

  3. String

    Strings are used to store data that involves characters (e.g., names or addresses). Strings can be created in Python by enclosing a sequence of characters within a pair of single or double quotes.

     print("Hello")
    
     # Multi line string
     a = """Lorem ipsum dolor sit amet,
     consectetur adipiscing elit,
     sed do eiusmod tempor incididunt
     ut labore et dolore magna aliqua."""
     print(a)
    
     a = "Hello, World!"
     print("2nd character:", a[1]) # Accesing a character using index value
    
     for x in "banana":
       print(x)
    
     txt = "The best things in life are free!"
     print("free" in txt)  # Checking the content is available in string or not
    

  4. Boolean

    Boolean type only has two types of return values: True and False. These return values are interchangeable with the integers 1 and 0.

     # has_passed is a boolean
     # false is assigned to has_passed (a boolean)
     has_passed = False
     marks = 90
     if (marks > 60):
         # true has been assigned to has_passed (a boolean)    
         has_passed = True
    
     print (has_passed)
    

Non-Primitive DS


Built-in DS vs User Defined DS

  • As the name suggests, these Data Structures are built-in with Python which makes programming easier and helps programmers use them to obtain solutions faster. Some examples of built-in data structures include Lists, Tuple, Dictionary, and Set.

  • User-defined data structures are created by the programmers based on their specific requirements. These can be implemented using built-in data structures or by creating custom classes. Some examples of user-defined data structures include Stack, Queue, LinkedList, and Tree.

Built-in DS

  1. List

    • Lists are used to store data of different data types sequentially. There are addresses assigned to every element of the list, which is called an Index. The index value starts from 0 and goes on until the last element called the positive index. There is also negative indexing which starts from -1 enabling you to access elements from the last to the first.

        my_list = [] #create empty list
        print("Empty list creation: ", my_list)
        my_list = [1, 2, 3, 'Abhi', 3.132] #creating list with data
        print("Creating list: ", my_list)
        my_list.append([555, 12]) #add as a single element
        print("Appended list: ", (my_list))
        my_list.extend([234, 'Ashu']) #add as different elements
        print("Extending the list: ", my_list)
        my_list.insert(1, 'Start') #add element i
        print("Inserting a element in a position: ", my_list)
        my_list.count(2)  #count the occurance in list
        print("count the occurance: ", my_list)
        del my_list[5] #delete element at index 5
        print("Deleteing a element at a index: ", my_list)
        my_list.remove('Ashu') #remove element with value
        print("Removing a element: ", my_list)
        a = my_list.pop(1) #pop element from list
        print("poping a element: ",my_list)
        my_list.clear() #empty the list
        print("Empty the list: ", my_list)
      

  2. Tuple

    • Tuples are the same as lists are with the exception that the data once entered into the tuple cannot be changed no matter what. The only exception is when the data inside the tuple is mutable, only then the tuple data can be changed.

        my_tuple = (1, 2, 3) #create tuple
      
        my_tuple2 = (1, 2, 3, 'ab') #access elements
        for x in my_tuple2:       # Printing all the elements
            print(x)
      
        my_tuple = my_tuple + (4, 5, 6) #add elements
      

  3. Dictionary

    • A dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. The dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key: value. Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable.

        x = {'Abhi': 7, 'Ashu': 10}
        x['Abhi'] = 3 #updating the value
        print(x) # All the key value in dictonary
        print(x['Abhi']) # get the value of a key
      
        del x['Ashu'] # delete a key
        print(x)
      
        x['Ajay'] = 9 # Adding a key value pair
        print(list(x)) # Listing all the key
        print(sorted(x)) # Sorting the key
        'Abhi' in x # Checking the key available in dict
        'jack' not in x # Checking the key is not there
      

  4. Set

    • Sets are a collection of unordered elements that are unique. Meaning that even if the data is repeated more than one time, it would be entered into the set only once. It resembles the sets that you have learned in arithmetic. The operations also are the same as are with the arithmetic sets.

        my_set = {1, 2, 3, 4, 5, 5, 5} #create set
        my_set.add(4) #add element to set
        5 in my_set #fast membership testing
        print(my_set) #duplicates have been removed
      
        a = set('abracadabra')
        b = set('alacazam')
      
        print(a)     # unique letters in a
        print(b)     # unique letters in b
        print(a - b) # letters in a but not in b
        print(a | b) # letters in a or b or both
        print(a & b) # letters in both a and b
        print(a ^ b) # letters in a or b but not both
      

User-Defined DS

Linear DS

  1. Array

    • Arrays in Python are Data Structures that can hold multiple values of the same type. Often, they are misinterpreted as lists or Numpy Arrays. Technically, Arrays in Python are distinct from both these.

        cars = ["Ford", "Volvo", "BMW", "Volvo"]
      
        print(cars[2])
        print(len(cars))
      
        for x in cars:
          print(x)
      
        cars.append("Honda") #Add one more element 
        print(cars)
        cars.count("Volvo") #Returns the number of elements 
        print(cars)
        cars.pop(1) #Delete the second element
        print(cars)
        cars.remove("BMW") #Delete the element that has a value
        print(cars) 
        cars.clear() #Removes all the elements
        print(cars)
      

  2. Stack

    • Stacks are linear Data Structures that are based on the principle of Last-In-First-Out (LIFO) where data that is entered last will be the first to get accessed.

    • It is built using the array structure and has operations namely, pushing (adding) elements, popping (deleting) elements and accessing elements only from one point in the stack called the TOP. This TOP is the pointer to the current position of the stack.

    • Stacks are prominently used in applications such as Recursive Programming, reversing words, undo mechanisms in word editors and so forth.

  3. Queue

    • A queue is also a linear data structure that is based on the principle of First-In-First-Out (FIFO) where the data entered first will be accessed first.

    • It is built using the array structure and has operations that can be performed from both ends of the Queue, that is, head-tail or front-back. Operations such as adding and deleting elements are called En-Queue and De-Queue and accessing the elements can be performed.

    • Queues are used as Network Buffers for traffic congestion management, used in Operating Systems for Job Scheduling and many more.

  4. LinkedList

    • Linked lists are linear Data Structures that are not stored consequently but are linked with each other using pointers. The node of a linked list is composed of data and a pointer called next. These structures are most widely used in image-viewing applications, music player applications and so forth.

Non-Linear DS

  1. Tree

    • Trees are non-linear Data Structures that have roots and nodes. The root is the node from where the data originates and the nodes are the other data points that are available to us. The node that precedes is the parent and the node after is called the child.

    • There are levels a tree has to show the depth of information. The last nodes are called the leaves. Trees create a hierarchy that can be used in a lot of real-world applications such as the HTML pages use trees to distinguish which tag comes under which block. It is also efficient for searching purposes and much more.

  2. Graph

    • Graphs are used to store data collection of points called vertices (nodes) and edges (edges).

    • Graphs can be called the most accurate representation of a real-world map. They are used to find the various cost-to-distance between the various data points called the nodes and hence find the least path.

    • Many applications such as Google Maps, Uber, and many more use Graphs to find the least distance and increase profits in the best ways.

Task

  1. Give the Difference between List, Tuple and Set. Do Handson and put screenshots as per your understanding.

    List: A list is Mutable, which is an ordered collection of items. And the items in the list can be replaced or changed.

    Set: A set is Mutable, which is an unordered collection of items. And the items can not be changed or replaced.

    Tuple: A Tuple is immutable, which is an ordered collection of items. And the items cannot be changed or replaced.

     l = ["Abhi", "Neha", 7, 211.3, 5 + 11j]
     print(l)
     print(type(l))
    
     s = {1, 2, 4, 2, 7, 5, 9, 7, 8, 9}
     print(s)
     print(type(s))
    
     t = (1, "Abhi", 24.3)
     print(t)
     print(type(t))
    

  2. Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.

     fav_tools = { 
       1:"Linux", 
       2:"Git", 
       3:"Docker", 
       4:"Kubernetes", 
       5:"Terraform", 
       6:"Ansible", 
       7:"Chef"
     }
    
     print(fav_tools)
     print(fav_tools[3])
    

  3. Create a List of cloud service providers eg.

     cloud_providers = ["AWS","GCP","Azure"]
     print(cloud_providers)
    

  4. Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

     cloud_providers.append("Digital Ocean")
     print(cloud_providers)
    
     print(sorted(cloud_providers))
    

Thank You,

Abhisek Moharana