Data Types

Python is a dynamically typed language. It means that the type of a variable is determined by what type of value is assigned to it. To determine the type of a variable Python provides a function called type. Here is an interactive Python session that shows the dynamic type assignment:

>>> x = 3
>>> type (x)
<type 'int'>

>>> x = "Hello"
>>> type (x)
<type 'str'>

>>> x = True
>>> type (x)
<type 'bool'>

Even though Python is dynamically typed there are certain types that are built into the language. Python handles each type differently. Python also allows users to introduce and use their own types through the definition of classes. Here are some of the native types in Python.

Numbers

In Python, there are integers, floating point numbers, and complex numbers. Booleans represent truth values False and True. Booleans are treated as integers 0 and 1. Integers can be plain integers that are in the range -2147483648 to 2147483647 or long integers that can be any size but limited by the size of the memory of the system. Floating point numbers are of double precision. Complex numbers are just pairs of double precision numbers.

Strings

A string is an immutable list of 8-bit (1 byte) characters. There is no separate character type. A character is a string of one character.

Tuples

A tuple is an immutable collection of items enclosed by parentheses and separated by commas.

Lists

A list is a mutable collection of items enclosed by square brackets and separated by commas.

Sets

A set is a collection of immutable and unique items.

Dictionaries

A dictionary is a collection of (key, value) pairs. The keys in a dictionary must be unique but the values need not be.