Day 14 Task: Python Data Types and Data Structures for DevOps

What is 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

Here are the different data types in Python:

  1. Numeric types: These include integers, floats, and complex numbers.

  2. Boolean type: This is a binary data type that can have one of two values, either True or False.

  3. Sequence types: These include strings, lists, and tuples.

  4. Dictionary type: This is an unordered collection of key-value pairs.

  5. Set type: This is an unordered collection of unique elements.

  6. None type: This is a special type in Python that represents the absence of a value.

What is 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.

Python also provides some advanced data structures such as stacks, queues, and heaps, which are used to store and manipulate data based on specific requirements. These data structures can be implemented using built-in data structures or custom data structures.

Here is your today’s Tasks:

01.Give the Difference between List, Tuple and set. Do Hands-on and comment down as per your understanding.

Data Structures in Python are a way of organizing and storing data in a structured manner, making it easier to access and manipulate the data. Python offers several built-in data structures, such as lists, tuples, dictionaries, sets, and more, which can be used to store and manipulate data efficiently.

Lists are the most commonly used data structure in Python, and they are used to store a collection of items in a specific order. They can be modified easily and are mutable, meaning their elements can be changed. Tuples, on the other hand, are similar to lists but are immutable, meaning their elements cannot be changed once they are defined.

Dictionaries are used to store key-value pairs, and each key is associated with a value. They are mutable and can be modified easily. Sets are similar to lists but can only contain unique elements, and they are unordered.

Python also provides some advanced data structures such as stacks, queues, and heaps, which are used to store and manipulate data based on specific requirements. These data structures can be implemented using built-in data structures or custom data structures.

Here is some differences in terms of their characteristics and functionality:

  1. Mutability: Lists and sets are mutable, which means you can add, remove, or modify elements. Tuples are immutable, which means once you create a tuple, you cannot modify its elements.

  2. Syntax: Lists are defined using square brackets [], tuples are defined using parentheses (), and sets are defined using curly braces {}.

  3. Duplicate elements: Lists and tuples can have duplicate elements, while sets cannot.

  4. Indexing: Lists and tuples are indexed, which means you can access individual elements by their position in the sequence using indexing notation (e.g., my_list[0]). Sets are not indexed.

  5. Ordering: Lists and tuples are ordered, which means the elements have a specific order that is maintained. Sets are unordered, which means the elements have no guaranteed order.

  6. Usage: Lists are commonly used for sequences of data that need to be modified. Tuples are often used to represent a fixed sequence of data, such as coordinates. Sets are often used when you need to perform set operations, such as union or intersection, on a collection of data.

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

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

Here’s an example code to print your favorite tool just by using the keys of the dictionary:

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

# Using Dictionary method get() to print the value of the key "2"
print("My favorite tool is", fav_tools.get(2))

Here’s an explanation of the code:

  1. First, we define a dictionary called fav_tools which contains key-value pairs of integers and strings respectively. The integers are the keys and the strings are the corresponding values.

  2. Next, we use the get method of the dictionary to get the value corresponding to the key 2, which is "Git". We store this value in a variable called tool.

  3. Finally, we print the value of tool using the print function.

Output:

My favorite tool is Git

So, when you run this code, it will print the string "Git", which is the value corresponding to the key 2 in the fav_tools dictionary.

03. Create a List of cloud service providers eg.

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

Code for List of cloud providers:

cloud_providers = 
[
  "AWS",
  "Azure", 
  "Google Cloud", 
  "IBM Cloud"
]

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

Code to add Digital Ocean to the list of cloud providers:

# Creating the cloud providers list
cloud_providers = ["AWS", "Azure", "Google Cloud"]

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

# Sort the list in alphabetical order
cloud_providers = sorted(cloud_providers)

# Print the sorted list
print(cloud_providers)

In this program, we first define a list called cloud_providers with three cloud service providers. We then use the append() method to add "Digital Ocean" to the list.

Next, we use the sorted() function to sort the list in alphabetical order. The sorted() function returns a new sorted list, which we then assign back to cloud_providers.

Finally, we use the print() function to display the sorted list.

Output:

['AWS', 'Azure', 'Digital Ocean', 'Google Cloud', 'IBM Cloud']