Day 14: Python Data Types and Data Structures for DevOps

Day 14: Python Data Types and Data Structures for DevOps

Python Data types

Python data types are used to define the type of variable.

In Python, a data type communicates with the interpreter about how the programmer intends to use the data and information stored.

type() function is used to find the data type of a variable.

For example:

your_variable = 100
print(type(your_variable))

This command tells that the your_variable will belongs to <class 'int'>.

Typecasting: It is used for converting one data type to another.

Suppose we have a 'float' datatype and we want to convert it to 'int' we can do this using typecasting.

num = 121.56 #float
print(type(int(num)))

We can directly typecast using this way or we can do using passing the value to another variable and convert the data type

num = 121.56 #float
numint=int(num)
print(type(numint))

Data structure for DevOps

Data structures are used for organizing and managing data efficiently.

Two types of Python data structures: Primitive and Non Primitive

Primitive: A primitive data structure can store the value of only one data type.

Non Primitive: Non-primitive data structure is a type of data structure that can store data of more than one type.

Difference between List, Tuple, and Dictionary

List: It allows us to store multiple items in a single value.

  • Ordered

  • mutable

  • indexed

  • Duplicate allowed

  • Any datatype

  • A mix of different data types

  • Enclosed in square brackets []

fruits=["Apple", "Mango", "Orange", "Banana", "Apple"]
print(type(fruits)) # class=List
print(fruits)       # print the list
print(len(fruits))  # length=4
print(fruits[0:4])  # Accesing items of a list

#Adding elements to a list
newfruit=["Guava","Cherry"]
fruits.extend(newfruit)  #It will append another list to current list
print(fruits)

#Removing element of a list
fruits.remove("Cherry") #It will remove Apple from the list
print(fruits)

Output:

Tuple: It is used to store multiple items in a variable.

  • Ordered

  • Immutable

  • Duplicate allowed

  • Any Data type

  • A mix of different data types

  • Enclosed in parenthesis ()

#create a tuple
col = ("red", "blue","green","red")
print(col) #print all the items
print(type(col)) #type=tupple
print(len(col)) #length = 4

#creating a tuple with 1 item
fruits=("apple",) #if we have to crate tuple with 1 item we have to give comma after item otherwise it is consider as string
print(fruits)
print(type(fruits)) #string
print(len(fruits)) #length =1

#Accesiing items in tuple
colors=("red","blue","green","yellow")
print(colors[1])#positive indexing
print(colors[-1]) #negative indexing
print(colors[1:3])#range indexing
print(colors[-2:])#negative indexing -> it will print till last tuple range

Output:

Set: It is a container storing multiple values in a variable.

  • Unordered

  • Mutable

  • Duplicates not allowed

  • Unindexed

  • Any data type

  • A mix of different data types

  • Enclosed in curly braces {}

#Creating a set
names={"Sia","Tia", "Jia"}
print(names) #it will print in any sequence
print(type(names)) #class=set
print(len(names)) #length=3

#add elements in sets
names.add("Ria")
print(names)   # out: {'Tia', 'Ria', 'Sia', 'Jia'}
names.add("Tia") # if name is already present in sets then it will not print the duplicate value
print(names) # duplicate value are not allowed in sets

#add another sequence in set
name_list=("james", "Gun")
names.update(name_list)
print(names)   # out:{'Tia', 'james', 'Gun', 'Ria', 'Sia', 'Jia'}

#for removing element from a set
names.remove("Tia")
print(names)
#names.remove("loki") #if name is not present then it will throw an error
names.discard("loki")#this function willl not throw an error if value is not present in sets
print(names) #out: {'james', 'Gun', 'Ria', 'Sia', 'Jia'}

Output:

Some hands-on questions:

Task 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"
}
#Access favourite tool using key value
print("My favourite tool is ", fav_tools[4])

Output:

Task 2: 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.

cloud_providers = ["AWS","GCP","Azure"]
#Add digital Ocean to the list
cloud_providers.append("Digital Ocean") # ['AWS', 'GCP', 'Azure', 'Digital ocean']
#sort the list alphabetically
cloud_providers.sort()
#print the list
print("Cloud providers:", cloud_providers) # ['AWS', 'Azure', 'Digital ocean', 'GCP']

Output:

Summary

In this blog, we see the concept of data structures in which we see the difference between List, Tuple, and Set. We also cover how we change one data type to another data type. Knowing about lists, tuples, and sets helps DevOps engineers do their job better with Python. It lets them organize things, automate tasks, and work with data in their scripts and workflows.