Skip to main content

Command Palette

Search for a command to run...

Day 14 - Python Data Types and Data Structures

Published
โ€ข5 min readโ€ขView as Markdown
Day 14 - Python Data Types and Data Structures
A

๐Ÿš€ Welcome to the world of DevOps with Aakash! ๐ŸŒ

Hey tech enthusiasts! ๐Ÿ‘‹ I'm Aakash, a dedicated freelance Linux System Admin @xFusionCorp, and I'm on a mission to conquer the world of DevOps. Currently, I'm immersed in building exciting projects and sharing my learning experiences with you all.

๐Ÿ› ๏ธ My Tech Arsenal:

  • Docker
  • Linux
  • Kubernetes
  • Jenkins
  • AWS
  • Python
  • Java
  • Prometheus
  • Grafana
  • Ansible
  • Terraform

Join me in navigating the intricate realms of AWS DevOps technology. Let's embrace learning with gratitude and thrive in this ever-evolving landscape!

๐Ÿค Connect with me for:

  • Friendly chats
  • Shared experiences
  • Learning moments

Follow my journey on Twitter and Linkedin for daily insights and tech goodness. Let's build, automate, and innovate our way through the exciting world of DevOps! ๐Ÿš€ #DevOps #AWS #TechEnthusiast #InnovationJourney #90DaysOfDevOps

Introduction

Hello, DevOps enthusiasts! On Day 14 of our expedition, we will explore the nuances that surround Python's data types and structures. These are fundamental elements in achieving excellence as programmers. Let us come together to navigate through the intricate terrain of Python programming while uncovering vital principles essential to coding success.

Data Types

Python's data types form the basis of our algorithms and dictate allowable operations while adding structure to our programs. To understand their functionality, let us explore some of Python's available data types.

Numeric: These data types include integers, floating-point numbers, and complex numbers which enable precise numerical computation.

age = 27
pi = 3.14
complex_number = 2 + 3j

The variable "age" is assigned a value of 27. The constant pi is assigned the value of 3.14. Lastly, the variable "complex_number" has been set to equal 2 plus imaginary number 3j.

Sequential: Data types such as strings, lists and tuples are sequential in nature. These ordered collections of elements provide a flexible means for manipulating data.

name = "Alice"
my_list = [1, 2, 3, 4, 5]
my_tuple = (6, 7, 8)

The variables 'name', 'my_list', and 'my_tuple' have been assigned the values "Alice", [1, 2, 3, 4, 5], and (6 ,7 ,8) respectively in Python.

Boolean: The Boolean data types encompass logical values that depict either True or False, and are of great importance in decision-making within algorithms.

learning_is_ongoing = True

Set: A set is a collection of unique elements that are not ordered, which allows for the execution of set operations and guarantees data integrity.

numbers_with_no_duplicates = set([1, 2, 3, 4, 5])

Dictionaries: Dictionaries demonstrate the association of values with keys, creating a streamlined approach for accessing data based on distinct identifiers.

data = {"name": "Bob", "age": 30, "job": "DevOps Engineer"}

Data Structures

Alongside data types, data structures offer an effective way to organize and manipulate data. Here, we will explore some essential Built-In Python data structures.

Lists: Lists are similar to dynamic arrays, as they provide mutable collections that can accommodate various types of data. This makes them useful for facilitating the manipulation of dynamic data.

books_i_enjoy = ["The Hobbit", "Harry Potter", "The Alchemist"]

Tuple: Tuples are sequences that cannot be changed and contain defined sets of elements. They guarantee data validity and simplify the process of iteration by increasing efficiency.

size = [1920, 1080]

Dictionary: Once again, dictionaries function as associative arrays that provide key-value pairs for efficient data retrieval. They demonstrate the effectiveness of organizing data with speed and accuracy.

employee_info = {"name": "Eve", "age": 25, "department": "IT"}

The information of the employee named Eve includes their name, age (25), and department which is IT. This data is stored in a dictionary format using Python programming language as shown above:

Practice

Scenario-based question:

You are a software engineer working on a project that requires handling different types of data structures in Python. During a team meeting, a junior developer asks you to explain the differences between lists, tuples, and sets, as they're confused about when to use each one. Your task is to provide a clear explanation and then guide them through a hands-on exercise to solidify their understanding.

Tasks:

  1. Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.

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

  3. Create a List of cloud service providers eg.

Solution

Task 1:

Lists, tuples, and sets are all fundamental data structures in Python, each with its unique characteristics and use cases.

  1. List:

    • Lists are ordered collections of items that allow duplicates.

    • They are mutable, meaning you can add, remove, or modify elements after creation.

    • Lists are represented by square brackets [ ].

    • Example: my_list = [1, 2, 3, 4, 5]

  2. Tuple:

    • Tuples are ordered collections of items that allow duplicates.

    • They are immutable, meaning once created, you cannot change the elements.

    • Tuples are represented by parentheses ( ).

    • Example: my_tuple = (1, 2, 3, 4, 5)

  3. Set:

    • Sets are unordered collections of unique items.

    • They do not allow duplicates, and the elements are not indexed.

    • Sets are represented by curly braces { }.

    • Example: my_set = {1, 2, 3, 4, 5}

Task 2:

  1. First, let's create and manipulate these data structures in Python.

  2. Open a Python interpreter or a code editor and execute the following code snippets:

# Creating a list
my_list = [1, 2, 3, 4, 5]
print("List:", my_list)

# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
print("Tuple:", my_tuple)

# Creating a set
my_set = {1, 2, 3, 4, 5}
print("Set:", my_set)
  1. Now, let's work with dictionary methods to retrieve your favorite tool using its key:
fav_tools = {
  1: "Linux",
  2: "Git",
  3: "Docker",
  4: "Kubernetes",
  5: "Terraform",
  6: "Ansible",
  7: "Chef"
}

# Using dictionary methods to print your favorite tool
tool_key = 3
print("My favorite tool is:", fav_tools.get(tool_key))
  1. Finally, let's create a list of cloud service providers:
cloud_providers = ["AWS", "GCP", "Azure"]
print("Cloud Service Providers:", cloud_providers)

Take screenshots of your code execution and results to document your understanding and practice and Upload it on LinkedIn to share your learnings in Public.

Conclusion

We have finished exploring the data types and structures in Python, which has given us valuable knowledge on the fundamental components of programming with this language. Going forward, we'll dive deep in Python in the upcoming days. Let curiosity, tenacity and an unwavering commitment towards excellence be our guiding principles as we strive for expertise.

Ending this with a quote

Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill

Happy Learning

90DaysOfDevOps

Part 27 of 39

In this series I will be positing my learnings of DevOps for 90 days continuously. We will be starting from the basics covering linux and will go upto covering the advanced concepts of DevSecOps.

Up next

Day 13 - Basics of Python

Introduction Hey there, fellow DevOps enthusiasts! So, you're intimidated on hearing about coding, huh? Maybe you've been wondering if it's crucial for becoming a kick-ass DevOps engineer. Well, I got you covered. In this blog we're about to embark o...

More from this blog

D

DevOps with Aakash: Unleashing DevOps Excellence

44 posts

I'm Aakash, a freelance Linux System Admin @xFusionCorp, diving deep into the world of Docker, Kubernetes, Jenkins, AWS, and more. Let's navigate AWS DevOps tech together! Connect with me for friendly chats, shared experiences, and learning moments. Follow my journey on Twitter and Linkedin for daily insights. Let's build, automate, and innovate in the exciting world of DevOps! ๐ŸŒ