Skip to main content

Section 1.6 Writing Assignments—The Thonny IDE

While activecode and codelens are great for learning the material in this book as you are reading it, you won’t use them when you are writing an assignment. Instead, you will be using an Integrated Development Environment (IDE). There are a lot of IDEs available, but we recommend that you use Thonny 1  because:
  • Once you download and install it, you will have Python available to you right away—you won’t need to install Python separately.
  • You’ll have a single window that has both a source code editor (the top half of the window in Figure 1.6.1) and a Python shell (the bottom half of the window).
  • Thonny gives you the ability to go through your program one step at time, much like codelens.
Figure 1.6.1. The Thonny IDE Window
In the Python shell area, you can type Python statements and expressions, and, when you press ENTER, Python will immediately execute what you typed and come back with another prompt. Here’s an example of adding two numbers in the shell:
>>> 2 + 3
5
>>>
The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. We typed 2 + 3. The interpreter evaluated our expression and replied 5. On the next line it gave a new prompt indicating that it is ready for more input.
Working directly in the Python shell is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems.
Alternatively, you can write an entire program by placing lines of Python instructions in the upper part of the Thonny window, saving them to a file, and then using the Run button (the triangle in a green circle) to execute the contents of the file as a whole. Such a file is often referred to as source code. For example, you can write the following lines and save them in a file named firstprogram.py:
print("My first program adds two numbers, 2 and 3:")
print(2 + 3)
By convention 2 , files that contain Python programs have names that end with .py. Following this convention will help your operating system and other programs identify a file as containing Python code.
When we click the Run button (or choose Run current script from the Run menu), the results will appear in the Python shell area:
>>> %Run firstprogram.py
My first program adds two numbers, 2 and 3:
5
>>>
Thonny automatically provides the %Run firstprogram.py command; you don’t have to type it yourself.
Check your understanding

Checkpoint 1.6.2.

    Python source code files have names that end with:
  • .program
  • While it is a program, you use only two letters after the dot.
  • .py
  • Yes, that is the correct file suffix.
  • .python
  • Programmers usually don’t like typing long words. Hint: use just the first two letters of “python”.
thonny.org/
A convention is an unwritten rule that Python programmers are not required, but are generally expected to follow.