📓 About This Tutorial

This webpage is a static view of an interactive Jupyter Notebook tutorial. To get the full interactive experience where you can run Python code, modify examples and complete exercises, click the "Open in Colab" button. This will access the Google Colab notebook from a GitHub repository github.com/dchappell2/Computational-Physics and open it in Google Colab. You will need a Google account if you want to open it in Colab.

Note: You can download a pdf of the lecture slides for this chapter here: Chapter 0-3 slides

Chapter 0 - Welcome to Python and Jupyter Notebooks

Objectives:

0.0 Introduction

This Google Colab version of the tutorial is a Jupyter Notebook running in Google Colab. Jupyter notebooks are a great way to mix executable Python code with rich contents (HTML, images, equations written in LaTeX). Colab allows us to run notebooks on the cloud for free without installing Python on your computer.

The purpose of creating an interactive Coding Tutorial is for you to play with the code and try things out. If you "break" the document, you can always go back and re-download the original, so please experiment. The most important thing is to have a curious mindset, try things out and have fun!

These tutorials are designed to be self-contained so you should be able to learn the basics of Python by reading through them and doing the exercises at your own pace.

0.1 Google Colab can be SLOW

To have an enjoyable google Colab experience, we recommend the following:

0.2 Code Cells and Text Cells

Everything in the Google Colab version of this document is written in either a Code Cell or a Text Cell.

To create your own Code Cell or a Text Cell, do the following:

Note: Every section that contains multiple Text and Code cells will have an expand/collapse icon next to it. You can use these to hide (or reveal) the content in that section.

0.3 Running your first Python program

The Google Colab version of this document that you are reading is not a static web page, but an interactive environment called a notebook that lets you write and execute code. Notebooks consist of so-called code cells, which are blocks of one or more Python instructions. For example, here is a code cell that displays the message "Hello World" to the screen. The print() command is used to display messages or variables to the screen. Our message can be enclosed in either single or double quotes.

print("Hello World")

0.4 Skill Checks let you practice coding

Each tutorial has a set of Skill Checks (indicated by green check boxes ✅) that you will need to complete to receive credit for each chapter. Here's your first Skill Check:

Skill Check

Skill Check

0.5 Table of Contents

This tutorial is divided into chapters, each in a separate Jupyter Notebook. You can view the contents of a given chapter by clicking on the Contents icon found at the top of the left-hand icon bar (it looks the "hamburger" icon ≡ with 3 dots in front of it). Click on it now to try it out!

0.6 Turn the Google AI assistant off

Google Colab documents have a built-in AI assistant (called Gemini at the time of this writing). I've noticed that as I've written these tutorials, Gemini will read my text and then automatically produce sample Python code once I create a Code Cell. Most of the time, Gemini does a pretty good job of "understanding" my intention and writing code that I would have written. It's actually pretty amazing.

In fact, AI tools have revolutionized how folks code. AI assistants like chatGPT, GitHub Copilot, Cursor, and many others can speed up code production immensely. Later in the course, we will explore some of these tools. Overall, we think AI is a good thing for coding, not a bad thing.

However, when you are first learning the basics of Python, and perhaps coding in general, the AI assistant can make the exercises in these tutorials too easy. Psychologists who study learning talk about cognitive load theory and the idea of "desirable struggle":

If you leave the Autocomplete setting in Gemini turned on, it will automatically produce many of the coding exercises for you and you can basically "turn off your brain." In other words, you won't learn to code.

For these reasons, we ask that you turn off Gemini autocomplete while you are going through these tutorials. Here's how to do it:

Once you save the settings, they will be used as the defaults when you open other Colab documents. If you get stuck and you want to use the AI assistant, you can always turn it back on by checking the appropriate boxes.

Skill Check

Please sign the following statement by typing your name into the text box:

I understand the importance of "desirable struggle" in learning and I agree to use AI sparingly and only when I feel I have exhausted other options.

Signature (type your name): (only works in the Google Colab version of the document)

🟩

0.7 Comments

It is often a good idea to comment your code. A comment is a message to yourself (or others reading your code) to describe what your code is doing. Comments are created using the hash tag #. Anything placed after the hash tag on a given line will be interpreted as a comment (and not Python code). Comments may be placed on a line of their own or after Python code as shown in the following example:

# this is my first Python program
print('hi')     # this line prints the message "hi"

Play the above code block to execute it using one of the methods described above. You should see the message "hi" appear under the code block.

Key Points

This tutorial is a modified adaptation of "Python for Physicists"

© Software Carpentry