Table of contents
- Data Types
- Data Structures
- Tasks
- Task 1: Give the Difference between List, Tuple, and Set
- Task 2: Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
- 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.
Data Types
Data Types refer to the various types of values or data elements that can be stored and manipulated within a program. In Python, being an object-oriented programming (OOP) language, everything is treated as an object.
To gain a deeper understanding of the OOP concept in Python, I recommend reading this comprehensive article on Python's object-oriented programming.
In Python, Data Types are implemented as classes, and variables are instances (objects) of these classes.
For a detailed explanation of Data Types, including their syntax and examples, I have covered them extensively in this blog post on Python for DevOps. I believe it can be a valuable resource for all of us.
Data Structures
Python offers several built-in data structures that are used to store and organize data efficiently. Here are the main data structures in Python:
Lists: Lists are ordered, mutable sequences that can contain elements of different data types. They are created using square brackets
[]
and elements are separated by commas. Lists support indexing, slicing, appending, and various other operations.Tuples: Tuples are similar to lists but are immutable, meaning they cannot be modified after creation. They are created using parentheses
()
or without any brackets. Tuples are commonly used to store related values together.Sets: Sets are unordered collections of unique elements. They are created using curly braces
{}
or theset()
constructor. Sets are useful for removing duplicates, performing mathematical set operations (such as union, intersection, and difference), and testing membership.Dictionaries: Dictionaries are unordered key-value pairs. Each value is associated with a unique key, allowing for efficient retrieval and modification of values. Dictionaries are created using curly braces
{}
or thedict()
constructor. They are commonly used to represent mappings and lookups.Strings: Strings are sequences of characters. They are created using single quotes
''
, double quotes""
, or triple quotes"""
. Strings in Python are immutable, meaning they cannot be modified after creation. Python provides many string manipulation methods for working with strings.Arrays: Arrays are similar to lists but can only contain elements of the same data type. They are created using the
array
module and provide more efficient storage and operations for numerical data.Linked Lists: Linked lists are a data structure composed of nodes, where each node contains a value and a reference to the next node. Linked lists can be implemented using custom classes in Python and are useful for dynamically growing and manipulating data.
Stacks: Stacks are a type of data structure that follows the Last-In-First-Out (LIFO) principle. They can be implemented using lists or the
deque
class from thecollections
module. Stacks are commonly used in algorithms involving depth-first search and backtracking.Queues: Queues are a type of data structure that follows the First-In-First-Out (FIFO) principle. They can be implemented using lists or the
deque
class from thecollections
module. Queues are commonly used in algorithms involving breadth-first search and task scheduling.
These are the main data structures available in Python. Each data structure has its own characteristics and use cases, and understanding them allows you to choose the appropriate one for your specific needs.
Tasks
Task 1: Give the Difference between List, Tuple, and Set
List | Tuple | Set | |
Definition | Ordered, mutable collection of elements | Ordered, immutable collection of elements | Unordered, mutable collection of unique elements |
Syntax | Square brackets [] | Parentheses () or without any brackets | Curly braces {} or set() constructor |
Mutable | Yes | No | Yes |
Indexing | Yes | Yes | No |
Duplicates | Allowed | Allowed | Not allowed (automatically removes duplicates) |
Usage | Suitable when elements may change or need to be modified | Suitable when elements should remain constant or not be modified | Suitable for removing duplicates or performing mathematical set operations |
Example | fruits = ["apple", "banana", "orange"] | coordinates = (10, 20) | unique_numbers = {1, 2, 3, 4, 5} |
Lists :
numbers = [2, 4, 6, 8, 10]
numbers.extend([12, 14, 16])
print("Extended:", numbers)
From this example, we can understand that List is mutable.
Tuples :
student = ("John Doe", 25, "Computer Science")
student[1] = 26
From this output, we can understand that tuples are immutable.
Sets :
fruits = {'apple', 'banana', 'orange', 'mango'}
print(fruits)
From the output, we can understand that sets are Unordered.
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"}
print("My favorite tools is ",fav_tools[3])
fav_tools = {1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef"}
: This line creates a dictionary calledfav_tools
. The dictionary contains key-value pairs, where the keys are integers (1, 2, 3, etc.) and the corresponding values are strings representing different tools.print("My favorite tool is ", fav_tools[3])
: This line prints the value associated with the key3
in thefav_tools
dictionary. The expressionfav_tools[3]
retrieves the value"Docker"
from the dictionary based on the keyThe output will be
My favorite tool is Docker
.
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.
cloud_providers = ["AWS","GCP","Azure"]
cloud_providers.append("Digital Ocean")
print(cloud_providers)
cloud_providers.sort()
print(cloud_providers)
The code you provided demonstrates the use of a list data structure in Python. Here's a step-by-step explanation of what each line does:
cloud_providers = ["AWS", "GCP", "Azure"]
: This line creates a list calledcloud_providers
and initializes it with three strings: "AWS", "GCP", and "Azure". The list represents different cloud service providers.cloud_providers.append("Digital Ocean")
: Theappend()
method is used to add an element to the end of the list. In this case, it adds the string "Digital Ocean" to thecloud_providers
list.print(cloud_providers)
: This line prints the updatedcloud_providers
list after adding "Digital Ocean". The output will be["AWS", "GCP", "Azure", "Digital Ocean"]
.cloud_providers.sort()
: Thesort()
method is used to sort the elements of a list in ascending order. In this case, it sorts thecloud_providers
list in alphabetical order.print(cloud_providers)
: This line prints the sortedcloud_providers
list. The output will be["AWS", "Azure", "Digital Ocean", "GCP"]
.
I can be reached on LinkedIn as Prajwal Zalaki to provide feedback, suggestions, and corrections for my blog. Your input is highly valued, and I am committed to enhancing the quality of my content. Please feel free to connect with me and share your insights.