In the last article we looked into different types of Data Types in Python.Just to revise they are
Integers
Floating Point Numbers
Complex Numbers
Strings
Lists
Tuples
Ranges
Booleans
Sets
Dictionaries.
We are now going to see How they are categorized
Python has a number of built-in data types, including:
Numeric data types: These types store numeric values, such as integers, floating-point numbers, and complex numbers.
Sequence data types: These types store a collection of items in a specific order, such as lists, tuples, and strings.
Set data types: These types store a collection of unique items, without any specific order.
Mapping data types: These types store key-value pairs, such as dictionaries.
Numeric data types
The following table shows the different numeric data types in Python:
Data type | Description |
int | Stores whole numbers, such as 1, 10, and 100. |
float | Stores numbers with decimal places, such as 3.14159 and 27.5. |
complex | Stores complex numbers, which are numbers of the form a + bi, where a and b are real numbers and i is the imaginary unit. |
Sequence data types
The following table shows the different sequence data types in Python:
Data type | Description |
list | Stores a collection of items in a specific order. Lists can contain items of any type, including other lists. |
tuple | Stores a collection of items in a specific order. Tuples are similar to lists, but they are immutable, meaning that they cannot be changed once they are created. |
string | Stores a sequence of characters. Strings can be used to represent text, code, or any other type of data that is represented by a sequence of characters. |
Set data types
The following table shows the different set data types in Python:
Data type | Description |
set | Stores a collection of unique items, without any specific order. Sets can contain items of any type, including other sets. |
Mapping data types
The following table shows the different mapping data types in Python:
Data type | Description |
dict | Stores a collection of key-value pairs. Dictionaries can contain keys and values of any type, including other dictionaries. |
Custom data types
In addition to the built-in data types, Python also allows us to create our own custom data types using classes. This allows us to create data types that are specifically tailored to our needs.
For example, we could create a custom data type to represent a student, with fields for the student's name, ID number, and grades. We could then use this data type to store information about all of the students in a class.
Checking the data type of a variable
We can use the type()
function to check the data type of a variable. For example, the following code will print the data type of the variable x
:
Python
x = 100
print(type(x))
Output:
<class 'int'>
Data Structures
Data structures are a way of organizing data in a computer so that it can be used efficiently. They are the fundamental building blocks of computer programs, and they are used to implement a wide variety of algorithms.
Data structures are important because they allow us to:
Store and access data efficiently.
Perform complex operations on data in a reliable way.
Write code that is more readable and maintainable.
Python is a good language for learning data structures because it provides a number of built-in data structures, such as lists, tuples, dictionaries, and sets. Python also makes it easy to create custom data structures using classes.
Here is a more in-depth overview of some of the most common data structures in Python:
Lists
Lists are ordered collections of data. They can contain items of any type, and they can be resized as needed. Lists are implemented as dynamic arrays, meaning that they can grow and shrink as needed.
Lists are one of the most commonly used data structures in Python. They are used to store all sorts of data, such as lists of numbers, strings, and objects. Lists can also be used to implement other data structures, such as stacks and queues.
Tuples
Tuples are similar to lists, but they are immutable, meaning that they cannot be changed once they are created. Tuples are implemented as fixed-size arrays, meaning that they have a fixed size once they are created.
Tuples are often used to store data that does not need to be changed, such as the coordinates of a point or the dimensions of an image. Tuples can also be used as keys in dictionaries.
Dictionaries
Dictionaries are unordered collections of key-value pairs. The keys can be of any type, and the values can be of any type. Dictionaries are implemented as hash tables, meaning that they can efficiently access data based on the key.
Dictionaries are often used to store data that needs to be accessed quickly and efficiently, such as a lookup table or a database. Dictionaries can also be used to implement other data structures, such as graphs.
Sets
Sets are unordered collections of unique items. The items in a set can be of any type. Sets are implemented as hash tables, meaning that they can efficiently check if an item is in the set.
Sets are often used to remove duplicate items from a list or to perform set operations such as union and intersection.
Custom data structures
In addition to these built-in data structures, Python also allows us to create custom data structures using classes. This is useful for creating data structures that are tailored to our specific needs. For example, we could create a custom data structure to represent a graph or a tree.
Examples of data structures in Python
Here are some examples of how data structures are used in Python:
- Lists are used to store lists of data, such as a list of numbers, strings, or objects. For example, the following code uses a list to store a list of numbers:
Python
numbers = [1, 2, 3, 4, 5]
- Tuples are used to store data that does not need to be changed, such as the coordinates of a point or the dimensions of an image. For example, the following code uses a tuple to store the coordinates of a point:
Python
point = (10, 20)
- Dictionaries are used to store data that needs to be accessed quickly and efficiently, such as a lookup table or a database. For example, the following code uses a dictionary to store a lookup table of country codes:
Python
country_codes = {
"USA": "+1",
"UK": "+44",
"India": "+91"
}
- Sets are used to remove duplicate items from a list or to perform set operations such as union and intersection. For example, the following code uses a set to remove duplicate items from a list:
Python
numbers = [1, 2, 3, 4, 1, 5]
unique_numbers = set(numbers)
Conclusion
Data structures are an essential part of Python programming. By understanding and using data structures correctly, we can write code that is more efficient, reliable, and readable.
Difference between Data Type and Data Structure
Characteristic | Data type | Data structure |
Definition | Defines the type of data that can be stored | Defines how data is organized |
Examples | int, float, string | list, tuple, dictionary, set |
Purpose | To ensure that the correct type of data is stored in a variable | To allow data to be accessed and manipulated efficiently |
Difference between List,Tuple and Set
Characteristic | List | Tuple | Set |
Definition | An ordered collection of data | An immutable ordered collection of data | An unordered collection of unique data |
Mutability | Mutable (can be changed after creation) | Immutable (cannot be changed after creation) | Mutable |
Duplicates | Allowed | Not allowed | Not allowed |
Uses | Storing and accessing data in a specific order | Storing data that does not need to be changed | Removing duplicate items from a list or performing set operations |
Example | [1, 2, 3, 4, 5] | (1, 2, 3, 4, 5) | {1, 2, 3, 4, 5} |
Some Handson :
- Create Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.
- 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.
Thankyou for reading until here.Hope it added some value to your understanding.I am grateful to you for checking out my journey.See you in the next one.