Table of Contents » Reference Materials : Data Types
Data Types
Python, a versatile and popular programming language, offers a variety of built-in data types that cater to different needs. At its core are simple, fundamental types like integers (int), floating-point numbers (float), and booleans (bool), which represent true/false values. Strings (str) are used for text, encapsulating characters in quotes. Python also features composite data types for organizing and storing collections of data. Lists (list) are mutable sequences, capable of storing a mix of data types, and are ideal for ordered collections that might change over time. Tuples (tuple) are similar to lists but are immutable, meaning once created, their contents can't be changed, which is useful for fixed data sets. Dictionaries (dict) store key-value pairs, offering a powerful way to organize data in a way that's easily retrievable by keys. Sets (set) are used for unordered collections of unique elements, useful in situations where uniqueness is a priority.
Category | Python Syntax | Description |
---|---|---|
Text | str | Strings are sequences of alphanumeric characters include the alphabetic letters A thru Z, both upper case and lowercase, as well as the digits 0 thru 9. Remember that when a numeric digit is within an alphanumeric string, it is not considered a number but rather a textual representation of that digit. Alphanumeric also includes other characters found on keyboards, such as @ $ # & * ( ) { } [ ] , = - _ . + ; ' /. Also, a blank space (created on the keybard by the spacebar) is considered alphanumeric. |
Numeric | int | Integers are whole (counting) numbers, which can be negative or positive. Examples: 1, -45, 2462864. In Python integers can be of any length, the only limitation is available memory in the computer or device. When reading a numeric value, the Python interpreter determines the number is an integer if it starts with a whole number, with no prefix and with no decimal values at the end. |
Numeric | float | Floating point numbers are numbers that include a decimal point and digits to the right of it. Examples: 4.0, 3.14, 0.003. In Python, we can also represent floats as values in scientific notation by adding an e with a positive or negative number after the e indicating the number of digits of notation. For example, 1.0e-5 equals 0.00001. |
Numeric | complex | A Complex Number is a combination of a Real Number and an Imaginary Number. Complex numbers are used in scientific, mathematical and other application. You can learn more about the mathematical basis of Complex Numbers here. In Python, we represent Complex Numbers in the form < real number > + < imaginary number > + j. Examples: 5+2j, -2+4j, 6+19j. |
Sequence | list |
Lists are data structures in Python that allow us to store multiple data elements together with one variable name. Lists can store values of different data types in the same structure (this is called heterogeneous). The elements in a list are ordered and mutable, which means they remain in the specified order and that the values can be changed at runtime. Also, lists allow duplicate values, they are indexed (first element is index [0], second element is [1], etc.). In Python, lists are objects that have methods we can use to work with the list and they can be nested, that is, a list can contain lists. Example:
|
Sequence | tuple |
Tuples are another data structure in Python that is nearly identical to the list data structure, in that it is an indexed sequence however, unlike the list, tuples are immutable. Once a tuple has been declared it cannot be changed. Tuples are very useful for data values that will not need to be changed. In fact, in some applications changing data is forbidden, so using a tuple in those instances adds a layer of confidence in the integrity of the data. Example:
|
Sequence | range |
Ranges in Python establish a sequence of numbers that, by default, start with zero and increments by one up to one less than a specified number. The general form of a range is range(start, stop, step), where start is the number to begin the range (default is zero), stop is the number that indicates the end of the range (it is not inclusive in the range values) and step is the increment for the range (default is one). Example:
|
Map | dict |
Dictionaries are collections of data values stored as key:value pairs. Dictionaries in Python are very similar to lists, however they are not indexed like lists, rather they are indexed by the keys of the key:value pairs. Keys in dictionaries must be unique, dictionaries cannot contain duplicate keys, similar to primary keys in databases. Example:
|
Set | set |
Sets in Python are unordered data structures whose elements must be unique, that is, it does not allow duplicate elements. Elements may be added or removed, but element values in the set cannot be modified (that is, they are immutable). Example:
|
Set | frozenset |
A frozenset in Python is a set that is unchangeable, that is, it is frozen once it is defined.
|
Boolean | bool | Boolean is a binary state that can only be one of two values, True or False. Boolean variables in Python can only hold one of those two values and conditions can only result in one of those two values. We use boolean in many scenarios in programming, such as repeating an action until some condition becomes True. We often associate boolean result values of True and False to Yes or No questions. |