jafner.com


Getting Started with Python

  1. Why Python for Mechanical Engineers?
  2. Setting Up Your System
  3. How to Run Code
  4. Basic Python
  5. Reading Data
  6. Plotting Data
  7. Automating Tasks
  8. Engineering Math in Python
  9. Hands-On Labs
  10. Additional Resources

Why Python for Mechanical Engineers?

Python is a great solution for non-software engineers to get things done. It has a robust library of tools and a supportive community that makes it easy to find solutions. It is fast (compared to most engineering tools) and flexible for data analysis, automation, and visualization.

It's similar to Matlab, but Python is free and open source.

It can overlap with Excel, but Python can handle large datasets and complex calculations with ease - no waiting for a plot to load or hitting the row limits in a spreadsheet.

Also, due to how it installs, at least in my experience, it is not typically blocked by corporate IT limitations - you're not sneaking, it's just not a problem

Setting Expectations

This is an introduction to help you get things done, with the methods I've found best for me - this is not exhaustive nor will it qualify you as a software engineer. If you work through this page you've merely passed your driver's test - you are not even close to being a professional racer.

Setting Up Your System

This setup works well for me, and should for you too - but there is no shortage of good alternatives to try out!

Important Note

This is setup for Windows machines - process on Mac or Linux will be similar, but different

Install Python

  1. Download the latest version of Python for your system from the website
  2. Run the installer - make sure to check the box that says "Add Python to PATH" before clicking "Install Now"
  3. That's it - you have Python installed!

Install an IDE

An IDE is an Integrated Development Environment - a tool that helps you write, manage, and run your code more easily.

You don't have to use and IDE, but it makes a lot of things easier and faster

  1. Download VSCode and install it
  2. Open VSCode, go to the Extensions tab (left sidebar, square icon), search for "Python" and install the extension by Microsoft
  3. Also install the Jupyter extension by Microsoft
  4. Restart VSCode and you should be good to go - you can customize settings and additional extensions as you want

Create your first Virtual Environment

This is another thing you don't have to do, but wlearn the habit now and it will keep projects organized for future you

Note on Virtual Environments

This is a self contained environment for your project (a specific folder with its own Python installation and packages) - you will need to do this again for each project.

  1. Create a folder for your first project - do it in command prompt if you want, or just a new folder in Windows Explorer or VSCode
  2. Navigate to that folder in command prompt or powershell
    1. In command prompt/powershell use "cd [filepath]" to get there
    2. In VSCode, open the folder and then open the integrated terminal and you'll be in the right spot - see it's a handy tool!
  3. Create the virtual environment by running the command: python -m venv .venv
  4. Activate the virtual environment:
    • In command prompt, run: .venv\Scripts\activate.bat
    • In powershell, run: .venv\Scripts\Activate.ps1
    • Execution Policy Errors

      I have run into execution policy issues when trying to run this command in powershell. If you get an error, you can bypass it... but just switch to command prompt or use the VSCode terminal.

    • In VSCode terminal, it should auto activate when you open the terminal - if not use the powershell command
  5. You should see the terminal prompt change to show the virtual environment is active - something like: (.venv) C:\Users\you\path\to\project>
  6. While in here, let's install some packages via pip - these are essentially python toolboxes; run pip install pandas numpy matplotlib plotly
    1. These are the packages we'll be using in this guide - pandas for data manipulation, numpy for numerical operations, matplotlib and plotly for plotting
    2. We will likely install more later, but this is a good start.
  7. To deactivate the virtual environment when you're done working, just run the command: deactivate
  8. Next time you come back, just re-run the activation script and the environment will how you left it.

How to Run Code

Objectives

Running Python In the Terminal

This is the most basic way to run Python code - directly in your terminal. It isn't a bad way to start, but it's not the most efficient and will have you jumping between tools. We'll walk through here for the sake of completeness, but I recommend mostly forgetting about this after this section, until you find a reason to come back to it.

  1. Open your terminal (command prompt, powershell, or VSCode terminal)
  2. Make sure your virtual environment is activated (see previous section)
  3. Create a new Python file in your project folder - you can do this in VSCode or any text editor. Name it hello.py
  4. Add the following code to the file (using VSCode or a text editor such as Notepad):
    print("Hello, World!")
  5. Save the file
  6. In the terminal, run the script by typing: python hello.py
  7. You should see the output: Hello, World!

And there you have it - running your first Python script in the terminal - you're a real hackerman! But now let's move on to more practical and efficient ways to run Python code.

Running Python in VSCode

VSCode makes it easy to write and run Python code with its built-in features and extensions.

  1. Open your project folder in VSCode
  2. Create a new Python file named hello_vscode.py
  3. Add the same code as before, with a small modification:
    print("Hello, World from VSCode!")
  4. Save the file
  5. Right-click anywhere in the file or click the "Run" button and select "Run Python File in Terminal"
  6. You should see the output: Hello, World from VSCode! show up in the integrated terminal (probably the bottom of your screen)

VSCode also offers features like IntelliSense (code completion), debugging, and version control integration, making it a powerful tool for Python development. It's not a dedicated Python IDE, but it's very effective and will let you work well with other file types as well.

VSCode also has a lot of extensions for convenience - we've installed some alreayd, but go ahead and epxlore if any catch your attention or you think there should be a certain capability.

Running Python in Jupyter Notebooks

Jupyter Notebooks are an interactive way to write and run Python code, combining code, text, and visualizations in a single document.

  1. In your terminal (with the virtual environment activated), install Jupyter by running: pip install jupyter (you only need to do this once per virtual environment)
  2. In VSCode, create a new file titled hello_jupyter.ipynb
  3. Open the file in VSCode - it should open in the Jupyter Notebook interface
  4. In the first cell (should be code type by default), type:
    print("Hello, World from Jupyter!")
  5. Press Shift + Enter to run the cell
  6. You should see the output: Hello, World from Jupyter! below the cell

Jupyter Notebooks are great for data analysis, visualization, and sharing your work with others. They allow you to document your code and results in a clear and organized manner via markdown cells interspersed within the code blocks.

Basic Python

Objectives

Limitations of this Section

This may be the most underdeveloped section of this page - my goal is to give you just enough information to get things done.

Python is known for its simple and readable syntax, making it easy to learn and use. Here are some basic concepts to get you started:

And there you have it - a very incomplete introduction to Python. As you work through the examples and labs, you'll become more familiar with Python's capabilities and syntax.

Reading Data

Plotting Data

Automating Tasks

Engineering Math in Python

Hands-On Labs

Additional Resources