w3resource

What are the built-in data types in Python?

Built-in data types in Python

Python provides several built-in data types to represent different kinds of data. Here are fundamental data types in Python:

Numeric Data Types:

int: Integer data type represents whole numbers, positive or negative, without fractions.

float: Floating-point data type represents real numbers with a decimal point or an exponent.

Text Data Type:

str: String data type represents a sequence of characters, enclosed within single, double, or triple quotes.

Boolean Data Type:

bool: Boolean data type represents a binary value, either True or False, used for logical operations.

Sequence Data Types:

list: List data type represents an ordered collection of elements, mutable (modifiable).

tuple: Tuple data type represents an ordered collection of elements, immutable (cannot be changed after creation).

range: Range data type represents an immutable sequence of numbers in a specified range.

Mapping Data Type:

dict: Dictionary data type represents a collection of key-value pairs, where each key is unique and associated with a value.

Set Data Types:

set: Set data type represents an unordered collection of unique elements, where duplicates are automatically removed.

frozenset: Frozenset data type represents an immutable version of a set.

Sequence of Characters:

bytes: Bytes data type represents a sequence of bytes, similar to a string, but immutable.

bytearray: Bytearray data type represents a mutable sequence of bytes.

None Data Type:

None: None data type represents absence of value. It is often used to represent null or undefined Data values.

Other Data Types:

complex: Complex data type represents complex numbers with a real and imaginary part (e.g., 3+5j).

Example:

Code:

# Examples of different data types
x = 42              # int
pi = 3.14           # float
me = "David"       # str
is_employee = True   # bool

list_nums = [1, 2, 3]           # list
tuple_nums = (10, 20, 30)       # tuple
dict_nums = {"Red": 2.0, "Green": 1.5}  # dict

set_data = {1, 2, 3, 4}          # set
frozenset_data = frozenset({1, 2})  # frozenset

no_value = None         # None
complex_number = 2 + 3j  # complex

print(type(x))    # Output: <class 'int'>
print(type(pi))   # Output: <class 'float'>
print(type(me)) # Output: <class 'str'>
print(type(is_employee))   # Output: <class 'bool'>
print(type(list_nums))   # Output: <class 'list'>
print(type(tuple_nums))  # Output: <class 'tuple'>
print(type(dict_nums))   # Output: <class 'dict'>
print(type(set_data))    # Output: <class 'set'>
print(type(frozenset_data))  # Output: <class 'frozenset'>
print(type(no_value))  # Output: <class 'NoneType'>
print(type(complex_number))  # Output: <class 'complex'>

Output:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
<class 'frozenset'>
<class 'NoneType'>
<class 'complex'>


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-interview/what-are-the-built-in-data-types-in-python.php