Day 14 of 90 Days of DevOps Challenge: Diving into Python Data Types and Data Structures

·

4 min read

Hello Everyone! 🌟

As we continue our journey into Python, it’s time to explore some core concepts that are essential for programming and DevOps tasks—Data Types and Data Structures. Understanding these will not only improve our coding skills but also make our programs more efficient and organized.


What Are Data Types?

Data types classify the kind of data we work with in Python. Since everything in Python is an object, data types are essentially classes, and our variables are instances of these classes. Here’s a quick rundown of Python’s built-in data types:

  1. Numeric: Integers (e.g., 42) Floats (e.g., 3.14) Complex Numbers (e.g., 1+2j)

  2. Sequential: Strings (e.g., "Hello World") Lists (e.g., [1, 2, 3]) Tuples (e.g., (1, 2, 3))

  3. Boolean: True or False

  4. Set: Unordered collections of unique items (e.g., {1, 2, 3})

  5. Dictionaries: Key-value pairs (e.g., {"name": "Alice", "age": 25})

To check the type of a variable, you can use the type() function:

your_variable = 100 print(type(your_variable))

Understanding Data Structures

Data structures are ways of organizing and storing data to make it accessible and efficient.

Python offers several built-in data structures:

  1. Lists: Ordered collections of items. Mutable, meaning you can change their content. Example: my_list = [1, 2, 3, 'Python']

  2. Tuples: Ordered collections of items, similar to lists. Immutable, meaning once created, they cannot be changed. Example: my_tuple = (1, 2, 3, 'Python')

  3. Dictionaries: Unordered collections of key-value pairs. Keys are unique and used to access corresponding values. Example: my_dict = {"name": "Alice", "age": 25}


Hands-On Tasks

  1. List, Tuple, and Set Comparison

    To understand the differences between lists, tuples, and sets, here’s a brief comparison:

    Lists: Mutable, ordered, can contain duplicate items. Tuples: Immutable, ordered, can contain duplicate items. Sets: Mutable, unordered, no duplicate items.

    Here’s how you might use each:

     my_list = [1, 2, 2, 3]
     my_tuple = (1, 2, 2, 3)
     my_set = {1, 2, 3}
    
  2. Working with Dictionaries

    Create a dictionary of your favorite tools and use dictionary methods to retrieve values:

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

    Create and modify a list of cloud service providers:

     cloud_providers = ["AWS", "GCP", "Azure"] 
     cloud_providers.append("Digital Ocean") 
     cloud_providers.sort() 
     print(cloud_providers) 
     # Output: ['AWS', 'Azure', 'Digital Ocean', 'GCP']
    

Sample Code

Here is a sample code that I made for a better understanding:

Difference between List, Tuple and Disctionary

# Example of a List
my_list = [1, 2, 3, 4, 5]
print("List:")
print(my_list)
print("Type:", type(my_list))
print("Length:", len(my_list))
print("Mutable:", True)  # Lists are mutable
print()

# Example of a Tuple
my_tuple = (1, 2, 3, 4, 5)
print("Tuple:")
print(my_tuple)
print("Type:", type(my_tuple))
print("Length:", len(my_tuple))
print("Mutable:", False)  # Tuples are immutable
print()

# Example of a Dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
print("Dictionary:")
print(my_dict)
print("Type:", type(my_dict))
print("Length:", len(my_dict))
print("Keys:", my_dict.keys())
print("Values:", my_dict.values())
print("Mutable:", True)  # Dictionaries are mutable
print()

# Example of a Set
my_set = {1, 2, 3, 4, 5}
print("Set:")
print(my_set)
print("Type:", type(my_set))
print("Length:", len(my_set))
print("Mutable:", True)  # Sets are mutable, but elements are unique
print("Unique Elements:", len(my_set) == len(set(my_set)))  # Sets automatically remove duplicates
print()

# Differences Recap:
print("Differences Recap:")
print("1. List: Ordered, mutable, allows duplicates.")
print("2. Tuple: Ordered, immutable, allows duplicates.")
print("3. Dictionary: Key-value pairs, mutable, keys are unique, unordered.")
print("4. Set: Unordered, mutable, unique elements, no duplicates allowed.")

Working with Dictionaries

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

print(fav_tools)
print(fav_tools[1])

Manipulating Lists

#creating a list
cloud_providers = ["AWS", "GCP", "Azure"]

#adding an element at the end
cloud_providers.append("Digital Ocean")

#sorting the list
cloud_providers.sort()

#printing the complete list
print(cloud_providers)


Further Exploration

To deepen your understanding, explore Python tutorials and practice these concepts. Hands-on experience is the best way to learn! Share Your Learning Journey

As always, I encourage you to share your learning experiences with the community. Post about your tasks, insights, and challenges on LinkedIn. Let’s inspire each other and continue growing our skills together!

Happy Coding! 🚀

Â