Skip to main content

Command Palette

Search for a command to run...

Python Data Types and Data Structures for DevOps

Published
โ€ข3 min readโ€ขView as Markdown
Python Data Types and Data Structures for DevOps

Hey everyone! Today, we will dive into Python data types and data structures for DevOps. Let's get started with some basics!

Data Types

Data types are classifications or categories of data items. In Python, everything is an object, and data types are classes that define the kind of value and the operations that can be performed on it. Python comes with various built-in data types:

  1. Numeric Data Types:

    • Integer: Whole numbers without a fractional part (e.g., 5, -3, 0).

    • Float: Numbers with a fractional part (e.g., 3.14, -0.5, 2.0).

    • Complex: Numbers represented as real and imaginary parts (e.g., 2 + 3j).

  2. Sequential Data Types:

    • Strings: Ordered collections of characters enclosed in single or double quotes (e.g., "Hello", 'Python').

    • Lists: Ordered collections that can hold items of different types (e.g., [1, "hello", 3.14]).

    • Tuples: Similar to lists but immutable (cannot be changed) once created (e.g., (1, 2, 3)).

  3. Boolean Data Type:

    • Represents truth values (True or False) and is used in logical operations.
  4. Set Data Type:

    • An unordered collection of unique elements (e.g., {1, 2, 3}).
  5. Dictionary Data Type:

    • Represents a collection of key-value pairs (e.g., {'name': 'John', 'age': 30}).

To check the data type of a variable in Python, you can use the type() function.

your_variable = 100
print(type(your_variable))  # Output: <class 'int'>

Data Structures

Data structures are a way of organizing data to improve access efficiency. Python provides several built-in data structures:

  1. Lists:

    • Similar to arrays in other languages, ordered collections that can hold items of various types.
  2. Tuples:

    • Immutable ordered collections that can store elements of different data types.
  3. Dictionaries:

    • Unordered collections of key-value pairs for efficient data retrieval.

Now, let's dive into some hands-on tasks!

Task 1: Difference between List, Tuple, and Set

  • List:

    • Ordered and mutable.

    • Allows duplicate elements.

    • Created using square brackets [].

  • Tuple:

    • Ordered and immutable.

    • Allows duplicate elements.

    • Created using parentheses ().

  • Set:

    • Unordered and mutable.

    • Contains unique elements (removes duplicates).

    • Created using curly braces {}.

Task 2: Using Dictionary Methods

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

# Printing favorite tool using the key
tool_number = 3
print(f"My favorite tool is: {fav_tools.get(tool_number)}")  # Output: My favorite tool is: Docker

Task 3: Manipulating a List of Cloud Service Providers

cloud_providers = ["AWS", "GCP", "Azure"]
cloud_providers.append("Digital Ocean")
cloud_providers.sort()

print(f"Updated cloud providers list: {cloud_providers}")
# Output: Updated cloud providers list: ['AWS', 'Azure', 'Digital Ocean', 'GCP']

Feel free to explore and experiment more with Python data types and data structures. Happy coding! ๐Ÿ˜ƒ๐Ÿš€๐Ÿ