In this article, you’ll learn about different data types available in Python.
Python Data Types
Python has many built-in data types to choose from, and it’s important to understand what each of them is before writing any code with them. This article will take you through the built-in data types in Python and give you some examples of when to use each type. Let’s get started! Following are the built-in data types of Python:
Numeric | int, float, complex |
Text | str |
Sequence | list, tuple, range |
Mapping | dict |
Boolean | bool |
Set | set, frozenset |
Binary | bytes, bytearray, memoryview |
1. Numeric Type
The numeric type represents the data that has a numeric value. In Python, there are three main distinct numeric data types: integers, float, and complex. Each numeric type has different characteristics and uses. For example, integers are used for whole numbers, while floating-point numbers are used for fractional values. Complex numbers are used for imaginary values.
Examples:
# Integer x = 7 print("Type of x: ", type(x)) # Float y = 7.0 print("Type of y: ", type(y)) # Complex z = 5 + 2j print("Type of z: ", type(z))
Output:
Type of x: <class 'int'> Type of y: <class 'float'> Type of z: <class 'complex'>
2. Text Type
Text data, also known as string data, is one of the most common data types in Python. It is used to represent textual information, such as words or phrases. Strings are enclosed in quotation marks (single or double), and can contain any characters, including letters, numbers, and special characters. In Python, strings are immutable, which means they cannot be changed once they have been created. This makes them ideal for storing information that should not be modified, such as passwords or confidential information.
Examples:
############## Single Quotes a = 'AiHints provides content 4 you.' # Display text print(a) # Display data type print("Type of a: ", type(a)) ############### Double Quotes b = "AiHints provides content 4 you." # Display text print(b) # Display data type print("Type of b: ", type(b)) ################ Triple Quotes (Multiline String) c = '''AiHints provides content 4 you. The content is related to AI, ML, DL, Python, and CV.''' # Display text print(c) # Display data type print("Type of c: ", type(c))
Output:
AiHints provides content 4 you. Type of a: <class 'str'> AiHints provides content 4 you. Type of b: <class 'str'> AiHints provides content 4 you. The content is related to AI, ML, DL, Python, and CV. Type of c: <class 'str'>
3. Sequence Type
Python’s Sequence Type is a data structure that holds a sequence of elements. The most common Sequence Type is the list, but there are other types available as well, such as tuple and range. Each type has its own advantages and disadvantages, so it’s important to choose the right one for your needs. Here’s a quick overview of each type:
Examples:
x = [1, 2, 3, 4, 5] print("Type of x: ", type(x)) y = (1, 2, 3, 4, 5) print("Type of y: ", type(y)) z = range(5) print("Type of z: ", type(z))
Output:
Type of x: <class 'list'> Type of y: <class 'tuple'> Type of z: <class 'range'>
4. Mapping Type
Python’s mapping types are dictionaries, used to store key-value pairs. A dictionary is made up of keys and values. Keys can be any immutable data type, such as strings, numbers, or tuples. Values can be any type, including mutable types like lists or dictionaries.
Example:
x = {"student" : "Hussain", "marks" : 90} print("Type of x: ", type(x))
Output:
Type of x: <class 'dict'>
5. Boolean Type
In Python, the Boolean type is a data type that can be either True or False. Boolean values are often used to track whether a certain condition has been met or not. For example, you might use a Boolean value to track whether a user is logged in or not.
Examples:
a = True print("Type of a: ", type(a)) b = False print("Type of b: ", type(b))
Output:
Type of a: <class 'bool'> Type of b: <class 'bool'>
6. Set Type
In Python, the set type is a data type that consists of set and frozenset.
Examples:
a = {1, 2, 3, 4, 5} print("Type of a: ", type(a)) b = frozenset({1, 2, 3, 4, 5}) print("Type of b: ", type(b))
Output:
Type of a: <class 'set'> Type of b: <class 'frozenset'>
7. Binary Type
Python’s binary type stores data as a sequence of bits, typically eight bits per byte. The most common way to represent a binary number is with the base-2 numeral system, which uses the digits 0 and 1.
Examples:
a = b"AiHints" print("Type of a: ", type(a)) b = bytearray(7) print("Type of b: ", type(b)) c = memoryview(bytes(7)) print("Type of c: ", type(c))
Output:
Type of a: <class 'bytes'> Type of b: <class 'bytearray'> Type of c: <class 'memoryview'>
I highly recommend you get the “Python Crash Course Book” to learn Python.