Archangel Macsika Sikademy Image
Archangel Macsika

Learn About the Different Data Types in Python Programming

Tutorial 8 of 11 | 4 minutes read
Learn About the Different Data Types in Python Programming - Sikademy

Data Types in Python

A data type is an important concept not only in Python Programming but also in general programming.

Data stored in memory can be of different types and each performs distinct functions.

For example, the name of a person is stored as a different type, while the age is of a different type.

Some programming languages like Java, C, and C++ practice "type declaration" (explicitly stating the type of the variable before assigning a value).

In python programming, there's no type declaration. The type of the variable is automagically detected through the assigned value.

Standard Built-in Data Types in Python

There are different standard data types in python programming. These data types are built into python.

Python has the following data types built-in by default:

Data TypeFull NameUsesExample
strStringFor text and alphanumeric values.var = "Archangel Macsika"
intIntegerFor numeric values.var = 15
floatFloating-PointFor decimal values.var = 15.3
complexComplexFor complex numeric values.var = 9.314e-53j
listListFor values that are in sequence.var = ["Monday", "Tuesday", "Wednesday"]
tupleTupleFor values that are in sequence.var = ("Monday", "Tuesday", "Wednesday")
rangeRangeFor values that are in sequence.var = range(6)
dictDictionaryFor values that involve mapping.var = {"name" : "Archangel", "age" : 24}
setSetFor values in set format.var = {"Monday", "Tuesday", "Wednesday"}
frozensetFrozensetFor values in Set Formatvar = frozenset({"Monday", "Tuesday", "Wednesday"})
boolBooleanFor values in boolean format (True or False).var = True
bytesBytesFor binary type of values.var = b"Archangel"
bytearrayBytearrayFor binary type of values in array.var = bytearray(7)
memoryviewMemoryviewFor binary type of values.var = memoryview(bytes(7))

Getting the Data Type in Python

Using the type() function, you can get the data type of any value or object:

For example:



full_name = "Archangel Macsika"
print(type(x))

Output:

<class 'str'>

Data Type Conversion and Casting in Python

Sometimes, you may want to explicitly state the data type or convert from one data type to another. For instance, change a list data type to a set.

There are several built-in functions in python to help you achieve this. And after conversion, the object returned is the new converted object.

For example:



var = ["Monday", "Tuesday", "Wednesday"]
print(type(var))

var = set(var)
print(type(var))

Output:

In the output below, the list data type is the original data type of the var, whereas the set data type is the newly converted one.

<class 'list'>

<class 'set'>

Here are other data types and their functions used to either explicitly set or convert to another data type:

Data TypeFunctionExample
strstr(x)var = str("Archangel Macsika")
intint(x[,base])var = int(15)
floatfloat(x)var = float(15.3)
complexcomplex(real[,imag])var = Complex(9.314e-53j)
listlist(x)var = list(("Monday", "Tuesday", "Wednesday"))
tupletuple(x)var = tuple(("Monday", "Tuesday", "Wednesday"))
rangerange(x)var = range(6)
dictdict(x)var = dict("name" : "Archangel", "age" : 24)
setset(x)var = set(("Monday", "Tuesday", "Wednesday"))
setfrozenset(x)var = frozenset(("Monday", "Tuesday", "Wednesday"))
boolbool(x)var = bool(4)
bytesbytes(x)var = bytes(4)
bytearraybytearray(x)var = bytearray(7)
memoryviewmemoryview(x)var = memoryview(bytes(7))

Wrap Off

Data type is an important concept in Python Programming. They come in different types and perform different functions.

If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.

Tutorial Exercise

Find out what these data types are and how to use them: chr(), unichr, ord(), hex(), oct(), eval(), and repr().


Enjoy this Tutorial? Please don't forget to share.