Introduction to Python for Engineers

Python is a high-level and general-purpose programming language. According to several survey results or search engine queries such as the TIOBE index, it is one of most popular programming languages. Part of the reason that it is a popular choice for scientists and engineers is the language versatility, online community of users, and powerful analysis packages such as Numpy and Scipy. This course uses the latest release of Python that is currently at version 3.9.

There are several resources for learning Python online. For a good start, look at Code Academy (Python track) or view the documentation at Python.org. For users with a foundation in MATLAB, there are excellent resources that demonstrate equivalent Python (Numpy) commands.

Install Python on Windows

Install Python on MacOS

Install Python on Linux

Online Platforms

There are several online platforms that allow Python through a web-browser including Try Jupyter and Google Colab.

Install Packages with pip (Command Line)

Sometimes a script uses a package that is not yet installed. Once Python is installed, a package manager such as pip or conda can be used to install, remove, or update packages.

Below is an example on how to install GEKKO Optimization Suite from the command line (start cmd).

 pip install gekko

The GEKKO package name can be replaced with any available package name such as NumPy.

 pip install numpy

Install Packages with pip (Python Script)

Packages can also be managed from a Python script by attempting to load the package with try. If the import fails, the except section imports pip and installs the package.

Load and Optionally Install GEKKO Package

module='gekko'
try:
    from pip import main as pipmain
except:
    from pip._internal import main as pipmain
pipmain(['install',module])

Load and Optionally Install NumPy Package

module='numpy'
try:
    from pip import main as pipmain
except:
    from pip._internal import main as pipmain
pipmain(['install',module])

The GEKKO or NumPy package names can be replaced with any available package. The pip package manager connects to an online repository to retrieve the latest version of the package and install it. Sometimes a package is needed on a computer that isn't connected to the internet or the package isn't available through pip. Below is information on installing a package wheel (whl) file.

Install Package Wheels (whl) Offline

The pip package manager can also be used to install local (previously downloaded) wheel (.whl) files but dependencies may not be automatically installed if not connected to the internet. Below is an example wheel file installation for NumPy version 1.13.1 and SciPy version 0.19.1 for Python 3.6 with 64-bit Python.

 pip install numpy-1.13.1+mkl-cp36-cp36m-win_amd64.whl
 pip install scipy-0.19.1-cp36-cp36m-win_amd64.whl

Use Christoph Gohlke's whl files for Windows installations. Many of the packages depend on the Visual C++ 2015 Redistributable (x64 and x86 for CPython 3.5 and 3.6) that is available from Microsoft. After installing the Visual C++ redistributable, download and install NumPy and SciPy packages (in that order) for Python 3.6 on Windows. The downloaded wheel file names should not be changed because the wheel file name verifies compatibility with the current Python version.

This same tutorial is also show in a MATLAB introduction.

Non-Ideal Gas Equation

Source Code

R = 0.0821  # L-atm/K
T = 500     # K
V = 5       # L/mol
Pc = 37.2   # atm
Tc = 132.5  # K

a = 0.427 * pow(R,2) * pow(Tc,2.5) / Pc
b = 0.0866 * R * Tc / Pc

# Compute in atm
P_ig = R * T / V
P_rk = R * T / (V-b) - a/(V*(V+b)*pow(T,0.5))

# Convert to Pascals
P_ig = P_ig * 101325
P_rk = P_rk * 101325

print("The ideal gas pressure: " + str(P_ig) + " Pa")
print("The Redlich-Kwong pressure: " + str(P_rk) + " Pa")