Data Types#

There are several different types of data that Python can work with in conjunction with operators. When Python puts data into a computer’s memory to store the data or operate on the data it creates what is called in computer science an object. Objects will get covered more in the next section, but for now just know that everything in Python is an object, whether it’s data we’ll work with or a function that will perform an operation. Not every programming language uses objects, but those that do are called object-oriented programming languages.

We’ve already seen one data type, integers, and will use other types such as floating point numbers, strings, lists, and dictionaries in this course. We’ll first focus on integers, floating point numbers, and strings.

Integers#

The set of all integers is described using the symbol \(\mathbb{Z}\) in mathematics. If an item, \(x\) is an element of this set of numbers, meaning \(x\) represents an integer, the notation for this would be \( x \in \mathbb{Z}\). In Python, the integer data type is called int.

Some examples of integers are:

10, -3, 0

In Python, unlike in other programming languages, there is no limit to the size of your integer. As long as your computer has enough memory to hold the integer, Python is happy to store and work with it.

Floating-Point Numbers#

In mathematics, the set of real numbers contain all non-imaginary numbers and are represented as the set \(\mathbb{R}\). In Python, real numbers are called floating-point numbers, and the data type is called float for short. float values are specified with a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.

Some examples of floats are:

17.1, -3.0, 1.7e-9

Strings#

Strings are sequences of characters stored together. A character can be a number, letter, or symbol. The string type in Python is called str.

Examples of strings include:

hello, Unbelievable!, Δ100

Strings may be delimited using either single or double quotes. It’s best practice to be consistent in your use of single or double quotes. All the characters between the opening quote and matching closing quote are part of the string.

You can use the built-in print() function to display the value of an object in Python. For a string object, that means displaying the sequence of characters contained within the object without the quotes.

print('This is a string')
This is a string

If you want your string of characters to contain a single quote, you need to delimit your string using double quotes.

print("This string has one single quote. It's pretty special.")
This string has one single quote. It's pretty special.

Using single quotes to create a string that also contains a single quote will result in an error, as Python will think that your string terminates at the first matching single quote, and be confused by the remainder of the instruction that does not make sense.

print('This string has one single quote. It's going to be a problem')
  File "<ipython-input-1-c99359b0e48a>", line 1
    print('This string has one single quote. It's going to be a problem')
                                                ^
SyntaxError: invalid syntax

You can similarly use single quotes to define a string containing double quotes.

print('This string has "unneccesary" double quotation "marks" in several "places"')
This string has "unneccesary" double quotation "marks" in several "places"

There are several other characters that have special meaning and may cause Python to invoke that special property if encountered in a string. You can indicate to Python to use the actual character, not the special meaning, by using a backslash ( \ ) in front of the character. The inclusion of a backslash to change the interpretation of an input is called an escape sequence.

print("This string has single quotes \', double quotes \", backslashes \\, and more!")
This string has single quotes ', double quotes ", backslashes \, and more!

Additionally, the backslash character can impart special meanings to normal characters contained in a strong. For example, including the characters \n in a string will cause it to create a new line in the printed output.

print('This string will be printed on \ntwo different lines.')
This string will be printed on 
two different lines.

A table of common escape sequences is included below:

Escape Sequence

Sequence Results in…

\n

line break

\t

tab

\uxxxx

Unicode character with 16-bit hex value xxxx

\Uxxxxxxxx

Unicode character with 32-bit hex value xxxxxxxx

\N{<name>}

Unicode character with provided name

For example:

print('This \ttext \twill be \nprinted on \u2192 multiple lines \n\U00000394\N{rightwards arrow}0')
This 	text 	will be 
printed on → multiple lines 
Δ→0

This unicode escape sequences are typically used to insert characters that are not easy (or possible) to create with a keyboard, not easily readable, or not printable to the screen.

Additional details on how to further use integers, floats, and strings, as well as additional data types, will be covered as the need arises in our work.

Extra Details on Floating Point Numbers#

There are a few details that 99% of the time you don’t need to worry about when working with floats in Python, but knowing this information may be helpful if you’re tracking down the cause of odd behavior in your code.

Under the surface, Python float values are 64-bit “double-precision” values, according to the IEEE 754 standard. Float values are very flexible, but they do have limits.

  1. A float can represent extremely large and extremely small numbers. There are limits, but you will rarely encounter them.

  2. A float only represents 15 or 16 significant digits for any number; the remaining precision is lost. This limited precision is enough for the vast majority of applications.

  3. After combining float values with arithmetic, the last few digits may be incorrect. Small rounding errors are often confusing when first encountered.

The maximum value a floating-point number can have is approximately \(1.8 \times 10^{308}\). Python will indicate a number greater than that by the string inf

1.7e308
1.7e+308
1.7e310
inf

The closest a nonzero number can be to zero is approximately \(5.0 \times 10^{-324}\). Anything closer to zero than that is effectively zero

4.9e-324
5e-324
4.9e-326
0.0

Floating point numbers are represented internally as binary (base-2) fractions. Most decimal fractions cannot be represented exactly as binary fractions, so in most cases the internal representation of a floating-point number is an approximation of the actual value. In practice, the difference between the actual value and the represented value is very small and should not usually cause significant problems. However, when working with very large or very small numbers or fractions, oddities can occur from time to time.

For example, adding \(\frac{1}{10}\) and \(\frac{7}{10}\) should result in \(\frac{8}{10}=0.8\), but as we see below, Python doesn’t quite agree due to the internal representation of these numbers.

1/10 + 7/10
0.7999999999999999

Although float values are not always exact, they are certainly reliable and work the same way across all different kinds of computers and programming languages.