Day 14 : Python Data Types and Data Structures

Day 14 : Python Data Types and Data Structures

Data Types

  • Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

  • Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.

  • 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 is the data type of the variable used, we can simply write:

variable=100; type(variable)

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 fundamentals of these data structures in a simpler way as compared to other programming languages.

  • Lists: 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.

  • Tuple: 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.

  • Dictionary: Dictionary is like hash tables in any other language with the time complexity of O(1). 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

Tasks

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

ListTupleSetDictionary
A list is a non-homogeneous data structure that stores the elements in columns of a single row or multiple rows.A Tuple is also a non-homogeneous data structure that stores elements in columns of a single row or multiple rows.The set data structure is also non-homogeneous but stores the elements in a single row.A dictionary is also a non-homogeneous data structure that stores key-value pairs.
The list can be represented by [ ]A tuple can be represented by ( )The set can be represented by { }The dictionary can be represented by { }
The list allows duplicate elementsTuple allows duplicate elementsThe Set will not allow duplicate elementsThe dictionary doesn’t allow duplicate keys.
The list can be nested among allA tuple can be nested among allThe set can be nested among allThe dictionary can be nested among all
Example: [1, 2, 3, 4, 5]Example: (1, 2, 3, 4, 5)Example: {1, 2, 3, 4, 5}Example: {1: “a”, 2: “b”, 3: “c”, 4: “d”, 5: “e”}
A list can be created using the list() functionA tuple can be created using the tuple() function.A set can be created using the set() functionA dictionary can be created using the dict() function.
A list is mutable i.e. we can make any changes in the list.A tuple is immutable i.e. we can not make any changes in the tuple.A set is mutable i.e. we can make any changes in the set, but elements are not duplicated.A dictionary is mutable, but Keys are not duplicated.
List is orderedTuple is orderedSet is unorderedThe dictionary is ordered
Creating an empty list l = []Creating an empty Tuple t = ()Creating a set a = set()Creating an empty dictionary d = { }

List: List is a datatype in Python, that we can write as a list of comma-separated values, in square brackets. The list is mutable. This can store any type of element.

Tuple: Python Tuple is a collection of objects separated by commas. In some ways, a tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.

Set: A Set in Python programming is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Dictionary: A Dictionary in Python is a collection of key values, used to store data values like a map, which, unlike other data types holds only a single value as an element.

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" }

3.Create a List of cloud service providers eg. cloud_providers = ["AWS", "GCP", "Azure"]. Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

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