Python Lists, Tuples and Dictionaries

Python Lists, Tuples and Dictionaries

#90 Days of DevOps Challenge #Day 14 Task #TrainWithShubam

Python Installation and Data Types

Data Structures

  • Data Structures are a way of organizing data so that it can be accessed more efficiently depending on the situation.

  • Data Structures are fundamentals of any programming language around which a program is built.

  • Python helps to learn the fundamental of these data structures more simply as compared to other programming languages.

  • Example: Lists Python Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

  • Example: Tuple Python Tuple is a collection of Python objects much like a list but Tuples are immutable i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.

  • Example: Dictionary Python dictionary is like hash tables in any other language. It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, a Dictionary holds the key: value pair. Key-value is provided in the dictionary to make it more optimized

Difference Between List, Tuple and Dictionary.

  • List and Tuple are the same both can store a list of different datatypes or similar datatypes and items can be picked by using an index.

  • The list is represented in square brackets [ ], Tuple is represented in curly braces ( )

  • we can edit items in the list whereas we cannot edit items in a tuple it is immutable.

  • A dictionary is like a table where we can store the values in key: pair format which is very helpful to get the items based on key and we can retrieve data using the key value.

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

#Creating a Dictionary

fav_tools = { 
  1:"Linux", 
  2:"Git", 
  3:"Docker", 
  4:"Kubernetes", 
  5:"Terraform", 
  6:"Ansible", 
  7:"Chef"
} 

#Printing my favourite tool from Dictionary

print(fav_tools[3])

Create a List of cloud service providers eg.

#Creating a List of cloud service providers eg.
cloud_providers = ["AWS","GCP","Azure"]
#printing cloud providers
print(cloud_providers)

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

#Creating a List of cloud service providers eg.
cloud_providers = ["AWS","GCP","Azure"]

#printing cloud providers
print(cloud_providers)

#Adding Digital Ocean to cloud_providers using append function
cloud_providers.append("Digital Ocean")
print(cloud_providers)

#Sorting the cloud providers using sort function
cloud_providers.sort()
print(cloud_providers)