Lab: Editing and Running Python Programs#
Note
Source: Adapted from the C# edition (data/lab-edit-compile-run.rst).
Python workflow replaces the C# edit-compile-run cycle; VS Code and
terminal instructions are original additions.
The goal of this lab is to get a working Python program running on your computer.
The Python Workflow#
Unlike C#, Python does not require a separate compile step. The workflow is:
Write a
.pyfile with a text editor.Run it directly with
python3.
There is no build or compile command.
Setting Up#
If Python 3 is not yet installed, download it from python.org. Open a terminal and verify:
$ python3 --version
Python 3.12.0
We recommend Visual Studio Code as an editor. Install the Python extension from the VS Code marketplace.
The Interactive Shell#
Before writing a file, try the interactive shell. Type python3 at the
terminal prompt:
$ python3
Python 3.12.0 (default, ...)
>>>
You are now at the Python REPL (Read-Evaluate-Print Loop). Type any expression and press Enter:
>>> 2 + 3
5
>>> "Hello!"
'Hello!'
>>> print("Hello, world!")
Hello, world!
Exit with quit() or Ctrl-D.
Your First Script#
Create a new file called
hello.pywith this content:print("Hello, world!")
Run it from the terminal:
$ python3 hello.py Hello, world!
The Painting Program#
Now run the painting program from A Sample Python Program.
Create a file called
painting.pyand type in the program.Run it and verify you get the same output as shown in the sample run.
Run it a second time with different values (for example, length 15 and width 6.5) and check that the output is correct.
Calculation of Room Paint Requirements
Enter room length: 15
Enter room width: 6.5
The wall area is 344.0 square feet.
The ceiling area is 97.5 square feet.