Data Science and Computing with Python for Pilots and Flight Test Engineers
Data Types
Variables in a program are “containers” that can contain pieces of information. They can contain numbers, as we have seen so far, but they can also contain other data types. A data type is a collection or grouping of data.
Basic data types are integers, floating-point numbers, characters, and Booleans. Integers and floating-point numbers can come in different storage sizes in computer memory, which affects what range of numbers they can represent. Strings of characters are sequences of numbers, letters, and other symbols that represent the symbol and have no numerical value. Booleans can take only one of two values: true or false.
But there are other data types in Python, such as lists, tuples, dictionaries, and sets, which are iterable objects (i.e. you can get an iterator from them) and have additional functionality.
One can also create one’s own data structures using classes (see later), and many new data structures you will encounter in various libraries have been created this way.
Numbers
Below we show you integers, real numbers (called floating point numbers in programming), and strings. (Different types exist for these variables, depending on the number of bytes that are used in the computer to store them (such as int, long int, float, double, etc.), but discussing this is beyond the scope of this introduction, since in Python you do not need to indicate their type explicitly during variable declaration – the interpreter automatically infers the type from what the variable looks like.)
# Integers (whole numbers):
x = 4
# (do so by not including a decimal after the number)
# Floating Point Numbers (with decimals)
x = 5.4
x = 1.0e+3
# the last expression means 1000 (written as 1.0 * 10^3)
Strings
Strings are sequences of characters. They can consist of letters, numbers, and other characters and do not have a numerical value. Strings are enclosed either in single or double quotation marks.
# Strings (you can use single or double quotes to denote strings:
y = 'Car'
z = "Dog"
a = '2'
b = '10'
# variables do not have to have numerical values, they can be strings of characters instead.
Above, even variable \(a\) does not contain the value 2, but rather the symbol “2”. If you wanted to get a numerical value from it (e.g. to be used in a math equation), you would have to convert the character or string into an integer or floating-point number first. The is perhaps better explained with variable \(b\) above: it is the string consisting of the symbol ‘1’ and the symbol ‘0’. It is not interpreted as meaning the number 10. Again, you would have to convert it first from a string to an integer or floating point number.
Lists
We can also create lists of variables, as illustrated below, and then access the individual elements of that list by putting the item index in square brackets right after the list name. Lists are enclosed in square brackets, with their elements separated by commas.
# Lists are encloses in square brackets:
myfirstlist = [x, y, z]
print(myfirstlist)
print("First list entry is:", myfirstlist[0]) # Note: list count (first entry) starts at 0.
print("Second list entry is:", myfirstlist[1])
print("Third list entry is:", myfirstlist[2])
Lists allow a variety of operations via their attached methods. For instance, one can append an element to a list like so:
mysecondlist = [1, 2, 3, 4]
mysecondlist.append(5)
print(mysecondlist)
Lists are iterable objects, from which we can obtain an iterator, which steps through all the elements sequentially. More information about lists can be found in the Python tutorial.
NumPy Arrays
NumPy arrays (often called arrays in short) are not fundamental data types in Python, but are added by the NumPy mathematics library. We want to discuss NumPy arrays here anyway, to be able to highlight their similarities and differences to lists.
NumPy arrays have the advantage that you can do matrix operations with them (linear algebra) and other math operation on them, like averaging over entries. But they also have some constraints compared to lists. NumPy arrays can be multidimensional, but all lines or columns in one dimension must have the same length. All entries also have to be of the same data type. Finally, you cannot use list methods like “append” on NumPy arrays.
Below we give an example of the 2D NumPy array that corresponds to the matrix
$$ A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{pmatrix} $$
and compare how the same would be done, if this object was realized as a list of lists (each row of the outer one-dimensional list being a one-dimensional list itself).
First, notice that the second entry (“row”) of the list of lists “i_am_a_list” contains only the elements 4 and 5, whereas the first and second each have three elements. In lists, this is allowed. And the very last element is a string ‘cat’ and not an integer number like all the others. This would not be allowed in a NumPy array.
Notice also how the syntax is extracting an element slightly differs between NumPy arrays and list. Note that the index count starts at zero (not 1) for both NumPy arrays and lists.
import numpy as np # this line imports the Numpy module into the notebook and will be explained later.
#####
# Numpy array example:
i_am_an_array = np.array([[1,2,3], [4,5,6], [7,8,9]])
x = i_am_an_array[0,1]
print("Array entry:", x)
#####
# List example:
i_am_a_list = [[1,2,3], [4,5], [7,8,'cat']]
y = i_am_a_list[0][1]
print("List element:", y)
Dictionaries
Python also knows dictionaries (enclosed in curly braces), which contain key-value pairs. For more information on lists and dictionaries, see the section on data structures in the online Python tutorial. Each key can appear only once in a dictionary (multiple occurrences would overwrite previous ones).
# Dictionaries are enclosed in curly braces and contain key-value pairs:
myfirstdict = {
"author": "Ralph D. Kimberlin",
"title": "Flight Testing of Fixed-Wing Aircraft",
"year": 2003,
"publication": book
}
print(myfirstdict)
User-Defined Data Structures
The user can also construct their own, very complicated data structures, which contain variables of various data types and functions. These user-defined data structures are called classes, and they are extremely useful for so called object-oriented programming. They will be covered in the next lesson.