Python Data Types

Our interactions (inputs and outputs) with a program are treated in many programming languages as a stream of bytes or ‘data’ for simplicity. These data can be interpreted as representing values that we understand. Additionally, within a program, we process this data in various ways such as adding them up, sorting them or concatenating them. This data comes in different forms. Some examples include:

  • Your name – A simple sequence of alphabet characters
  • Your age – Usually a whole number
  • Amount of money in your pocket – Fractional (decimal)

A major part of understanding how to design and code programs is centered in understanding the types of data that we want to manipulate and how to manipulate that data.

data type is therefore a classification of data which tells the compiler or interpreter how the programmer intends to use the data. Python supports various types of data, including integer, list, character or string, and boolean, dictionary, etc

Scalar Types

  • int: Positive or negative whole numbers (without a fractional part) e.g. -10, 10, 456, 4654654.
  • float: Any real number with a floating-point representation in which a fractional component is denoted by a decimal symbol or scientific notation e.g. 1.23, 3.4556789e2.
  • complex: A number with a real and imaginary component represented as x + 2y.
  • bool: Data with one of two built-in values True or False. Notice that ‘T’ and ‘F’ are capital. true and false are not valid booleans and Python will throw an error for them.
  • None: The None represents the null object in Python. A None is returned by functions that don’t explicitly return a value.

Sequence Type

A sequence is an ordered collection of similar or different data types. Python has the following built-in sequence data types:

  • String: A string value is a collection of one or more characters put in single, double or triple quotes.
  • List: A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets.
  • Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses.

Mapping Type

Dictionary: A dictionary Dict() object is an unordered collection of data in a key:value pair form. A collection of such pairs is enclosed in curly brackets. For example: 

{1:"Steve", 2:"Bill", 3:"Ram", 4: "Farha"}

Set Types

  • set: Set is mutable, unordered collection of distinct hashable objects. The set is a Python implementation of the set in Mathematics. A set object has suitable methods to perform mathematical set operations like union, intersection, difference, etc.
  • frozenset: Frozenset is immutable version of set whose elements are added from other iterables.

Mutable and Immutable Types

Data objects of the above types are stored in a computer’s memory for processing. Some of these values can be modified during processing, but contents of others can’t be altered once they are created in the memory.

Numbersstrings, and Tuples are immutable, which means their contents can’t be altered after creation.

On the other hand, items in a List or Dictionary object can be modified. It is possible to add, delete, insert, and rearrange items in a list or dictionary. Hence, they are mutable objects.

In General

Data type is an important concept to know. Variables can store data of different types and different types can do different things. The data type of a value determines what operations can be performed on that value. For example, if the data type of a value is string, you can concatenate it with another string, but you cannot perform division on it. To perform devision, the data type must be a Number type.

You may also like...
[instagram-feed]