Data Science and Computing with Python for Pilots and Flight Test Engineers
Coding Basics
The code of a computer program consists of a series of instructions. In Python, each instruction belongs onto one line, and the line break indicates that the instruction has ended. Python does not use any special characters at the end of a statement, such a semicolon in C/C++.
Doing Mathematics in a Computer Program
Equal signs in a computer program are not equal signs in a mathematical sense. Instead, they are assignments. The variable on the left of the equal sign is assigned the value of the entire expression on the right. Below, the variable \(x\) is first assigned the value 3 and later the new value 5 (which overwrites the previous value).
x = 3
x = 5
This is why on the left, we can always only have one variable (something like \( x + 4 = 6\) would not be allowed – the computer does not have the ability to solve the equation and figure out that \(x = 2\) in the above example). Likewise, all variables on the right need to have numerical values if we are doing math, such that they compute to a number.
Keeping the above in mind, we can now increase the value of variable \(x\) by two with this line of code:
x = x + 2
Again, the computer does not try to solve the equation for \(x\). It is an assignment. The variable on the left is assigned the value of the mathematical expression on the right. From the previous cell, we have that \(x=5\). Thus, this is the value that \(x\) has, as it enters this expression on the right. Then 2 is added to 5, and the value 7 is obtained on the right. The above line, therefore, newly assigns the value 7 to variable \(x\).
Strings
The equal sign can be used for any kind of assignment, even if the expression on the right is not mathematical. A string is a number of consecutive characters, typically enclosed in single or double quotation marks. The following line of code assigns to the variable “vehicle” the string “Car” (i.e. the word spelled out).
vehicle = 'Car'
Output
We often want to output something during a program run, for instance to see, what the value of a variable is. We can do this with the following syntax:
print(x)
print("x = ", x)
print("The vehicle I possess is a ", vehicle, ".")
The first line simply prints the value of variable \(x\) without anything else. The second line prints “\(x=\)” first and then the value of variable \(x\) on one line. The second line prints a whole sentence, with the string contained in the variable “vehicle” as the last word in the sentence, before the period. You can use a comma as in the example above to separate different items you want to print on one line.
The above just prints on the screen, in the Jupyter notebook, below the code cell, where print() occurs. We will learn later how to output data into a file and how to save figures of our plots, images, and videos with Python.