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

#90daysofdevops

#day14

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

To check what is the data type of the variable used, we can simply write: your_variable=100 type(your_variable)

Data Structure

Data structures in Python provide us with a way of storing and arranging data that is easily accessible and modifiable. These data structures include collections. Some of the most commonly used built-in collections are lists, sets, tuples, and dictionaries.

  1. Difference between List, Tuple, and set

List

  • Syntax includes square brackets [,] with ‘,’ separated data.

  • It is an ordered collection of data.

  • Duplicate data entry is allowed in a List.

  • Has integer-based indexing that has a value starting from ‘0’

  • New Items in a list can be added using the append() method.

      list1 = [1 , 2, 'abc', 3, 'def']
      list2 = []
      list3 = list((1,2,3))
      print(list1)
      # Output: [1, 2, 'abc', 3, 'def']
      print(list2)
      # Output: [ ]
      print(list3)
      # Output: [1, 2, 3]
    

Tuple

  • Syntax includes curved brackets ( , ) with ‘,’ separated data.

  • It is also an ordered collection of data

  • Duplicate data entry is allowed in a Tuple.

  • Also, has integer-based indexing that has a value starting from ‘0’

  • Tuples being immutable, contain data with which it was declared, hence no new data can be added to it

  • Being immutable, no data can be popped/deleted from a tuple

      tuple1=(1,2,'abc', 3, 4)
      tuple2=()
      tuple3=tuple((1,3,5,"hello"))
      print(tuple1)
      # Output: (1, 2, 'abc', 3, 4)
      print(tuple2)
      # Output: () )
      print(tuple3)
      # Output: (1, 2, 3, 'hello')
    

Dictionary

  • dictionaries strictly contain key-value pairs.
  • Syntax includes curly brackets { , } with ‘,’ separated key-value data.

  • Keys are unique, but two different keys CAN have the same value.

  • Has a Key based indexing i.e. keys identify the value.

dict1={"key1":"value1","key2":"value2"}
dict2={}
dict3=dict({1:"one",2:"two",3:"three"})
print(dict1)
# Output: {'key2': 'value2', 'key1': 'value1'}
print(dict2)
# Output: {}
print(dict3)
# Output: {1: 'one', 2: 'two', 3: 'three'}
  1. Create a 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" }

    image

    image

    3. Create a List of cloud service providers eg.

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

    image

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

    image

    image