The data type is one of the most important components of python. You can do a lot of interesting things with these data types. It is a fact that in python, all data types are objects.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
The data type is like a specification about the type of data you want to store in memory, and python has several built-in data types:
- String text type: str
- Numeric types: int, float, complex
- Sequence types: list, tuple, range
- Mapping type: dict
- Set data type: set, frozenset
- Boolean type: bool
- Binary type: byte, bytearray, memoryview
Now let’s categorize all these data types using the function type()
to display the data type of the variable.
Text format
String data type str
str stands for string (string) used to store text in python. Strings can be written in double or single quotes, so you can write like this:
Eg:
Result:
Hello, world! <class 'str'>
Number type
Integer data type int
int stands for integer (integer) used to store integers (positive integers and negative integers).
Eg:
Result:
4 <class 'int'>
float . real number data type
float stands for real number (decimal).
Eg:
Result:
3.14 <class 'float'>
A complex number has a real and imaginary part, each of which is a real number. Complex numbers can be written in two forms: real + (imag)jcomplex(real, imag).
Eg:
Result:
(5+10j)
<class 'complex'>
Sequence type data type
list
List (list) is a data type where you can store a set of data. List can also contain different data types. Lists can also sort, change elements, and allow duplicates
Eg:
Result:
['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye'] <class 'list'>
tuple
Tuple is a data type where you can store a collection of data. Tuples can also contain different data types. Tuples are sortable, cannot change elements, and allow elements to be duplicated.
Eg:
Result:
('Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye') <class 'tuple'>
range
The range type represents an immutable (unchangeable) sequence of numbers. Usually used to repeat a specific number of times in a for loop.
Eg:
Result:
range(0, 10) <class 'range'>
Mapped data type
dict
dict stands for dictionary in python. Dict is used to store data values ​​in key:values. Dict is an unordered, mutable set, and no duplicates are allowed.
Eg:
Result:
{'Learning': 'Programming', 'Language': 'Python', 'Day': 4} <class 'dict'>
Data type Set
set
set is a data type where you can hold a set of data. Set can also contain different data types. Set is unordered and unindexed and does not allow duplicate elements.
Eg:
Result:
{'Black Widow', 'Iron Man', 'Thor', 'Hawkeye', 'Hulk', 'Captain America'} <class 'set'>
frozenset
The frozenset data type can be created by the function frozenset()
. Jaw frozenset()
accepts an iterable object and returns an immutable frozenset object (just like a set object, just immutable).
Eg:
Result:
frozenset({'cherry', 'banana', 'apple'}) <class 'frozenset'>
Boolean type
bool
bool stands for boolean in python. Booleans represent one of two values: True or False.
Eg:
Result:
True <class 'bool'> False <class 'bool'>
Binary type
bytes
The byte data type can be created in two ways: using the byte() function or using the “b” prefix.
Eg:
Result:
b'hello' <class 'bytes'> b'Hello' <class 'bytes'>
bytearray
Jaw bytearray()
returns a bytearray object. It can convert objects to bytearray objects.
Eg:
Result:
bytearray(b'\x00\x00\x00\x00') <class 'bytearray'>
memoryview
jaw memoryview()
returns an object in the memory view of a specified object.
Eg:
Result:
<memory at 0x2b4f7a8a7408> <class 'memoryview'>
Note
As you have seen, some data types can also be implemented using their constructors. This same technique can also be applied to any data type.
Eg:
Result:
Hello, World! 4 3.14 (5+10j) ['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye'] ('Captain America', 'Iron Man', 'Thor', 'Hulk', 'Black Widow', 'Hawkeye') range(0, 10) {'Learning': 'Programming', 'Language': 'Python', 'Day': 4} {'apple', 'cherry', 'banana'} frozenset({'banana', 'cherry', 'apple'}) True False b'\x00\x00\x00\x00' bytearray(b'\x00\x00\x00\x00') <memory at 0x2b8346a29408>
If you guys support and I find this article view is high, I will do some more articles comparing data types in python. So please support me.