Introduction to Python for Mechanical and Aerospace Engineering

Understand why Python is a good choice for engineering subjects and applications.

Who is this course for?

What do you think of when you hear the word “programming?” For most of the general population, programming conjures up images of advanced mathematics, smelly geeks, and “hacking into the mainframe,” whatever that means. For engineers and engineering students, programming generally brings back memories of the triumphs and struggles of introductory to computer science courses.

These traditional computer science courses for engineering focus on the fundamentals of programming without demonstrating the wide array of practical applications for outside fields.

As a result, engineering students know (or, at least, learned) the principles of inheritance and object-oriented programming, but the courses fail to link these concepts to actual engineering applications. As a result, mechanical and aerospace engineering disciplines get pushed to MATLAB because the computational abilities of programming languages tend to not be highlighted. MATLAB has traditionally dominated the engineering space because it is viewed as a batteries-included software kit focused on functional programming. Everything in MATLAB is some sort of array, and it lends itself to engineering integration with its toolkits like Simulink and other add-ins. The downside of MATLAB is that it is proprietary software; the license is expensive to purchase, and it is more limited than Python for doing tasks besides calculating or data capturing.


About this Course

Each chapter of this course will introduce the problem or program you are trying to create, the body of the program as it is developed, and a conclusion or summary. This format should provide enough context to understand the general idea of the program and should allow you to pick through what you need to know to get started. Links to any third-party library official website will also be included in an appendix at the end of this course, which usually has a documentation section that will introduce the library and show examples. This documentation is beneficial in learning more about the specific library or troubleshooting pieces of code that will not cooperate.

About Python

Python is a general-purpose programming language that can be used to write code for both small and large projects. Companies like Dropbox, Instagram, Instacart, and Reddit, among others, use Python in some or all of their production code. The language has three major selling points: it is free, it has good code readability, and it has a batteries-included philosophy, meaning that it can be quickly downloaded and used. The majority of the time, Python code reads very similarly or almost exactly like normal English.

This helps turn your code from a pseudocode, where you get the general idea of what you want your program to do, to an executable code. Python is also highly regarded because of its wide-ranging standard library. A library, also called a module, is a folder of pre-written code that you can use and re-use. Using a Python module is like calling your favorite car guy/gal to help diagnose a problem with your car. You benefit from their expertise without having to do the hard work, like researching the problem, ordering parts, or diagnosing the problem. For a Python example, you can use the built-in operating system module os to help you move a file from your Documents folder to your Desktop folder. All you have to do is tell Python which files to move and to where, and it takes care of the rest. The standard library has modules to help manage files on your computer, calculate and manage dates and times, time how long it takes your code to run, help test your code, and many more.

Another benefit of Python is that it is open-source, which makes it easy to use others’ work to help you. While Python has a good standard library, it has an even better third-party library support and distribution system. A standard package management system called pip downloads libraries with one command: pip install library_name. (You do not need to worry about pip in this course, but it is a good thing to know about if you end up downloading Python to your machine!) You can take advantage of extensive numerical methods with the library Numpy (Numerical Python, pronounced numb-pie), and you do not have to worry about the inner workings of the programs you are using. Numpy has a guide [1] that converts MATLAB syntax to Python syntax for any MATLAB converts that may be reading.

A common saying is that, “If you feed someone to fish, they will eat for a day. If you teach them to fish, they will eat for a lifetime.” Learning how to program in Python is not teaching someone how to fish; it is teaching them how to decide which way of acquiring food is the best. Python might not always be the best tool to accomplish something, but it is almost always the second-best. And in this case, being second-best at a lot of things is definitely better than being the best in a very narrow field. Being a jack-of-all-trades means that Python can be used to record or fetch data, process it, display it, save it, publish it to a website, and email it, which makes it much more versatile than MATLAB.


What level of engineering is required?

This course assumes a college junior level understanding of mechanical and aerospace engineering, with examples that touch on thrust available and thrust required for an aircraft, dynamic pressure and how it changes with altitude and velocity, airfoils, orbital mechanics and orbital parameters, and mechanical properties of different aluminium alloys. Do not be scared if you do not fully understand all of these topics, we will help you learn! The

Python examples will hit home more clearly if you understand the engineering subject that is being used for the example. Showing these engineering examples takes the abstract philosophy of object-oriented programming and turns it into an actual project. Orbital parameters become a lot more clear when you can manipulate them and see how they change orbit characteristics, for example.

This course does not assume any level of programming experience. This section will show you the basic structure of Python, its default data types, and the rules of programming in Python. It is by no means a comprehensive tutorial on computer science principles or philosophy. There can be semantic differences between how fast a particular data type is compared to another data type, but we will not be making a big deal out of any of that. You are not here to make something run 0.00003 seconds faster. You are here to make cool engineering applications in Python. You will also generally try to follow Python best practices; there may be one or two coding practices that are “discouraged” but improve clarity and understanding or are easier to write.