Python Data Types and Data Structures for DevOps
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 instances (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 Structure
In Python, data structures are like containers that help us store and manage information in a neat and efficient way. They are tools that make working with data simple and organized. Here are some common data structures in Python:
Lists: Lists are like ordered shopping bags that can hold different things together. You can add, remove, or change items in the bag.
Tuples: Tuples are similar to lists but more like sealed packages. Once you put things inside, you can't change them.
Dictionaries: Dictionaries work like phone books, where you find names (keys) and their corresponding numbers (values). It helps you quickly find what you need.
Sets: Sets are like collections of unique items, just like a group of friends without any duplicates.
Strings: Strings are like sentences made up of letters. They help us work with text and words.
Arrays: Arrays are specialized tools to work with numbers efficiently. They are helpful for advanced math and calculations.
Task 1: Give the Difference between List, Tuple, and Set.
1. List:
Lists are ordered collections of elements.
They are mutable, which means you can add, remove, or modify elements.
Lists are defined using square brackets
[]
.Elements in a list can be duplicates.
my_list = [1, 2, 3, 2, 4]
2. Tuple:
Tuples are ordered collections of elements, just like lists.
However, tuples are immutable, meaning that once created, their elements cannot be changed.
Tuples are defined using parentheses
()
.Elements in a tuple can be duplicates.
my_tuple = (1, 2, 3, 2, 4)
- Set:
A set is an unordered collection of unique elements enclosed in curly braces
{}
or created using theset()
function.Sets are mutable, so you can add or remove elements after creating them.
Sets do not allow duplicate elements. If you try to add a duplicate element, it will be automatically removed.
Sets are typically used when you need to perform set operations like union, intersection, and difference.
my_set = {1, 2, 3, 4}
Task 2: Create the below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.
# Create the fav_tools dictionary
fav_tools = {1: "Linux", 2: "Git", 3: "Docker", 4: "Kubernetes", 5: "Terraform", 6: "Ansible", 7: "Chef"}
# Your favorite tool's key (assuming you like "Docker" in this example)
your_favorite_tool_key = 3
# Print your favorite tool using the dictionary method
favorite_tool = fav_tools[your_favorite_tool_key]
print("Your favorite tool is:", favorite_tool)