DAY - 14 Python Data Types and Data Structures for DevOps

DAY - 14 Python Data Types and Data Structures for DevOps

ยท

5 min read

TABLE OF CONTENTS

Introduction :

In the previous blog, we learned about the various Data Types in Python.

  • Data types are fundamental concepts in programming that define the type of data a variable can hold or a program can process.

  • They include integers (whole numbers), floating-point numbers (with decimal points), booleans (true/false), strings (sequences of characters), characters (single symbols), arrays (collections of elements), lists (dynamic collections of elements), tuples (immutable collections), dictionaries (key-value pairs), sets (collections of unique elements), enums (named symbolic constants), and pointers (memory addresses).

  • Properly choosing data types is crucial for memory efficiency, performance, and correctness in programming.

  • For a detailed understanding pls refer to sonal1597.hashnode.dev/day-13-introduction-..

How to check what type of variable is used?

In Python, we can check the data type of a variable using the built-in function type(). The type() function returns the data type of the given variable.

COPY

COPY

integer_variable = 42
float_variable = 3.14
boolean_variable = True
string_variable = "Hello, World!"


print(type(integer_variable))  
print(type(float_variable))    
print(type(boolean_variable))  
print(type(string_variable))

output :

COPY

COPY

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>

In the above example, type() is used to check the data types of integer_variable, float_variable, boolean_variable, and string_variable, and it prints the corresponding data types: int, float, bool, and str, respectively.

Data Structures in Python:

In Python, data structures are used to organize and store data in a specific format, making it easier to access, modify, and process the data efficiently.

Python provides several built-in data structures, and some of the commonly used ones are:

  1. Lists:

    Lists are ordered collections that can contain elements of different data types.

    They are mutable, meaning you can modify their elements after creation.

    Lists are denoted by square brackets [].

    COPY

    COPY

      my_list = [1, 2, 3, 'hello', True]
    
  2. Tuples:

    Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation.

    They are denoted by parentheses ().

    COPY

    COPY

      my_tuple = (1, 2, 3, 'world', False)
    
  3. Dictionaries:

    Dictionaries are unordered collections of key-value pairs.

    Each value in the dictionary is associated with a unique key.

    Dictionaries are denoted by curly braces {}.

COPY

COPY

dict = {'name': 'John', 'age': 30, 'is_student': False}
  1. Sets:

    Sets are unordered collections of unique elements.

    They do not allow duplicate values.

COPY

COPY

set = {1, 2, 3, 4, 4}

COPY

COPY

# Output
{1, 2, 3, 4}      (duplicate 4 is removed)

Tasks :

Task 1 :

Give the Difference between List, Tuple and Set. Do Handson and put screenshots as per your understanding.

Let's see the differences between lists, tuples, and sets,

  1. List:

    • Ordered collection of elements.

    • Mutable: You can modify, add, or remove elements after creation.

    • Allows duplicate elements.

    • Denoted by square brackets [].

  2. Tuple:

    • Ordered collection of elements.

    • Immutable: Once created, the elements cannot be changed.

    • Allows duplicate elements.

    • Denoted by parentheses ().

  3. Set:

    • An unordered collection of unique elements (no duplicates allowed).

    • Mutable: You can add or remove elements after creation.

    • Elements are not indexed, so you cannot access elements by their position.

    • Denoted by curly braces {} or by using the set() function.

Let's go with a hands-on example to demonstrate the differences. We will create a list, a tuple, and a set and observe their behavior.

COPY

COPY

# List
list = [1, 2, 3, 3, 4, 5]
print("List:", list)

# Tuple
tuple = (1, 2, 3, 3, 4, 5)
print("Tuple:", tuple)

# Set
set = {1, 2, 3, 3, 4, 5}
print("Set:", set)

Output:

COPY

COPY

List: [1, 2, 3, 3, 4, 5]
Tuple: (1, 2, 3, 3, 4, 5)
Set: {1, 2, 3, 4, 5}

As we can see, the list and tuple both allow duplicate elements, while the set automatically removes duplicates to maintain unique elements.

Task 2:

Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.

COPY

COPY

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

COPY

COPY

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

# your favorite tool's key is 4 (Kubernetes)
favorite_tool_key = 4

# your favorite tool from the dictionary
favorite_tool = fav_tools.get(favorite_tool_key)

# Printing the favorite tool
print("My favorite tool is:", favorite_tool)

Output :

COPY

COPY

My favorite tool is: Kubernetes

Task 3:

  • Create a List of cloud service providers

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

  • Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

  • [Hint: Use keys to built-in functions for Lists]

Here's a Python program to add "Digital Ocean" to the list of cloud_providers and sort the list in alphabetical order:

COPY

COPY

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

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

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

print("Updated list of cloud service providers:")
print(cloud_providers)

When you run this program, it will add "Digital Ocean" to the list and sort it in alphabetical order. The output will be:

COPY

COPY

Updated list of cloud service providers:
['AWS', 'Azure', 'Digital Ocean', 'GCP']

Conclusion:

In conclusion, data structures are fundamental building blocks in computer science and programming. They provide a way to organize and store data efficiently, allowing for quick access, retrieval, and manipulation of information. Various data structures, such as arrays.

Choosing the appropriate data structure is essential for optimizing the performance of algorithms and solving various problems efficiently. Understanding the strengths and weaknesses of different data structures is crucial for designing efficient and scalable software applications.

I hope you find the blog post helpful! If you do, don't forget to like ๐Ÿ‘, comment ๐Ÿ’ฌ, and share ๐Ÿ“ข it with your friends and colleagues.

ย