Python Data Types and Data Structures | Python for DevOps | Part 2

Python Data Types and Data Structures | Python for DevOps | Part 2

·

4 min read

In this blog, we will discuss in detail Python Data Types and Data Structures.

DataTypes

What are Data Types? Data types are the different kinds of values or data items that can be stored and manipulated in a program. Since Python is an OOPs language, i.e., Object Oriented Programming, everything is an object.

To understand and know more about the OOPs concept for Python, this article explains in detail: the Python OOPs concept.

Data Types are classes, Variables are instances(objects) of these classes.

I have discussed Data Types and their basic syntax with examples in detail in this blog: Python Data Types. I find it can be useful for all of us.

Data Structures

Data structures are ways of organizing and storing data in a computer's memory or in storage devices. They can be used to efficiently store, retrieve, and manipulate data in various algorithms and applications.

In Python, there are several built-in data structures that are commonly used in data manipulation and analysis. These include:

  1. Lists: A mutable, ordered collection of elements. Lists can contain elements of different data types.

  2. Tuples: An immutable, ordered collection of elements. Tuples can contain elements of different data types.

  3. Sets: An unordered collection of unique elements. Sets can only contain hashable data types.

  4. Dictionaries: An unordered collection of key-value pairs. Dictionaries can contain elements of different data types.

  5. Arrays: A homogeneous, multidimensional collection of elements. Arrays can be created using the NumPy library.

  6. Linked Lists: A data structure that consists of a sequence of nodes, where each node contains a value and a reference to the next node in the sequence. Linked lists can be implemented using Python classes.

  7. Stacks and Queues: Abstract data types that are used to manage data in a last-in, first-out (LIFO), or first-in, first-out (FIFO) manner, respectively. These can be implemented using lists or other data structures.

  8. Trees and Graphs: Data structures that are used to represent hierarchical or interconnected relationships between elements. These can be implemented using Python classes.

In today's blog let's complete a few tasks related to Lists, tuples, Sets, and Dictionaries.

Tasks

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

 

Lists

Tuples

Sets

Mutability

Mutable

Immutable

Immutable

Ordering

Ordered

Ordered

Unordered

Duplicates

Can contain duplicate elements

Can contain duplicate elements

Elements must be unique

Syntax

Defined using square brackets []

Defined using curly braces ()

Defined using parentheses {}

Usage

Ordered collections of related values that may need to be modified.

Fixed collections of related values that should not be modified.

When it's important to quickly determine whether an element is a member of the collection or not.

Let us understand how these work using practical examples:

Lists:

fruits = ['apple', 'banana', 'orange', 'apple']
fruits[1] = 'pear'
print(fruits)

From the output, it's observed that the list is modified.

Tuples

days_of_week = ('Monday', 'Tuesday', 'Wednesday')
print(days_of_week)
days_of_week[1] = ('Thursday')

From the output error, we can see that we cannot modify a tuple.

Sets

all_fruits = {'apple', 'banana', 'orange', 'apple'}
print(all_fruits)

From the output, we can observe that the duplicate element is not considered and the output is unordered.

Task 2: Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.

Input:

fav_tools = {1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef"}
print("My favorite tools is ",fav_tools[1])

Output:

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

Input:

cloud_providers = ["AWS","GCP","Azure"]
cloud_providers.append("Digital Ocean")
print(cloud_providers)
cloud_providers.sort()
print(cloud_providers)

There are various ways to add elements to a list in Python. One is using append(). It appends the element to the end of the list.

To sort the list alphabetically sort() is used.

Well, this was it for this blog. In the next blog, let's learn more about Python. Until then, keep reading my blogs and connect with me on LinkedIn and let's have a conversation.


To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.

#Day14 #90DaysofDevops

Did you find this article valuable?

Support Sneha K S by becoming a sponsor. Any amount is appreciated!