Data type in Python

Python is a dynamically typed programming language. This simply implies that we don't need to explicitly indicate the data type assigned to a variable. In other words, the data type is automatically declared when you assign a value to the variable.

The Python interpreter determines the type of value based on what is assigned to the variable. If we want to check the data type of the value assigned to a variable, we can use the function type()

Let's use the type() function to find the data type of the value assigned to the variable as follows:

b = 24
type(b) #<class 'int'>

In our case, the data type of the value assigned to the variable is an integer. In other words, the data type of the variable b is of type int(integer).

Let's represent the data types in tabular form for better understanding.

                Datatypes                            Classes                   Narration
Number data types  Int, float, complex Number types represent numeric values.
Text data types str  This represents a sequence of characters enclosed in a quote(" " or ' '). 
Sequence data types list, tuple, range This holds a collection of items.
None type Nonetype Indicates the absence of a value.
Boolean type bool Holds True and False values
Set type set, frozenset Describe a set of unique value
Mapping types dict Holds key-value pair
Binary type bytes It represents a sequence of bytes

Python Number data type

The Python number data type is categorized as numbers. The numbers in Python fall into three types namely int, float, and complex.

int: The int are integer numbers like 1, 2, -12

float: The floats are decimal numbers like 1.4, 1.0, etc

complex: These are complex numbers.

int_number = 5 #int value
float_number = 20.2 #float value
complex_number = 3.20j #complex value

According to the code above, the numbers 5, 20.2, and 3.20j are assigned to the variables int_number, float_number, and complex_number respectively.  int, float, and complex are the data types of the variables. Let's use the type function to confirm their classes.

type(int_number) #<class 'int'>
type(float_number) #<class 'float'>
type(complex_number) #<class 'complex'>

Python Text data type

The text data type is basically the string data type. Strings are identified as a sequence of characters that are enclosed in a pair of quotes. Python allows strings to be enclosed by either pair of single or double quotes. Strings are immutable sequence data types. In other words, strings cannot be changed after creation. Any new change creates a completely new string object.

Let's represent a string using an example.

my_string = "hello world"
type(my_string) # <class'str'>
print(my_string) # Print out the whole string, hello world
print(my_string[0]) # print the first letter of the string, h
print(my_string[0:3]) # print the letters 'hel'

Python Sequence data type

The sequence types include lists, tuples, and range data types. 

  • List: A list is a collection of items separated by a comma and enclosed within a square bracket []. Generally, a list can contain items of different data types. Lists are ordered and mutable collections of items.
  • Tuples: A tuple is an ordered collection of items separated by a comma and enclosed within a parenthesis. Tuples are immutable, which means they cannot be updated. 
  • Range: Range represents an immutable sequence of numbers in a specified range.

 Let's represent them with examples.

list1 = [10, "ten", True, 20.2, 50]  # A list with different data type.
tuple1 = ("name", 21, "boy") # A tuple with a different data type.
range1 = range(5) # The range of number from 0 to 5(5 exclusive)

The list1 variable is a list that contains different data types. The list contains a number, string, and boolean data type.

The tuple1 variable is a tuple that contains different data types. The tuple contains a string and a number data type.

The range1 variable is a range() function that enumerates the sequence of numbers 0, 1, 2, 3, and 4.

None type

The none type is basically the None keyword. None is used to define a null variable or an object. The None implies the absence of a value, used as a placeholder or default.

For example

a = None
type(a)
<class 'Nonetype'>

The None keyword is assigned to the variable a. The type of the variable a is NoneType.

Boolean type

The boolean data type consists of True and False, used in logical operations and control flow. They belong to the class bool

a = True
b = False
type(a)
<class 'bool'>
type(b)
<class 'bool'>

Set type

Set is an unordered collection of unique items. The items in a set is enclosed in a pair of curly bracket, {}. The set types are of two types namely set and Frozen set

set: The set is mutable and has a unique element. 

fruit = {"apple", "orange", "banana", "pineapple", "apple"}
print(fruit) #{"orange", "apple", "banana"}

The duplicate of the items in the set is removed because the set consists of unique elements. 

Frozen set : Frozen set is an immutable version of the set.

b = frozenset('abcdefgh')
print(b) #frozenset({'e', 'b', 'h', 'a', 'g', 'd', 'c', 'f'})

Mapping type

The mapping type is basically the dictionary data type. The dictionary consists of a key-value pair. The keys are unique and are used to access the values. The values can also be accessed by square bracket []. 

dict = {"name": "Micheal", "age": 30, "School": "Oxford"}
print(dict["age"]) # 30
print(dict.keys()) #Output the keys

Binary type

bytes: It represents a sequence of bytes (immutable).

For example

a = b"hello world"
type(a) #<class 'bytes'>

Summary

The Python data type consists of the number, text, sequence, set, mapping, None, boolean, and the binary data type. Data type is a classification that indicates the type of data to be assigned to a variable.