Data Types and Data Structures in Python

Data Types and Data Structures in Python

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: your_variable=100 type(your_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 fundamental of these data structures in a simpler way as compared to other programming languages.

  • 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

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

  • Dictionary Python 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 and set. Do Handson and put screenshots as per your understanding.

List, Tuple, and Set are three commonly used data structures in Python. Each of them has its unique features and use cases. Here are the differences between them:

  1. List:
  • A list is a collection of ordered elements.

  • It is mutable, which means the elements in the list can be changed.

  • Lists are defined with square brackets [ ] and elements are separated by commas.

Example: list = [1, 2, 3, 'four', 'five']

  1. Tuple:
  • A tuple is a collection of ordered elements.

  • It is immutable, which means that once created, the elements in the tuple cannot be changed.

  • Tuples are defined with parentheses () and elements are separated by commas.

Example: my_tuple = (1, 2, 3, 'four', 'five')

  1. Set:
  • A set is an unordered collection of unique elements.

  • It is mutable, which means the elements in the set can be changed.

  • Sets are defined with curly braces {} or with the set() function.

Example: my_set = {1, 2, 3, 4, 5} or my_set = set([1, 2, 3, 4, 5])

In summary, the main differences between lists, tuples, and sets are their mutability, order, and uniqueness. Lists are mutable and ordered, tuples are immutable and ordered, and sets are mutable and 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"
}

Task 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.

[Hint: Use keys to built-in functions for Lists]

If you want to deep dive further, Watch Python

*****************************************************************************

for code - GitHub

Thanks for reading! Hope you find this helpful.

Happy learning !!!

Suggestions are always welcome.

~Sumit

Thank You - Shubham Londhe