Python Data Types and Data Structures for DevOps

Python Data Types and Data Structures for DevOps

ยท

3 min read

Data Types:

Python has several built-in data types, including numeric types (int, float, complex), string (str), boolean (bool), and collection types (list, tuple, dict, set). Each data type has its own set of properties, methods, and behaviors that allow programmers to manipulate and process data effectively in their programs.

  • 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.

To check what is the data type of the variable used, we can simply write: your_variable=100 type(your_variable)

Python Structures for DevOps:

Numeric (Integers, complex numbers, and floating-point numbers) ๐Ÿ”ข

Integers are numbers without decimal points. Floats are numbers with decimal points. Complex numbers have real parts and imaginary parts. Complex numbers store the real and imaginary parts as a float by default.

Sequential (Strings, lists, and tuples) ๐Ÿงต๐Ÿ“œ

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

Boolean ๐Ÿ…ฟ๏ธ

Dictionaries function as hash tables, with a time complexity of O(1) for most operations. They are an unordered collection of key-value pairs, making them highly optimized for storage and retrieval of data. ๐Ÿ“š

Set๐Ÿงฎ

Unordered collection, mutable, contains only unique elements. ๐Ÿงฎ

Dictionaries, and more. ๐Ÿ“š

Dictionary is a built-in Python Data Structure that is mutable. It is similar in spirit to List, Set, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. On an abstract level, it consists of a key with an associated value. In Python, the Dictionary represents the implementation of a hash-table.

Python Data Types and Data Structures for DevOps

Tasks ๐Ÿ“

1. Difference between List, Tuple, and Set ๐Ÿ”„๐Ÿ“‹๐Ÿงฎ

  • List: Ordered collection, mutable, can contain elements of different types. ๐Ÿ“‹

  • Tuple: Ordered collection, immutable, can contain elements of different types. ๐Ÿ”„

  • Set: Unordered collection, mutable, contains only unique elements. ๐Ÿงฎ

2. Hands-On with Dictionary Methods ๐Ÿ› ๏ธ

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

favorite_tool_key = 2
favorite_tool = fav_tools.get(favorite_tool_key)
print(f"My favorite tool is {favorite_tool}")

3. Creating a List of Cloud Service Providers โ˜๏ธ

cloud_providers = ["AWS", "GCP", "Azure"]

4. Adding Digital Ocean and Sorting the List ๐ŸŒŠ

cloud_providers.append("Digital Ocean")
cloud_providers.sort()
ย