Data types classify data values. they represent how they will used or need data type to perform any operation on them.
Remember if you try do any operation on variable of one type and variable of another, you will get an error.
In Python, common data types include:
int (Integer)
: Whole numbers.
float:
Decimal numbers.
str (String)
: Text.
bool (Boolean):
True or False.
list:
Ordered collection.
tuple:
Immutable ordered collection.
dict (Dictionary):
Key-value pairs.
set:
Unordered collection of unique elements.
bytes:
Immutable sequence of bytes.
bytearray:
Mutable sequence of bytes.
memoryview:
Memory buffer interface.
NoneType:
Represents absence of value.
below is the examples of all, click run to see the output :-
# Integer x = 5 print(type(x)) # <class 'int'> # Float y = 3.14 print(type(y)) # <class 'float'> # Complex z = 2 + 3j print(type(z)) # <class 'complex'> # String name = "John" print(type(name)) # <class 'str'> # List numbers = [1, 2, 3] print(type(numbers)) # <class 'list'> # Tuple coordinates = (10, 20) print(type(coordinates)) # <class 'tuple'> # Range r = range(5) print(type(r)) # <class 'range'> # Dictionary person = {"name": "Alice", "age": 30} print(type(person)) # <class 'dict'> # Set unique_numbers = {1, 2, 3} print(type(unique_numbers)) # <class 'set'> # Frozenset frozen_numbers = frozenset({4, 5, 6}) print(type(frozen_numbers)) # <class 'frozenset'> # Boolean is_student = True print(type(is_student)) # <class 'bool'> # Bytes b = bytes([65, 66, 67]) print(type(b)) # <class 'bytes'> # Bytearray ba = bytearray(5) print(type(ba)) # <class 'bytearray'> # Memoryview mv = memoryview(ba) print(type(mv)) # <class 'memoryview'> # None none_type = None print(type(none_type)) # <class 'NoneType'>
Some time we may get query like
which two python data types can relate to JSON
?
- Remember the two Python data types that can relate to JSON
are dict
(Dictionary) and list
(Ordered collection).