(An invaluable Python tutorial is available here)
(A rich style guide published by the Python Foundation is here)
Python is a script language (which means it is interpreted directly and not compiled into machine code). It can be used in many environments and has a wide variety of uses. Python scripts are used in software development and system maintenance. One of the things that sets Python apart is the rich collection of libraries that have been developed for it - everything from motion detection to mathematics and more.
Before you install Python, it would be best the read this entire section. There is a "quick and dirty" way to install Python, which will allow you to learn the basics. But, if you want to go beyond the basics, (e.g., using math or machine learning tools) you will need to install additional libraries and there are tools for avoiding conflicts between the added elements.
To run Python on a Mac, go to www.python.org/downloads and download the latest installation package. Python.app and IDLE.app will be installed. (IDLE is short for Integrated Development and Learning Environment.). Once Python is installed on the Mac, open a terminal window and type "python3" and the Python prompt ">>>" will appear. At this prompt, you can directly type Python commands.
There are tools for adding components (features) to Python's core. As I write this, I am not an expert in those tools, however, I have discovered PIP (Preferred Installer Program) and ActiveState, and am exploring the pros and cons. Mixing these tools does not work. You want to find a provider and stick with it. [This is subject to revision as I gain experience.]
Currently, I am using ActiveState. They offer a platform where you can select components that you need to install along with the core Python modules, resolve potential conflicts in those packages, and proceed to upgrade your installation. For example, I tried to install TensorFlow (machine learning software) following the instructions on TensorFlow's website. However, after the TensorFlow installation, there were still missing components, indicated by "ModuleNotFoundError". (The missing modules have names like "google", "protobuf", and "typing_extensions".) As I write this, ActiveState's engineering team is working on putting together a complete package that will work.
Alternatively, I could have used PIP. This seems to be the most commonly used installation tool. However, PIP does not come with customer service. I am hoping to get the added help that ActiveState supplies.
Regardless of who provides the best installation service, the important thing to know is that Python is not simply a "plug and play" language when you use it for functionality like testing other software packages. Part of learning Python is learning how to install it.
The print command and syntax is as follows:
The comment marker in Python is the # sign, as in the following comment:
# This is a comment and will not be executed.
Python allows you to declare and initiate the values of variables quickly. You don't have to declare variables before you use them. You simply set a new variable to a value and it dimensions it appropriately. See the example under "String Data Type" above. In that example, there is no need to "dim" the variable in advance.
There are some restrictions and specifications for variable names in Python:
Suppose you want update the value of a variable. Variables can be self-referencing as follows:
count = 1
count = count + 1
(Python will now print: 2)
Different types of data use different amounts of computer memory. Here are the data types in Python:
You can use the type() function to find out what data type a variable is. For example, let's say you previously set var = (1,2). The following line will return <class 'tuple'>
Python has the following numerical data types:
If you need to convert a number into a particular data type, you can use the following functions:
Python can perform math calculations. The syntax is straightforward, using + for addition, - for subtraction, * for multiplication, and / for division. Note that there is no need to put quotation marks ("..." or '...') around math expression. An example of a math calculation follows:
print(10474 * 1.5)
This will evaluate to 15726.0. Note that commas should not be included (they will be evaluated as spaces) and answers will have as many decimals as the figures in the expression do (except when accuracy will increase with more decimals.)
More math operarators are: // for integer division, % for remainder of a division, and ** for exponentiation. Note that you cannot use the frequently used symbol ^ for exponentiation in Python, as it produces a different result. Here are examples of how these work:
Setting a variable equal to a string (marked by ' ') both declares the string variable and initiates its value, as in the following example:
name = 'Frank Lloyd Wright'
To find the length of a string, use the len(). If the string variable name was holding the string "Frank," the following syntax...
...will return "5"
Referring to a "substring" is easy. The characters in a string are indexed starting with 0 (zero) on the left, and you can refer to a character with square brackets. For example, if the string variable name was holding the string "Frank," the following syntax...
...will return "r"
(You don't actually need to use the print() function. If you simply type "name" and press enter, you'll receive the same response.
You can also refer to characters using negative numbers from the right. For example, if the string variable name was holding the string "Frank," the following syntax...
...will return "n"
You can select multiple characters with square brackets. For example, if the string variable name was holding the string "Frank," the following syntax...
...will return "ra"
Note that the first number "1" is the index of the first leter selected and "3" is the index of the first letter that will be sliced off.
Note that spaces between words are also characters and are indexed. For example, if the string variable name was holding the string "Frank Long," the index of the space between "Frank" and "Long" would be 5.
You can concatenate strings by using the "+" sign. For example, if the variable first_name was set to "Frank" and the variable last_name was set to "Long" the following syntax would return "Frank Long":
print(first_name + " " + last_name)
The Boolean data type is often used for "directing traffic." For example, a script can direct the computer to perform a function as long as an equation evaluates as TRUE, and to diverge from or stop a function when a condition evaluates as FALSE. The logical comparison operators that might be used with Booleans are as follows:
For example, the following statement will evaluate to True.
print(1 == 1) [or simply 1 == 1]For another example, the following statement will evaluate to False.
1 == 2
Note that when comparing strings, the strings must be enclosed in single (or double) quotation marks, and the case of every letter matters. The following command...
text == text
...will return an error because it missing quotation marks around the strings. And this command...
"Text" == "text"
...will evaluate as False because the case of every letter is not the same.