Skip to main content

Day 14:Python Data Types and Data Structures for DevOps

Published 4 min readMarkdown
Day 14:Python Data Types and Data Structures for DevOps

Python important DataStructre with their explanation:-

Tuples:-

A tuple is a collection of ordered, immutable elements enclosed in parentheses.

You can also create a tuple using the built-in tuple() function, like this

Tuples support indexing and slicing, just like strings.

In Python, you can concatenate two or more tuples using the + operator. When you concatenate tuples, you create a new tuple that contains all the elements of the original tuples in the order they were concatenated. Here's an example:

Lists:-

A list is a collection of ordered, mutable elements enclosed in square brackets. Each element in the list can be of any data type such as integer, string, float, or even another list. It also defines by using the list() method.

There are many built-in methods that you can use to manipulate lists. Here are some of the most commonly used list methods:

  1. append() - Adds an element to the end of the list

  2. extend() - Adds multiple elements to the end of the list

  3. insert() - Inserts an element at a specific position in the list

  4. remove() - Removes the first occurrence of an element from the list

  5. pop() - Removes and returns the last element of the list

  6. index() - Returns the index of the first occurrence of an element in the list

  7. count() - Returns the number of times an element appears in the list

  8. sort() - Sorts the list in ascending order

Sets:-

A set is an unordered collection of unique elements. They are similar to lists and tuples, but they cannot have duplicate elements, and their order is not guaranteed. Sets are defined using curly braces {} or using the set() function.

Sets have many built-in methods that you can use to manipulate them.

  1. add() - Adds an element to the set

  2. update() - Adds multiple elements to the set

  3. remove() - Removes an element from the set

  4. discard() - Removes an element from the set if it is a member. If the element is not a member, it does not raise an error.

  5. pop() - Removes and returns an arbitrary element from the set

  6. clear() - Removes all elements from the set

  7. union() - Returns the union of two sets (i.e., all the elements that are in either set)

  8. intersection() - Returns the intersection of two sets (i.e., all the elements that are in both sets)

  9. difference() - Returns the difference between two sets (i.e., all the elements that are in set1 but not in set2)

Dictionary:-

A dictionary is an unordered collection of key-value pairs, where each key is unique and associated with a value. Dictionaries are defined using curly braces {} or using the dict() function.

Dictionaries have many built-in methods that you can use to manipulate them.

  1. get() - Returns the value associated with a key. If the key is not found, it returns a default value (None by default).

  2. keys() - Returns a list of all the keys in the dictionary

  3. values() - Returns a list of all the values in the dictionary

  4. items() - Returns a list of all the key-value pairs in the dictionary as tuples

  5. pop() - Removes and returns the value associated with a key

  6. update() - Adds or updates key-value pairs in the dictionary

  7. clear() - Removes all key-value pairs from the dictionary

Tasks:-

  1. Give the Difference between List, Tuple and Set.
Collection VS FeaturesMutableOrderedIndexingDuplicate Data
List✔✔✔✔
Tuple𐄂✔✔✔
Set✔𐄂𐄂𐄂
Dictionary✔✔✔𐄂
  1. Create the 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"
}

Input:-

fav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef"}
favorite_tool_key = 3
favorite_tool = fav_tools[favorite_tool_key]
print("my favorite tool is",favorite_tool)

Output:-

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.

Input:-

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

Output:

About the author

No comments yet. Be the first to comment.

More from this blog