A little build-up...
What is Data?
Data is a information that has been translated into a form that is efficient for movement or processing.
The above definition seems the most relevant for our use. Other definitions of data in binary digital form and likewise are not discussed here for simplicity.
Data Types
In software programming, data type refers to the type of value a variable has and what type of mathematical, relational or logical operations can be applied without causing an error.
The data type defines which operations can safely be performed to create, transform and use the variable in another computation.
Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string, lists, tuples), Boolean, Set, Dictionaries, etc.
To check what type of data a variable holds, use
type(variableName)
.
Data Structures
A data structure is a specialized format for organizing, processing, retrieving and storing data during runtime in RAM.
There are several data structures each built for specific use-cases.
Why we need data structures?
Typical base data types, such as integers or floating-point values, that are available in most computer programming languages are generally insufficient to capture the logical intent for data processing and use.
The applications however, must understand how data should be organized to simplify processing.
Data structures bring together the data elements in a logical way and facilitate the effective use, persistence and sharing of data.
Python Data structures
List
The list can be defined as an abstract data type in which the elements are stored in an ordered manner for easier and efficient retrieval.
Allows repetition of data
It is mutable
myList = [205, 'Kalnirnay', ("abcd", "efgh")]
Tuple
A tuple can be defined as an abstract data type in which the elements are stored in an ordered manner for easier and efficient retrieval.
Allows repetition of data
It is immutable
Contains a fixed number of elements
In order to change the contents of a tuple, it first needs to be converted to a mutable data structure like a list
my_tuple = (1, "namaste", {"key" : "value"}) # runs fine my_tuple[2] = 32 # exercise below at the end of the blog
Set
A set can be defined as an abstract data type in which the elements are stored in an unordered manner.
Repitition of elements is NOT allowed.
It is mutable.
var = {"string1", 234, ("omega", "alpha")}
Dictionaries
A dictionary can is an abstract data type in which the elements are stored in key-value pairs.
A single key can have multiple values but only by setting containers like set, lists, tuples, etc
It is mutable.
myDict = {1:"first", 2:"second"} print(myDict)
Tasks
Difference between List, Tuple and Set.
- Being ordered means that the items have a defined order, and that order will not change.
List | Tuple | Set | Dictionary | |
Mutability | Yes | No | Yes | No |
Syntax | [ ] | ( ) | { } | { key : value } |
Ordered/Unordered | Ordered | Ordered | Unordered | Ordered |
Create a dictionary and print your favorite tool from it.
fav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef" } print(fav_tools[2])
Create a List of cloud providers
cloud_providers = ["AWS", "GCP", "Azure", "Alibaba", "IBM", "Oracle", "Salesforce", "SAP", "VMWare"]
Simple exercise below :
my_tuple = (1, "namaste", {"key" : "value"}) # runs fine
my_tuple[1] = 32 # trying to change gives an ERROR
my_tuple[2]["key"] = "anime++" # this is VALID and doesn't give an error, why?
Check the comments in the above code-block there is an interesting question. The solution(you'll find) explains can be pretty useful during projects/scripting. It's fine if the concept of mutability and immutability is not understood right-away, you'll get along as you actually put it to some application.