Python Data Types
- Previous Page Python Variables
- Next Page Python Numbers
Built-in data types
In programming, data type is an important concept.
Variables can store different types of data, and different types can perform different operations.
In these categories, Python has the following built-in data types by default:
Text type: | str |
Numeric type: | int , float , complex |
Sequence type: | list , tuple , range |
Mapping type: | dict |
Set type: | set , frozenset |
Boolean type: | bool |
Binary type: | bytes , bytearray , memoryview |
Get data type
You can use the type() function to get the data type of any object:
Instance
Print the data type of variable x:
x = 10 print(type(x))
Setting data type
In Python, when you assign a value to a variable, you set the data type:
Examples | Data Types | Try It |
---|---|---|
x = "Hello World" | str | Try It |
x = 29 | int | Try It |
x = 29.5 | float | Try It |
x = 1j | complex | Try It |
x = ["apple", "banana", "cherry"] | list | Try It |
x = ("apple", "banana", "cherry") | tuple | Try It |
x = range(6) | range | Try It |
x = {"name" : "Bill", "age" : 63} | dict | Try It |
x = {"apple", "banana", "cherry"} | set | Try It |
x = frozenset({"apple", "banana", "cherry"}) | frozenset | Try It |
x = True | bool | Try It |
x = b'Hello' | bytes | Try It |
x = bytearray(5) | bytearray | Try It |
x = memoryview(bytes(5)) | memoryview | Try It |
Set Specific Data Type
If you want to specify the data type, you can use the following constructors:
Examples | Data Types | Try It |
---|---|---|
x = str('Hello World') | str | Try It |
x = int(29) | int | Try It |
x = float(29.5) | float | Try It |
x = complex(1j) | complex | Try It |
x = list(('apple', 'banana', 'cherry')) | list | Try It |
x = tuple(('apple', 'banana', 'cherry')) | tuple | Try It |
x = range(6) | range | Try It |
x = dict(name='Bill', age=36) | dict | Try It |
x = set(('apple', 'banana', 'cherry')) | set | Try It |
x = frozenset(('apple', 'banana', 'cherry')) | frozenset | Try It |
x = bool(5) | bool | Try It |
x = bytes(5) | bytes | Try It |
x = bytearray(5) | bytearray | Try It |
x = memoryview(bytes(5)) | memoryview | Try It |
- Previous Page Python Variables
- Next Page Python Numbers