Overview: Data types


This is a short overview to data types in Python. For more information, please refer to The Coder's Apprentice Chapter 3.

Overview

What is a data type?

  • A classification identifying a type of data.
  • Each data type specifies what kind of data is stored within it.

Built-in data types

  • In most programming languages, there is a set of built-in data types. The built-in data types for Python that we commonly use are:
    • int — used for integer numbers
    • float — used for decimal (or floating point) numbers
    • str — used for strings (a sequence of characters)
    • bool — used to hold either True or False (boolean)
    • list — used to store multiple values of other data types in an ordered sequence
    • set — used to store multiple values of other data types in an unordered sequence
    • dict — used to store information in key-value pair (dictionary)

String and string literal

  • String = a sequence of characters
    For example, "Hello World" is a string
  • String literal = a value/string that appears in a program, the actual value of the thing it represents
    For example, "Hello World" in print("Hello World")
    Note: string literal must be surrounded by quotation marks
  • Quotation marks used
      print("Python is awesome!")
      print('Python is awesome!')
      print('"Python is awesome!"')
      print('Python\'s awesome!')   

Immutable data type

  • An immutable data type is a type of a variable (or an object) whose value cannot be changed after its creation.
  • int, float, and str are immutable data types
  • A variable with this type can be reassigned but a reference from a variable is going to point to somewhere else in memory.

Mutable data type

  • A mutable data type is a type of a variable (or an object) whose value can be changed after its creation.
  • list, set, and dict are mutable data type.
  • A variable with this type can be reassigned. A reference from a variable still points the same place in memory