Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Your first Python program

You’ve already changed a greeting during setup. Now let’s look at what made that work. A program is a set of instructions. A Python source file stores those instructions as text, and the Python interpreter carries them out.

Text and instructions

print("Hello, Spartronics!") asks Python to display text in the terminal. print is the name of a built-in function, a reusable action. The parentheses contain the information we give it. Here that information is a string: text surrounded by quotation marks. The quotation marks tell Python where the text starts and ends; they are not part of the displayed greeting.

Python runs these statements from top to bottom. An empty line makes the file easier for people to read. A line starting with # is a comment for people; Python ignores the comment when running the file. Comments explain a purpose, but changing a comment alone won’t change what the program does.

Read it before running it

Put this worked example in student-work/foundations/first_program.py. Read it once and write down what you think it prints. Then save and run python student-work/foundations/first_program.py using your environment’s Python.

first_program.py
# Introduce the program.
print("Hello, Spartronics!")
print("This is my first program.")

You should see two separate lines, in the same order as the statements. Swap the two print lines, save, and run again. Did the order change as you expected? Now change only the comment and run once more. That change should not affect output.

Keep code and terminal commands separate: python ... is the command that starts the interpreter; it does not belong inside the file being interpreted.

Try it yourself

Before moving on

Without looking back, explain what the quotes, parentheses, and comment each do. Change the final line, save, rerun, and point to the changed output.