Skip to main content

Command Palette

Search for a command to run...

Day 14 :- Python Data Types and Data Structures for DevOps.

Updated
4 min read
Day 14 :-  Python Data Types and Data Structures 
                          for DevOps.

Tasks :-

  • What is Datatypes.

  • What is Data Structures.

  • Give the Difference between List, Tuple and set.

  • Create a dictionary and print it.

  • Create list and add a item into the list and sort the list in alphabetical order.

What is Data Structures :-

In Python, "Datatypes" refers to the classification or categorization of data items. Python is a dynamically typed language, which means that you don't need to explicitly declare the datatype of a variable. Instead, Python infers the datatype based on the value assigned to it.

Python provides several built-in datatypes, including:

  1. Numeric Types:

    • int: Integer numbers without decimal points.

    • float: Floating-point numbers with decimal points.

    • complex: Numbers with a real and imaginary part.

  2. Sequence Types:

    • list: Ordered collection of items, mutable (modifiable).

    • tuple: Ordered collection of items, immutable (unchangeable).

    • range: Represents a sequence of numbers.

    • str: String, an immutable sequence of characters.

  3. Mapping Types:

    • dict: Collection of key-value pairs, unordered.
  4. Set Types:

    • set: Unordered collection of unique items.

    • frozenset: Immutable set, similar to set but cannot be changed after creation.

  5. Boolean Type:

    • bool: Represents truth values, either True or False.
  6. None Type:

    • None: Represents the absence of a value or a null value.
  7. Binary Types:

    • bytes: Immutable sequence of bytes.

    • bytearray: Mutable sequence of bytes.

    • memoryview: Memory view object used to work with the memory of objects.

These datatypes provide the foundation for representing and manipulating data in Python programs. Additionally, Python allows defining custom datatypes using classes, enabling developers to create more complex data structures tailored to their specific needs.

What is Data Structures :-

In Python, "Data Structures" refer to the way data is organized, stored, and managed in memory. Data structures are fundamental for organizing and manipulating data efficiently. Python provides built-in data structures as well as the flexibility to create custom data structures using classes.

Here are some commonly used built-in data structures in Python:

  1. Lists: Lists are ordered collections of items. They are mutable, meaning you can change the elements after creating the list. Lists are created using square brackets [].

  2. Tuples: Tuples are ordered collections of items, similar to lists, but they are immutable, meaning you cannot modify the elements after creating the tuple. Tuples are created using parentheses ().

  3. Dictionaries: Dictionaries are unordered collections of key-value pairs. They provide a way to store and retrieve data based on keys rather than indices. Dictionaries are created using curly braces {}.

  4. Sets: Sets are unordered collections of unique elements. They are useful for tasks like removing duplicates from a sequence or testing set membership. Sets are created using curly braces {} or the set() function.

  5. Strings: Strings are immutable sequences of characters. They are used to represent textual data. Strings are created using single quotes ' ', double quotes " ", or triple quotes ''' ''' or """ """.

  6. Arrays: Arrays are a sequence of elements of the same type. Unlike lists, arrays can only store elements of the same data type. Arrays are provided by the array module in Python.

  7. Deque: Deque (double-ended queue) is a generalization of stacks and queues. It allows fast insertion and deletion at both ends. Deque is provided by the collections module in Python.

  8. Heapq: Heapq is a heap queue algorithm implemented in Python. It provides methods for creating and manipulating heap data structures.

These data structures serve various purposes and have different performance characteristics, making them suitable for different use cases. Choosing the right data structure is essential for writing efficient and maintainable code. Python's built-in data structures are optimized for ease of use and performance in most common scenarios. However, in specialized cases, custom data structures may be necessary to achieve optimal performance.

Give the Difference between List, Tuple and set :-

Certainly! Here's a programmatic comparison between lists, tuples, and sets in Python:

# Creating a list
my_list = [1, 2, 3, 4, 5]

# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)

# Creating a set
my_set = {1, 2, 3, 4, 5}

# Printing the data structures
print("List:", my_list)
print("Tuple:", my_tuple)
print("Set:", my_set)

Create a dictionary and print it :-

here's how you can achieve the result using the get() method without any additional logic:

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

# Print your favorite tool using its key
favorite_tool_key = 2  # Change this to the key corresponding to your favorite tool
favorite_tool = fav_tools.get(favorite_tool_key)
if favorite_tool:
    print("My favorite tool is:", favorite_tool)
else:
    print("Key not found. Please choose a valid key from the dictionary.")

Create list and add a item into the list and sort the list in alphabetal order :-

sort the list in alphabetical order:

# Initial list of cloud service providers
cloud_providers = ["AWS", "GCP", "Azure"]

# Add "Digital Ocean" to the list
cloud_providers.append("Digital Ocean")

# Sort the list in alphabetical order
cloud_providers.sort()

# Print the sorted list
print("List of cloud service providers after adding and sorting:")
for provider in cloud_providers:
    print(provider)

More from this blog

blog by yogesh ✍

23 posts