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

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

Variables

Variables are containers for storing data values and do not need to be declared with any particular type, and can even change type after they have been set.

How to create a variable in Python?

To create a variable, just assign any value to it -

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 classes and variables are instances (objects) of these classes.

  • Python has the following built-in data types 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 -

Data Structures

  • Data Structures are a way of organizing data so that it can be accessed more efficiently depending on 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.

  • Python has the following built-in data structures by default:

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

      Lists are defined in [].

    • 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. Just like a List, a Tuple can also contain elements of various types.

      Tuples are defined in ().

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

      A dictionary is defined as {key:value}

    • Set - Python Set is an unordered collection of data that is mutable and does not allow any duplicate element. Sets are used to include membership testing and eliminating duplicate entries. When we print it, the elements are not in order.

      Sets are defined using {}.

How to change the datatype to a string?

To change a data type to a string, use str()

Task 1: Give the Difference between List, Tuple and Set.

List: Can be defined as ["string",1,2,3]. It can have duplicate values.

Tuple: Can be defined as ("string",1,2,3). It can have duplicate values.

Set: Can be defined as {"string",1,2,3}. It doesn't print duplicate values.

Here in the output, we can see that the set has a single "1" while there's a "1" in the last at line no-4 in the code.

Also, to declare an empty set, you can use a={} because this will mean you are declaring an empty dictionary.

Task 2: 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" }

To print a value from a dictionary, we have to use its key to use it.

Task 3: Create a List of cloud service providers and write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

To append anything to a list, use append().

To sort the list, use sort().

Thanks for reading!

#devops#90DaysOfDevops#TrainWithShubham

Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200