Python Data Types and Data Structures for DevOps
Python
Python is a flexible, high-level programming language for diverse tasks, including web development, data analysis, scientific computing, automation, and more. Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras, etc. Python is known for its simplicity and readability. Python is an open-source programming language, which means it is freely available to the public to use and modify without any restrictions.
Basic Python Data Types
Booleans (bool): are used to represent true or false values. Booleans are used for logical operations and conditional statements.
is_true = True
is_false = False
Integers (int): are used to represent whole numbers, both positive and negative, without a decimal point.
a = 7
Floating-point Numbers (float): are used to represent numbers with decimal points.
b = 2.34
Strings (str): are used to represent text data.
text = "Hello!"
Lists (list): are ordered collections of items. Lists can contain elements of different data types.
my_list = [1, 2, "three"]
Sets (set): are unordered collections of unique elements. They are often used to eliminate duplicates or perform mathematical set operations.
unique_numbers = {1, 2, 3, 4}
Dictionaries (dict): contains key-value pairs. It is useful for creating mappings and associative arrays.
person = {"name": "Jane", "age": 30}
Tuples (tuple): are similar to lists but immutable, meaning you cannot change the elements after creation.
(1, 2, 3, 4)
NoneType (None): Represents the absence of a value. It's often used to indicate the lack of a specific result or uninitialized variables.
result = None
Data Structures
Data Structures are a way of organizing data to be accessed more efficiently depending on the situation. Data Structures are fundamentals of any programming language a program uses. Python helps to learn the fundamentals of these data structures more simply than other programming languages.
Lists Python: Lists are like the arrays declared in other languages, an ordered data collection. 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, i.e., the elements in the tuple cannot be added or removed once created. 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 contains the key: value pair. Key-value is provided in the Dictionary to make it more optimized.
Tasks
- Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.
Characteristic | Lists | Tuples | Sets |
Mutability | Mutable (can be changed) | Immutable (cannot be changed) | Mutable (can be changed) |
Syntax | Enclosed in square brackets [ ] | Enclosed in parentheses ( ) | Enclosed in curly braces { } |
Order | Ordered (elements maintain the order of insertion) | Ordered (elements maintain the order of insertion) | Unordered (elements have no specific order) |
Duplicates | Allows duplicate elements | Allows duplicate elements | Does not allow duplicate elements |
Access | Elements can be accessed by index | Elements can be accessed by index | Elements can be accessed using iteration (no index) |
Examples | my_list = [1, 2, 3] | my_tuple = (1, 2, 3) | my_set = {1, 2, 3} |
Lists
Tuples
Sets
Create the below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.
fav_too l =
{ 1: "Linux", 2: "Git", 3: "Docker", 4: "Kubernetes", 5: "Terraform", 6: "Ansible", 7: "Chef" }
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]
Thanks for reading!