Python has the following built-in data types. The following table provides the definition, name in Python, examples, and how to set data types. You can also click links to see in-depth explanations and examples.
Data Types | Names in Python | Examples | Set Data Types |
---|---|---|---|
Text | str | “Hello World” | str(“Hello World”) |
Numeric | int | 12 | int(12) |
float | 12.5 | float(12.5) | |
complex | 2k | complex(2k) | |
Sequence | list | [‘Tesla’,’Toyato’,’Ford’] | list([‘Tesla’,’Toyato’,’Ford’]) |
tuple | (‘Tesla’,’Toyato’,’Ford’) | tuple((‘Tesla’,’Toyato’,’Ford’)) | |
range | range(3) | range(3) | |
Mapping | dict | {‘brand’:’Tesla’,’year’:2021} | dict(brand=’Tesla’,year=2021) |
Set | set | {“Tesla”,”Toyato”,”Ford”} | set({“Tesla”,”Toyato”,”Ford”}) |
frozenset | frozenset({‘brand’:’Tesla’,’year’:2021}) | frozenset({‘brand’:’Tesla’,’year’:2021}) | |
Boolean | bool | True | bool(2) |
Binary | bytes | b’hello world’ | bytes(“hello world”,’utf-8′) |
bytearray | bytearray(b’hello world’) | bytearray(“hello world”,”utf-8″) | |
memoryview | <memory at 0x000001F1EDBD4700> | memoryview(bytes(“hello world”,”utf-8″)) |
How you can define a Dictionary in Python
I have provided separate posts for almost all the data types mentioned above in the table, even for dictionaries. However, I think it is necessary to provide additional examples here for dictionaries. As we can see below, there are 2 different ways of defining a dictionary, one using brackets and the one using dict(). Note that, the syntax is slightly different between them.
dictionary_1={'brand':'Tesla','year':2021}
print(dictionary_1)
dictionary_2=dict(brand='Tesla',year=2021)
print(dictionary_2)
The following is the output.
{'brand': 'Tesla', 'year': 2021} {'brand': 'Tesla', 'year': 2021}