Python data types

In the previous tutorial, we learned how to create variables in Python. But if you remember, we did not specify what type of data we will assign to a variable. So how does Python get to know it? Actually, Python is a dynamically typed programming language. So, the Python interpreter implicitly gets the type of the variable and reserves space in the memory according to it, and stores the value. This is the reason, we do not specify the type of a variable in Python.


Standard data types in Python

Python has five standard/built-in data types.

  1. Numeric
  2. Boolean
  3. Sequence
  4. Dictionary
  5. Set
Python Data Types

Numeric Data type

In Python, a numeric data type represents a numeric value. A numeric data type in Python can be of following three types.

  1. Integers
  2. Float
  3. Complex number

Let’s discuss each one by one.


1. Integers – An integer value represents a positive or negative whole number.

# A program to demonstrate integer data type in Python

var1 = 100
print('Type of var1: ', type(var1))

var2 = -987
print('Type of var2: ', type(var2))

Output:

Type of var1:  <class 'int'>
Type of var2:  <class 'int'>

Note: The type() is a built-in function in Python. It returns the type of the variable passed to it.


2. Float – It represents any positive or negative decimal value.

# A program to demonstrate Float numbers in Python

f1 = 10.35
print('Type of f1: ', type(f1))

f2 = -9.86
print('Type of f2: ', type(f2))

Output:

Type of f1:  <class 'float'>
Type of f2:  <class 'float'>

3. Complex – It represents a non-real number. A complex number has two parts, a real part, and an imaginary part. The part associated with j is called the imaginary part.

# A program to demonstrate complex numbers in Python

c1 = 1 + 2j
print('Type of c1: ',  type(c1))

c2 = -10 + 20j
print('Type of c2: ', type(c2))

Output:

Type of c1:  <class 'complex'>
Type of c2:  <class 'complex'>

Boolean data type

There are only two possible values which a Boolean data type can hold, True and False.

# A program to demonstrate Boolean data type in Python

a = True
print('Type of a: ', type(a))

b = False
print('Type of b: ', type(b))

Output:

Type of a:  <class 'bool'>
Type of b:  <class 'bool'>

Sequence Data Type in Python

In Python, a sequence is an ordered collection of similar or different types of data. The data inside a sequence can be of standard or user-defined type. There are three types of sequences in Python:

  1. String
  2. List
  3. Tuple

1. String

In Python, a string is a sequence of Unicode characters. A string can be created by putting Unicode characters inside single quotes or double-quotes. In Python, It is also possible to create a multi-line string using triple quotes.

Note: Like other programming languages, there is no char data type in Python. A single character in Python is a string of length one.

# A program to demonstrate string data type in Python

str1 = 'Programmers Portal'  # Using single quotes
print('A single quote string:')
print(str1)
print(type(str1))

str2 = "Programmers Portal"  # Using double quotes
print('\nA double quote string:')
print(str2)
print(type(str2))

str3 = '''Hi,   
Welcome to
Programmers Portal''' # A multi line string
print('\nA multi line string: ')
print(str3)
print(type(str3))

length_one_str = "H"  # A single character string
print('\nsingle char string: ')
print(length_one_str)
print(type(length_one_str))

Output:

A single quote string:
Programmers Portal
<class 'str'>

A double quote string:
Programmers Portal
<class 'str'>

A multi line string: 
Hi,   
Welcome to
Programmers Portal
<class 'str'>

single char string: 
H
<class 'str'>

2. List

A list in Python is a sequence of comma separated values. These values can be of any built-in or user-defined type.

A list can be created by placing all the list items inside a square bracket [] and separating each item by a comma.

# A program to demonstrate List data type in Python

empty_list = []  # Emtpy List
print('Empty List:')
print(empty_list)
print(type(empty_list))


int_list = [1, 2, 3] # List of integers
print('\nA list of integers: ')
print(int_list)
print(type(int_list))


mixed_list = [1, 'Programmers Portal', 2.13] # A list containing
print('\nA list of mixed data types:')       # mixed data types
print(mixed_list)
print(type(mixed_list))

Output:

Empty List:
[]
<class 'list'>

A list of integers: 
[1, 2, 3]
<class 'list'>

A list of mixed data types:
[1, 'Programmers Portal', 2.13]
<class 'list'>

3. Tuple

A tuple is similar to a List in Python. The only difference is that a tuple is immutable in nature. It means tuple elements can’t be changed after creating it.

A tuple is created by placing all tuple items inside parentheses () and each tuple item separated by a comma.

# A program to demonstrate Tuple data type in Python

empty_tuple = () # Empty Tuple
print('Empty Tuple: ')
print(empty_tuple)
print(type(empty_tuple))


t1 = (1, 2, 3) # A tuple of integers
print('\nA tuple of integers: ')
print(t1)
print(type(t1))

t2 = (1, 'Welcome to', 'Programmers Portal', 2.13) # A tuple containg
print('\nA tuple of mixed data types: ')           # mixed data type
print(t2)                                          
print(type(t2))

Output:

Empty Tuple: 
()
<class 'tuple'>

A tuple of integers: 
(1, 2, 3)
<class 'tuple'>

A tuple of mixed data types: 
(1, 'Welcome to', 'Programmers Portal', 2.13)
<class 'tuple'>

Dictionary Data Type in Python

In Python, a dictionary is a collection of unordered data items. Where each item consists of a key-value pair.

A dictionary is created by placing all the key-value pairs inside curly braces {} and each pair separated by a comma.

# A program to demonstrate Python dictionary

empty_dict = {}
print('Empty Dictionary: ')
print(empty_dict)
print(type(empty_dict))

my_dict = {
    'Name': 'Programmers Portal',
    'Topic': 'Python',
    'chapter': 5,
    'value':2.13
}
print('\nA dictionary containing multiple data types:')
print(my_dict)
print(type(my_dict))

Output:

Empty Dictionary: 
{}
<class 'dict'>

A dictionary containing multiple data types:
{'Name': 'Programmers Portal', 'Topic': 'Python', 'chapter': 5, 'value': 2.13}
<class 'dict'>

Set in Python

A set in Python is a collection of unordered data items. Where each item is unique in the set.

A set is mutable in nature. It means that set items can be changed after creating it. Set items can be of any built-in or user-defined data type.

A set is created by passing all the set items into a built-in method set().

# A program to demonstrate set data type in Python

empty_set = set() # Empty set
print('Empty set: ')
print(empty_set)
print(type(empty_set))

int_set = set([1, 2, 3, 4])  # A set of integers
print('\nA set of integers: ')
print(int_set)
print(type(int_set))

string_set = set('Programmers Portal') # A set of characters
print('\nA set of characters: ')
print(string_set)
print(type(string_set))

mixed_set = set([1, 'Hello', 2.13, 1])  # A set of
print('\nMixed set:')                   # Mixed data types
print(mixed_set)  
print(type(mixed_set))

Output:

Empty set: 
set()
<class 'set'>

A set of integers: 
{1, 2, 3, 4}
<class 'set'>

A set of characters: 
{' ', 'e', 'g', 't', 'a', 'P', 'm', 's', 'o', 'r', 'l'}
<class 'set'>

Mixed set:
{1, 2.13, 'Hello'}
<class 'set'>