Python Data Types and Data Structures for DevOps | Day 14 of 90 Days of DevOps

Ajit Fawade
3 min readAug 1, 2023

--

Welcome to Day 14 of my #90DaysOfDevOps journey!

Today, we’ll explore Python’s powerful data types and data structures, essential for any DevOps engineer.

Let’s dive into the world of Python!

Data Types in Python

Python is a dynamically-typed language, which means you don’t need to explicitly specify the data type of a variable. Here are some common data types:

  • Integer (int): Represents whole numbers, e.g., 5, -7. Integers can be positive or negative.
  • Float (float): Represents numbers with a decimal point, e.g., 3.14, -2.71. Floats are used to store real numbers.
  • String (str): Represents textual data enclosed in single or double quotes, e.g., "Hello", 'DevOps'. Strings are versatile and widely used for text manipulation.
  • Boolean (bool): Represents the truth values True or False. Booleans are essential for logical operations and control flow.

You can check the data type of any variable using the type() function:

number = 42
print(type(number)) # Output: <class 'int'>

Data Structures in Python

Data structures are essential for organizing and managing data efficiently. Python offers versatile data structures:

Lists (list):

Lists are ordered collections of items, and they are mutable, meaning you can change their contents after creation. Lists are enclosed in square brackets [ ] and can contain elements of different data types.

numbers = [1, 2, 3, 4]
fruits = ["apple", "banana", "orange"]
mixed_list = [10, "hello", True]

You can access elements in a list using index positions, and you can modify, add, or remove elements.

Tuples (tuple):

Tuples are similar to lists, but they are immutable, meaning you cannot change their contents after creation. Tuples are enclosed in parentheses ( ).

point = (10, 20)
colors = ("red", "green", "blue")
single_item_tuple = (42,)

Tuples are useful when you want to create a fixed collection of elements that should not be modified.

Dictionaries (dict):

Dictionaries are unordered collections of key-value pairs, and they are mutable. Each key in a dictionary must be unique. Dictionaries are enclosed in curly braces { }.

person = {"name": "Alice", "age": 30, "city": "New York"}
car = {"brand": "Toyota", "model": "Corolla", "year": 2022}

You can access values in a dictionary using their keys, and you can add, modify, or delete key-value pairs.

List vs. Tuple vs. Dictionary

Here’s a comparison of the three data structures:


+------------+-----------------------------------------+----------------------------------------------+-----------------------------------------+
| Feature | List | Tuple | Dictionary |
+------------+-----------------------------------------+----------------------------------------------+-----------------------------------------+
| Mutability | Mutable (Can be changed after creation) | Immutable (Cannot be changed after creation) | Mutable (Can be changed after creation) |
| | | | |
| Order | Ordered | Ordered | Unordered |
| | | | |
| Syntax | [element1, element2, ...] | (element1, element2, ...) | {key1: value1, key2: value2, ...} |
+------------+-----------------------------------------+----------------------------------------------+-----------------------------------------+

  • List: Use lists when you need an ordered collection of elements that can be modified.
  • Tuple: Use tuples when you want an ordered collection of elements that should not be modified.
  • Dictionary: Use dictionaries when you need to store data as key-value pairs, and you want to be able to access, add, modify, or delete the data based on its keys.

Now, let’s complete the tasks for Day 14:

Task 1: Dictionary Fun

fav_tools = { 
1: "Linux",
2: "Git",
3: "Docker",
4: "Kubernetes",
5: "Terraform",
6: "Ansible",
7: "Chef"
}
# Print your favorite tool by using the keys of the dictionary
# Let's say your favorite tool is Docker, which has key 3
print(fav_tools[3]) # Output: "Docker"

Task 2: List Manipulation

cloud_providers = ["AWS", "GCP", "Azure"]
# Add "Digital Ocean" to the cloud_providers list
cloud_providers.append("Digital Ocean")
# Sort the cloud_providers list in alphabetical order
cloud_providers.sort()
print(cloud_providers) # Output: ["AWS", "Azure", "Digital Ocean", "GCP"]

Conclusion

Python’s data types and data structures are the building blocks of DevOps. They enable you to handle data with ease and efficiency. Whether it’s lists, tuples, or dictionaries, understanding and using these data structures will make your Python programming journey smoother and more enjoyable.

Connect with me on LinkedIn 🤝

Check out my GitHub for more resources 📚

Happy coding in Python! 🐍🚀

--

--

No responses yet