BYU logo Computer Science

Running a Python program

You should do this activity after you have downloaded the Lab 0 zip file

In PyCharm, your project contains all the folders and files for the semester:

cs110 project in PyCharm

These are in the top left corner:

cs100 project files in PyCharm

This should match the organization of your cs110 folder in File Explorer (Windows) or Finder (MacOS).

Later in the semester, you should folders for each of your labs, homeworks, and projects inside of this same cs110 folder.

Opening a file

You can open files and directories by double-clicking on them.

  1. Double-click on the hello folder.
  2. Double-click on the hello.py file.

You should see the hello.py program:

hello program in PyCharm

The main block

Every program has to start somewhere. In Python, we call this the main block. This is where Python starts when it runs your code.

We will explain more about the code here later. Don’t worry about learning what all of this means.

Running a program

To run a program, find its main block and click the green triangle to the left of it.

click the green triangle

Select Run 'hello'.

You will see a window open at the bottom of PyCharm that has the output of the program:

PyCharm output of hello

That is all this program does! It prints Hello! Welcome to CS 110!.

Every time a program finishes successfully, Python will print Process finished with exit code 0.

Running another program

  1. Double-click on the intro folder.
  2. Double-click on the introduce-yourself.py program.

You now have two programs open, each one in a separate window. You can click on the tabs at the top to switch between the two files.

PyCharm intro program

Run it by finding the main block and clicking on the green triangle next to it. You should see this in the window at the bottom:

Pycharm output of intro

Here, Python is waiting for input, meaning you need to type your name. Click in that window, type something, and press enter.

PyCharm result of intro

Stop and re-run

Run the intro program again by clicking on the green triangle next to the main block. Take a look at the window on the bottom again:

Pycharm output of intro

On the bottom left, you will see:

  • a red square — if you press this, it stops your program immediately
  • a green curved arrow — if you press this, it stops your program and re-runs it from the beginning

Try out these buttons to see what they do.