Data Types in Python — Part 1

Antarip Giri
CodeX
Published in
6 min readAug 17, 2024
Generated by the author using fal.ai

In this tutorial, we will look into the various data types used in the Python programming language. We will look at code snippets of each data type.

Before we dive into the data type in Python, we will take a little detour and look at a small code snippet and understand how various variables are stored, memory allocation, and tags in Python which will help us understand datatypes better.

Memory allocation, Tags in Python

#Adding two numbers
""" This program has two numbers
and produces the sum and the difference of them"""
first_num = 10 # the first number and value
second_num = 15# the second number and value
sum = first_num +second_num #adding two numbers
print("Sum of two numbers:",sum) #printing the sum of two numbers
difference = second_num - first_num
print("difference of two numbers:",difference)

The code snippet shows that two variables “first_num” and “second_num” store two integer values. Now, unlike languages like C or JAVA where you need to declare the data type explicitly of the variable before initializing the value to the variable. In Python, “first_num = 10”, a variable is created and a value of 10 is stored. As the value 10 is an integer the Python interpreter understands the memory requirement for an integer value.

Similarly, if any other data type value is assigned to a variable python allocates the memory. The next thing to note is that the ‘=’ or ‘+’ are called assignment operators and the variable name is called identifier. These identifiers can be variable names, method names, class names or any objects. Any change in the value assigned to a particular variable will update the same initial allocated memory.

One of the major differences between Python and other languages in assigning values to identifiers is Python sees these identifiers as tags to a memory location where the values are stored. Essentially, python considers every value as an object (10,15 etc). The change in the value of “first_num” changes the tag from the value 10 to the new value(say 20). When the following code runs,

third_num = first_num

Instead of creating another memory location for the same “object” it just tags the same object with another identifier (“third_num”) in this case. When we change the value say,

first_num = 20

Python changes the tag to the new value and the old value becomes a non-referenced object and is collected in the garbage collector.

What are the different data types in Python?

Datatypes in Python can be broadly classified into two categories: ‘built-in datatypes’ and ‘user-defined datatypes’.

Built-in datatypes are pre-defined and are available in Python. User-defined datatypes are created and defined by the users. You can also check out my video tutorial for this part of the blog:

In this tutorial, we will talk about types of built-in datatype. User-defined datatype will be dealt with in later advanced tutorials.

Built-in Datatype can be further classified as follows:

Now let us look at each built-in data type in more detail.

Numeric Datatypes

As the name suggests, these datatypes are used for numbers and are further categorised into int, float, and complex.

int datatype

It represents all types of integer numbers, i.e. without a decimal point or fractional number. To be precise the datatype can store values ranging from -2147483648 to 2147483647. However, this range may be larger on larger machines (having a larger word size, cannot be smaller).

what happens when the number (result) lies outside this range: python returns the datatype long integer (sometimes raises an error, OverflowError).

num_int = -10

In the above code snippet the variable ‘num_int’ is called the int type as it stores an integer type variable.

float datatype

It represents floating point numbers i.e. numbers with decimal points. If we store a floating point number in a variable as shown below.

num_flt = -10.09

The variable num_flt will be represented as a float datatype. Python float datatype also includes scientific notations where ‘E’ or ‘e’ represents an exponent of 10. The code below:

exp_num = 45.67E-4

This becomes quite useful when representing small or large decimal point numbers. When larger numbers are stored this way it takes up far less memory.

Complex datatype

It is written in the form of A + BJ or A +Bj. ‘A’ is the real part and ‘B’ is the imaginary part with a suffix ‘j’ or ‘J’ which takes the value of the square root of -1. Both the real and the imaginary parts can contain integer or floating-point numbers.

comp_num = -5.67 - 56J

The code snippet above represents the variable ‘comp_num’ as a complex datatype.

What about Binary? Octal? Hexadecimal Numbers?

To represent these types of numbers we use prefixes.

num_bin = OB11101100
num_oct = 0O723
num_hex = 0XFF

The code snippet above represents the binary, octal and hexadecimal numbers respectively.

Boolean datatype

This datatype represents the boolean value i.e. True or False. However, internally Python represents True as 1 and False as 0.

#booelan datatype
check = True #check variable is bolean type
check_1 = False
num = 19
num_1 = 17
check_num = num > num_1 # True is stored in check_num
print(check_num + check) #outputs 2 (True(1) + True(1) = 2)
print(check_num - check_1) #outputs (True(1) - False (0) = 1)
print(check_num - check) #outputs (True(1) - True (1) = 0 )
print(check_num + num) # Outputs (True(1) + num(19)) = 20

The code snippet above shows various operations with a boolean datatype. We can use boolean literals, and can also use logical operators and the output of these operators which is a boolean can be directly stored in a variable. Further, we can use arithmetic operators on boolean variables and as Python internally stores boolean variables as integers (True: 1 and False : 0) arithmetic operators are valid. We can also exploit this fact to use arithmetic operations between a boolean and an integer.

Sequences

In the previous section, we talked about singular datatypes stored in variables. Now what about a group of data elements? Python makes it convenient to store such a group of elements which are similar datatypes or different datatypes.

‘str’ or string datatype

The ‘str’ or string datatype is a group of characters. strings are represented inside a single quotes or double quotes. For multi-line strings, we need to use triple-double or triple-single quotes. We can further use string slicing or string comprehension operations. let's look at the code snippet below.

#string datatype
str_1 = 'This is my blog' #string syntax
str_2 = "on Python Datatypes" #also a string syntax
str_multi = '''This is my blog site.
The sites is hosted in medium''' #single triple quotes for multi line
str_multi_1 = """This is my blog site.
The sites is hosted in medium"""
print(str_1)
print(str_multi)
print(str_1+' '+str_2) #This is my blog on Python Datatypes
#multiplication and addition operator
print((str_1+' ') * 4)# This is my blog This is my blog This is my blog This is my blog
#python indexing starts from 0 (the 1st letter is [0] index)
print(str_multi[1:3]) # the 2nd and the third letter: hi
print(str_multi[:11]) # all letter uptil 11 (excluding 12 th letter) : This is my
print(ord(str_1[0])+1) #gets the ASCII values of the characters (T = 84 +1 = 85)

The code above shows various ways of assigning strings to a variable, variable str slicing and comprehensions possible and ASCII value extraction and operations. The ‘ord()’ method is called to extract the ASCII value of a character The expected outputs are given as a comment alongside the line of code. Note the string concatenation and string multiplication operations shown above.

In the next tutorial, we will continue the discussion on remaining datatypes.

Also, Thank you for reading so far.💪 Have a small tea or coffee break.😃If you like👍 the blog do like and comment👀 if you need any specific blog post regarding this. Also, feel free to share this with your friends, colleagues or those who you may benefit from this tutorial.

--

--

Antarip Giri
CodeX
Writer for

A Machine Learning and Data Science practictioner who talks about all things data .