Python Data Types

Definition(s):

Basic Data Types

TypeDescriptionExample
intInteger numbersx = 5
floatFloating-point numbersy = 3.14
complexComplex numbersz = 2 + 3j
strText/stringname = "Python"
boolBoolean (True/False)is_valid = True
NoneTypeRepresents absence of valuex = None

Collection Types

TypeMutableDescriptionExample
listYesOrdered, changeable sequence[1, 2, 3]
tupleNoOrdered, unchangeable sequence(1, 2, 3)
rangeNoImmutable sequence of numbersrange(5)
dictYesKey-value pairs{"name": "John", "age": 30}
setYesUnordered, unique elements{1, 2, 3}
frozensetNoImmutable setfrozenset({1, 2, 3})

Binary Types

TypeMutableDescriptionExample
bytesNoImmutable sequence of bytesb'hello'
bytearrayYesMutable sequence of bytesbytearray(5)
memoryviewN/AMemory view of an objectmemoryview(bytes(5))

Getting the Data Type

You can use the type() function to get the type of a variable:

x = 3.14
print(type(x))  # <class 'float'>

Setting a specific Data Type

You can use the following constructor functions to set a specific data type:

x = int(3.14)  # 3
y = float(5)   # 5.0
z = complex(2, 3)  # (2+3j)