Built-in Data Types in Python with Example

In this tutorial, we will learn about the data types in Python with the help of examples.

A data type in Python defines a specific type of value that a variable can store.

In other words, the type of data value we can store in an identifier such as a variable, is called its data type.

In the Python language, every value has a data type and everything is an object, meaning that Python represents all its data or value as objects. So, every value in Python is an object with a data type.

For example, if we store a value 50 to a variable num, then in Python, we will write as:

num = 50

Here, the object is 20 and its type is int, known as data type.

Internally, a variable in a program represents a memory location where data value stores. Data is an information that we store in computer programs.

For example, name, height, age, employee’s id, person’s birth year, the color of your hair, how many siblings you have, where you live, whether you’re male or female, numbers, etc. All these things are data or value.

Variables can store data of different types, such as an integer, floating-point, a string or boolean value, and these different types can perform different things.

To store these data, we require different data types depending on the needs of the application, because different types of data cannot occupy the same space in the computer’s memory.

So, the data type in Python determines how much memory will allocate for the data stored in a variable.

What is Dynamic data type in Python?

In other programming languages like Java, we require to specify the type of data a variable will store. It is a static data type.

But, Python does not impose any restriction in this context because in Python, data types are dynamic. This means that we don’t need to define the data type of variable explicitly.

The data type of a variable is determined in accordance with the value assigned to the variable. The change in value leads to the change in the variable’s type.

Thus, the same variable can hold the values of different data types at different times during the execution of a program. Let’s understand it clearly with the help of examples.

my_var = "Hi Friends!" # holding string.
my_var = 25 # same variable holding numeric value.
my_var = True # same variable holding boolean value.

As you can observe, we have used the same variable to hold a variety of different values, such as integer, string, and boolean value. Python automatically handles different types of values and automatically converts values from one type to another.

Other languages like C/C++/Java will not allow to define a generic variable of an unspecified data type. Therefore, such languages are known as “strongly typed languages”.

Types of Built-in Data types in Python

Like any other programming language, Python language allows us to work with the eight built-in standard data types. A list of all core data types are as follows:

  • Number
  • Boolean
  • None
  • String
  • List
  • Tuple
  • Dictionary
  • Set

Types of built-in data types in Python

These data types are further categorized into fundamental, sequence, and set data types.

  • Fundamental data types or native data types: int, float, and complex.
  • Sequence data types: string, list, and tuple.
  • Mapping data type: dictionary

Number Data types in Python

Number represents the numeric data or value (also called numeric literal) to perform mathematical operations. In Python, the number data types stores numeric values, such as integers, floating-point number, and complex number.

For example, we can represent a person’s height as a number. Numbers look like this: 1, 20, 100.50, 22.8990, etc.

When we assign a value to a variable, the Python interpreter automatically creates a number object. The general syntax to assign a value to a number object is as:

<numeric literal name> = <numeric literal value>

Consider the following examples below.

num1 = 10
percentage = 80.50

From the above assignment,

  • By default, Python will treat num1 as an integer because we have assigned one integer value to a variable num1.
  • Python will treat percentage as float because we have assigned one float value to a variable percentage.

Python supports three fundamental numerical data types. They are:

  • int (signed integer values)
  • float (floating point real values)
  • complex (complex numbers)

All three fundamental data types have been defined as int, float, and complex class in Python. Let’s understand each type with the help of example programs.

Int Data type

A Python object of int data type represents an integer number (without any decimal point or fractional part). It can be a positive, negative, or zero. The range of numbers is -2147483648 to 2147483647.

Let’s create a Python program in which we will create a variable and assign an integer value.

num1 = 10
num2 = 20
num3 = 30

Let’s take an example in which we will add two integer variables and assign the result of them in an integer variable.

num1 = 100
num2 = 200
result = num1 + num2

Float Data type

A float data type can store the floating type number. A floating-point number is a real number that contains a decimal point. For example: 0.59, -2.4125, 555.789, etc. are the floating-point numbers. It can be accurate up to 15 decimal places.

Let’s create a program in which we will create a variable and assign a float value to it.

num1 = 10.555
num2 = 20.99
result = num1 + num2

In this example, num1 and num2 are float type variables because they store floating type values. We can also write floating-point numbers in scientific notation using e or E that represents exponential having a power of 10.

For example, we can write the number 3.5 * 10^5 as 3.5E5. Let’s take an example based on it.

num1 = 1.20E5
num2 = 2.30E5
result = num1 + num2

Generally, we use scientific notation to represent huge numbers in very less memory space.

Complex Data type

A complex number is a number that we write in the form of “a + bj”. Here, a stands for the real part of a number and b represents the imaginary part of a number.

The lower case letter j after b refers to the square root of -1. The part a and b may have integer or float numbers. For example, 5 + 5j, 2.5 + 4j are the complex numbers. Let’s take an example program of it.

num1 = 2 + 5j
num2 = 3.5 + 7.5j
result = num1 + num2

In the above example, the Python interpreter takes the data type of variable result as a complex type.

How to determine the type of variable in Python?

Python provides a function named type() to determine which class a variable or a value belongs to. Let’s take an example based on it.

num1 = 50
num2 = 20.50
num3 = 3.5 + 7.5j

# Checking an object belongs to a particular class.
print(isinstance(num3, complex))
      <class 'int'>
      <class 'float'>
      <class 'complex'>

We can also delete the reference to a number object by using the del command. The general syntax is as:

del var_name
del var1[, var2[, var3[, . . . . . .]]]
del (object, . . . . )

For example:

num1 = 20
num2 = 50
del num1
      File "C:\Python Project\EscapeSequence.py", line 4, in <module>
      NameError: name 'num1' is not defined.

In this example, since we have deleted the variable num1 using the ‘del’ command, this variable does not exist anymore when we tried to print it.

Boolean Data type

Python provides two built-in Boolean data types: True or False, which internally represents 1 or 0 in a numeric context. The two values True and False are only boolean objects. Boolean data type is defined as a bool class in Python.

We use boolean values in comparisons, conditional expressions, and in structures where need truth or falsity. Let’s see how boolean data type works with the help of examples.

a = (10 >= 4)
b = (25 == 5 * 5)
c = (18 != 2 * 9)
# Use the print function to see values stored in each variable.
print(a, b, c)
      True True False

Here, we have created three variables a, b, and c that will store boolean values resulting from the assigned expressions. Let’s take one more example based on boolean data type.

a = True
b = True + 5 # here, the value of True is 1.
c = False * False
# Use the print function to see values stored in each variable.
print(a, b, c)

# Use the type() function to see which class a variable belongs to.
      True 6 0
     <class 'bool'>

None Data type

None is a special data type that represents an object containing no any value, means empty. In Java, it is a null object. But, in Python, it is None object. In boolean expression, None data type represents false.

None data type is defined as NoneType class in Python. The use of None object is that it is used inside a function as the default value of arguments.

While calling a function, if we do not pass any value to the function, the default value will be taken as None. If we pass some value to the function, then that value is used by the function. Let’s take a very simple example of it.

a = None
     <class 'NoneType'>

In this example, we have assigned a None value to a variable a and printed the result (as None) using print() function. Moreover, we have used type() function to print the class of variable a.

Sequence Data type in Python

Sequence data type represents a group or collection of elements or items. For example, a group of integer numbers, a group of strings, etc.

A variable of sequence data type may have different values of the same data type or values of different data types. There are basically eight types of sequences in Python that are as:

  • String
  • List
  • Tuple
  • Set
  • Dictionary

Let’s understand each data type one by one in detail with examples.

String Data type in Python

String is the most popular and useful data types in Python. A string literal is a group or sequence of characters. It is immutable in nature.

In Python, strings are created using single, or double quotation marks. For example, “Rahul”, ‘age’, ‘1234’, etc. The general syntax to create a string in Python is as:

<name of string> = '<string value>'

For example:

str1 = "Hello Python"
str2 = 'Happy birthday to you'
      Hello Python
      Happy birthday to you
      <class 'str'>

Here, we have defined two strings with name str1 and str2 and assigned string values to them, one by double quotes, and other one by single quotes. Python will treat both variables as strings.

Note that when you begin a value with double quotation marks, then you must end with it with double quotation marks. The following declaration is wrong.

str = 'Hello Bob"

In Python, there is also possible to create a string using a pair of triple single quotes (‘ ‘ ‘) or a pair of triple double quotes (” ” “) without using escape character. When we want the string in multiple lines, including spaces, we use this way. For example:

str = """Welcome to Scientech Easy,
           Dhanbad, Jharkhand, India."""

List Data type

List is the most versatile and flexible data types of Python language. It is defined as a class list in Python. List is an ordered sequence of heterogeneous elements (i.e. does not need to be of the same data type). It is similar to an array in C, C++, or Java.

The major difference between a list and an array is that an array is a group of homogeneous elements (i.e. of same data type) while the list is a collection of elements of heterogeneous or different data types.

To create a list in Python, the general syntax is as:

<list_name> = [<value1>, <value2>, <value3>, . . . . ]

In the above syntax, the list has represented by square brackets and elements in the list has separated by commas. Let’s take an example based on the list data type.

list = [10, 20.50, "Python", True]
      [10, 20.5, 'Python', True]
      <class 'list'>

Here, we have created a list named list that contains four elements, such as 10, 20.50, “Python”, and True. We can add or remove elements from the list even at runtime because the list is mutable in nature, meaning we can update elements in the list. For example:

num_list = [10, 20, 30, 40]
num_list[2] = 50 # updating element.
      [10, 20, 30, 40]
      [10, 20, 50, 40]

Tuple Data type

Tuple data type is the same as list data type. It has been defined as a class tuple in Python. A tuple is an ordered sequence of heterogenous elements separated by commas.

The only difference between tuple and list is that a list is mutable while tuple is immutable, i.e. tuples once created, we cannot modify elements inside the tuple. It is used to protect the data and also faster than list.

We create the tuple in Python using the parentheses ( ) and separate elements in the tuple using commas. An example of it is as:

t = (10, 20, "Python", 2 + 10j)
      (10, 20, 'Python', (2+10j))
      <class 'tuple'>

Here, we have created a tuple named t that contains four elements.

Set Data type

A set is an unordered collection of unique heterogeneous elements separated by commas inside curly braces ({ }). It does not maintain the order of elements in the set. It means that elements may not appear in the same order as they have entered.

Moreover, the set does not contain duplicate elements. The set data type is defined as a class set in Python. The syntax to create a set in Python is as:

s = {1, 2, 'Hello', 4 + 50j}
      {1, 2, (4+50j), 'Hello'}
      <class 'set'>

The set data type is mutable in nature, meaning we can update the elements inside the set even at runtime.

Dictionary Data type

Dictionary is an unordered collection of elements in the form of key-value pairs. It is called mapping data type. It is commonly used to operate on a large amount of data.

The dictionary data type has been defined as a class ‘dict’ in Python. If the key is given, then we can retrieve the value associated with it.

We can create a dictionary using curly braces ({ }) in which the key-value pair is treated as an element and distinct elements are separated by commas. The colon (:) separates the key and value.

Let’s take an example in which we will create a dictionary named dict with three elements.

dict = {1 : 'Orange',
        2 : 'Apple',
        3 : 'Banana'}
      {1: 'Orange', 2: 'Apple', 3: 'Banana'}
      <class 'dict'>

In this example, the keys in the dictionary are 1, 2, 3, and values in the dictionary are ‘Orange’, ‘Apple’, and ‘Banana’. The dictionary data type is mutable in nature, meaning that we can update any value in the dictionary.

In this tutorial, we have elaborated on data types in Python through various examples. Basically, data type determines the amount of memory allocated to store the data in a variable.

Python supports different data types, such as numeric, basic, and sequence data types. Hope that you will have understood all the basic points about data types and practiced all example programs.
Thanks for reading!!!
Next ⇒ Global and Local variables in Python⇐ Prev Next ⇒

Please share your love