Python Data Types Tutorial

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

Data types

Data types are the classification of data elements into specific types. A data container’s type determines the type of data that it can store, and the kind of operations that can be performed on that data.

For example, we can perform arithmetic on numerical types but when we do the same with a string type, the result will be different.

Example:
# adding numbers
print(3 + 4)

# adding strings
print("Hello" + " 3")

In the first example above, the operands are numbers so the + operator will perform the arithmetic and return the result of 7.

In the second example, both operands are strings so the + operator will concatenate them together.

Numbers and strings are different types so the operations we can perform on them are different.

Python Data type: Numbers

A number is any representation of data which has a numeric value. Python has three types of numbers:

  • Integer. An integer is a positive or negative whole number.
  • Float. A float is a number with a fractional part, in which the fractional component is denoted by a decimal symbol or scientific notation.
  • Complex. A complex is a number with real and imaginary parts.
Python Data type: Booleans

A boolean data type represents two truth values of logic, true and false.

Python has two booleans:

  • True. True also represents a numerical 1.
  • False. False also represents a numerical 0.

Boolean True and False are two of the three keywords in Python that are capitalized. If the boolean is written all in lowercase letters, the interpreter will raise an error.

Python Data type: Strings

Python has a range of compound types often referred to as sequences. A string is one of those sequences, or collections.

A string is one or more characters that form words and sentences. Strings are declared differently from other collections such as the list. Strings are wrapped in single, double or triple quotes.

Python Data type: Ordered Collections

An ordered collection is a set of one or more mutable or immutable data items, not necessarily of the same data type. Ordered collections are numerically indexed and ordered by the index.

Python has two types of ordered collections:

  • List. A list is an ordered collection with mutable data items.
  • Tuple. A tuple is an ordered collection with immutable data items.
Python Data type: Unordered Collections

An unordered collection is a set of one or more mutable or immutable data items, not necessarily of the same data type. Unordered collections are not numerically indexed or does not support indexing.

Python has two types of unordered collections:

  • Set. A set is unordered collection with immutable data items. A set has no index and does not support indexing.
  • Dictionary. A dictionary is an unordered collection of mutable data items, in custom key:value form.
Python Data type: Special

A special data type is a data type unique to the programming language with a special meaning.

Python has one special data type:

  • None. None represents the absence of a value.

The None type is the third of the three keywords in Python that are capitalized. If None is written in all lowercase, the interpreter will raise an error.

Mutable and Immutable Objects

When a Python application is running, it temporarily stores data in memory (RAM). The values of some data types can be modified in memory at runtime, others are not allowed to be modified once space is allocated for them in memory.

Mutable types are types that may be modified at runtime, such as a list or dictionary that can add, delete and rearrange items in their collections.

Immutable types are types that may not be modified at runtime, such as a tuple.

type() function

Python has a built-in function called type() that allows us to see of which data type a specific element is.

Example: check a type
# integer
print(type(5))

# float
print(type(3.14))

# complex
print(type(3+4j))

# string
print(type("Hello World"))

# list
print(type(["Monty", 3.14]))

# tuple
print(type(("Monty", 3.14)))

# set
print(type({"Monty", 3.14}))

# dictionary
print(type({"Name": "Monty", "Pi": 3.14}))

Summary: Points to remember

  • A data type is a classification of elements to specific types.
    • Python has three numeric data types. Integer, float and complex.
    • The boolean data type only has two values, True and False. True represents 1 and False represents 0.
    • The string data type is a collection of single characters to form words and are wrapped in single, double or triple quotes.
    • The list data type is an ordered collection with mutable data items.
    • The tuple data type is an ordered collection with immutable data items.
    • The set data type is unordered collection with immutable data items which does not support indexing.
    • The dictionary data type is an unordered collection of mutable data items in key:value form.
    • The None special data type represents the absence of a value.
  • The values of mutable items can be changed at runtime.
  • The values of immutable items cannot be changed at runtime.
  • We can see which type an item belongs to with the built-in type() function.