Day 14 - Python Data Types and Data Structures for DevOps

Priyam KunduPriyam Kundu
3 min read

Data Types

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

Data Structures

Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

  • Lists Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type

  • Tuple Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.

  • Dictionary Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized

Task 1

  1. List:

    • Mutable: Lists are mutable, meaning their elements can be changed after creation.

    • Syntax: Lists are defined using square brackets [ ].

    • Ordered: Lists maintain the order of elements as they are inserted.

    • Example: [1, 2, 3, 4]

  2. Tuple:

    • Immutable: Tuples are immutable, meaning their elements cannot be changed after creation.

    • Syntax: Tuples are defined using parentheses ( ).

    • Ordered: Tuples also maintain the order of elements.

    • Example: (1, 2, 3, 4)

  3. Set:

    • Mutable: Sets are mutable like lists, but their elements are unique and unordered.

    • Syntax: Sets are defined using curly braces { }.

    • Unordered: Sets do not maintain the order of elements.

    • Unique Elements: Sets do not allow duplicate elements.

    • Example: {1, 2, 3, 4}

Task 2

Print Favorite Tool using Dictionary Methods

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

# Print favorite tool using dictionary methods
key = 2  # Key corresponding to "Git"
print("My favorite tool is:", fav_tools.get(key))

Task 3

Add Digital Ocean to Cloud Providers List and Sort Alphabetically

# List of cloud service providers
cloud_providers = ["AWS", "GCP", "Azure"]

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

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

# Print the sorted list
print("Sorted cloud providers list:", cloud_providers)

This code will print the favorite tool ("Git") using dictionary methods and then add "Digital Ocean" to the list of cloud providers and sort it alphabetically.

0
Subscribe to my newsletter

Read articles from Priyam Kundu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priyam Kundu
Priyam Kundu

Hi there 👋 I'm Priyam Kundu, a final year undergrad from Kolkata and an aspiring DevOps & Cloud ☁️ professional. As an aspiring DevOps engineer with a keen interest in Cloud ☁️ and DevOps, I'm here to grow and expand my knowledge in this field🚀. Looking forward to sharing my experiences and learning from all of you passionate professionals.