Here I have covered the Data Structures in Python with examples.
New day, New Topic... Let's learn along💪
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 more simply 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:
A
dictionary
in Python is an unordered collection ofdata values
, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair.Key-value is provided in the dictionary to make it more optimized.
Tasks:
Give the Difference between List, Tuple and set.
list:
- Lists are just like arrays, declared in another language which is an
ordered collection of data
.
# DataType Output: list
x = ["geeks", "for", "geeks"]
Tuples:
- Just like a list, a tuple is also an
ordered collection of Python objects
. The only difference between a tuple and a list is that tuples areimmutable
i.e. tuples cannot be modified after it is created.
# DataType Output: tuple
x = ("geeks", "for", "geeks")
Set:
- A Set is an unordered collection of data types that is
iterable
,mutable
and has no duplicate elements.
# DataType Output: set
x = {"geeks", "for", "geeks"}
Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
Method 1:
Method 2:
Write a program to add Digital Ocean
to the list of cloud_providers and sort the list in alphabetical order.
- Create a List of cloud service providers.
cloud_providers = ["AWS","GCP","Azure"]