an overview image

Data stored in computer's memory can be of many types, for example if we want to store a person's age  we might want to store it in integer form, if it is a name, a string data type will be more appropriate. Most programming languages offer builtin support for the most common data types, such as integers, strings , arrays, e.tc.

In Python, any value belongs to a particular data type. There is a builtin type for the common data types, as  demonstrated below:

  1. Integers (int): This data type is used to represent whole numbers, such as 5, 17, -123, etc. Integers in Python have no limit on their size and can be arbitrarily large or small.

  2. Floats (float): This data type is used to represent decimal numbers, such as 3.14, 0.5, -12.345. etc. Floats can also be expressed using scientific notation, such as 1.23e6 for 1.23 million.

  3. Booleans (bool): This data type represents the truth values True and False. Booleans are often used in conditional statements and loops to control the flow of a program.

  4. Strings (str): This data type is used to represent textual data, such as words, sentences, and paragraphs. Strings are enclosed in quotation marks(single, double or triple)

  5. Lists (list): This data type is used to represent a collection of values, which can be of different data types. Lists are enclosed in square brackets and individual values are separated by commas.

  6. Tuples (tuple): This data type is similar to lists, but they are immutable, meaning that once they are created, their values cannot be changed. Tuples are enclosed in parentheses and values are separated by commas.

  7. Sets (set): This data type is used to represent a collection of unique values. Sets are enclosed in curly braces and individual values are separated by commas.

  8. Dictionaries (dict): This data type is used to represent a collection of key-value pairs, where each key maps to a corresponding value. Dictionaries are enclosed in curly braces and each key-value pair is separated by a colon. 

We can use the built-in function type() to check the type of a value. Examples:

type(1)
//<class 'int'>
type(3.4)
//<class 'float'>
type("Hello")
//<class 'str'>
type([3, 5, 6])
//<class 'list'>

The isinstance() function is used to check explicitly whether a given value belongs to a particular type. The function takes two arguments, the value  and the  type(s). The second argument can be a single type object such as int str dict etc. or a tuple of types such as (int, str, dict).

The function  returns True if the value actually belongs to the given type or one of the given types ,otherwise,  False. Examples

Using single type object:

isinstance(3, int)
//True
isinstance([1,2,3], tuple)
//False
isinstance([1, 2, 3], list)
//True
isinstance("Hello", str)
//True
isinstance({1,2,3}, set)
//True
isinstance({"one":1, "two":2, "three":3}, set)
//False
isinstance({"one":1, "two":2, "three":3}, dict)
//True

Using multiple type objects:

isinstance(1, (float, str, int))
//True
isinstance(['Python', 'PHP'], (dict, list, str) )
//True
isinstance('Python', (int, list, set))
//False

Scalar Data Types vs Compound Data Types

In Python, scalar data types represent single values, while compound data types represent collections of values.

Scalar data types include:

  • Integers 
  • Floating-point numbers 
  • Booleans 
  • Complex numbers
  • Strings

Strings are grouped as scalar data type despite being made of sequence of characters because Python does not have any data type to represent single characters. A characters in Python  is represented as a string with just one value.

Compound data types include:

  • Lists
  • Tuples
  • Sets
  • Dictionaries 

The compound data types can be made of values from all other data types including their own types

 Example:

L = [1, ['a', 'b', 'c'], 2.5, 5, "Hello"]

Mutable vs Immutable Data Types

Immutable data types are those whose value cannot be changed after they are created. Any operation that appears to modify an immutable object actually creates a new object with the new value. Examples of immutable data types in Python include integers, floating-point numbers, Booleans, strings and tuples.

Example:

S = "Hello, "
S += "World!"

In the above example when we add the string "World!" to S, an entirely new string is created  which is then reassigned to variable S

Mutable data types, on the other hand, are those whose value can be changed once they are created. Any operation that modifies a mutable object changes the object itself, without creating a new object. Examples of mutable data types in Python include lists, dictionaries, and sets.

Example:

L = [1, 2, 3, 4, 5]
L += [6, 7, 8, 9, 10]

In the above case, the elements of the list, [6, 7, 8, 9, 10] ,are  just appended to the list L without creating a new list.

Type casting

Some values in Python can be manifested in more than one type, for example any integer can be represented as a string e.g integer  1 as a string is "1", the Boolean value True in integer form is 1 and the string "Hello" as a list is ['H','e','l','l','o'].   Type casting is where we explicitly turn a value from one type to another compatible type. 

There is a built-in function to match the built-in data types, for example the built-in function, int() can be used to turn compatible values to integers, the str() function will turn the value given to the string type, and all the other data types follows the same suit.

Examples:

int("34")
//34
int(False)
//0
int(True)
//1
int(6.7)
//6
float(6)
//6.0
float(True)
//1.0
str(True)
//'True'
str(7.9)
//'7.9'
str([1,2,3])
//'[1, 2, 3]'
list("Hello, World!")
//['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
tuple("Hello, World")
//('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd')

Using the type functions with argument being of the matching type returns the same value e.g:

int(2)
//2
float(4.0)
//4.0
str("Hello")
//'Hello'
list([1,2,3])
//[1, 2, 3]

Trying to use the functions with an unsupported type will raise a  TypeError e.g:

int([1, 2, 3])
//TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
list(1)
//TypeError: 'int' object is not iterable
dict([1, 2, 3])
//TypeError: cannot convert dictionary update sequence element #0 to a sequence

All values have a string representation. This means that any value in Python can be converted to a string using the built-in str() function.

Examples:

str(print)
//'<built-in function print>'
str(int)
//"<class 'int'>"