Python Datatypes

Datatypes

Based on the nature of data, we can divide the datatypes in Python into many categories. The following table provides the datatypes for each category.

CategoryDatatypes
Numericint, float, complex
Booleanbool
Sequencelist, tuple, range
Textstr
Mappingdict
Setfrozenset, set
Binarybytes, bytearray, memoryview
NoneNoneType

Examples

The following table gives Python examples, and the respective constructor function, for each of the datatypes.

The constructor function is used to explicitly specify the datatype when creating the value of that datatype.

DatatypeExampleBuiltin Function
intx = 14int()
floatx = 3.14float()
complextx = 3 + 4jcomplex()
strx = "Hello World"str()
listx = ["apple", "banana", "cherry"]list()
tuplex = ("apple", "banana", "cherry")tuple()
rangex = range(20)range()
dictx = {"name" : "apple", "quantity" : 20}dict()
setx = {"apple", "banana", "cherry"}set()
frozensetx = frozenset({"apple", "banana", "cherry"})frozenset()
boolx = Truebool()
bytesx = b"apple"bytes()
bytearrayx = bytearray(5)bytearray()
memoryviewx = memoryview(bytes(5))memoryview()
NoneTypex = None

Frequently Asked Questions

1. What are the numeric datatypes in Python?

Answer:

int, float, and complex are the numeric datatypes in Python.

2. What are the two possible values of boolean type?

Answer:

True and False are the two possible values of boolean type in Python.

3. Which datatype do you use to define a string of characters in Python?

Answer:

You can use 'str' to define a string of characters in Python. The str class also has built-in methods to work on the string values, which makes it the trivial choice for the character strings.

4. What is the difference between a frozenset and set in Python?

Answer:

frozenset and set in Python are used to store a set of items. The basic difference between the two datatypes is that: frozenset cannot be modified, whereas set can be modified.

Related Tutorials

Code copied to clipboard successfully 👍