Lab02: Python Comand Line and Thonny¶
Introduction¶
The goal for this lab is to gain experience editing, compiling and executing Python programs in the terminal and through the Thonny IDE. You may work on this lab individually or in a group of no more than three people.
Executing Python in the Terminal¶
Each of the steps below should be completed entirely inside the terminal: no GUI applications allowed. Refer to the Unix Tutorial for Beginners if you need to learn more about Unix commands.
- Create a cs149 folder on your desktop
mkdir cs149 - Move into the cs149 directory:
cd cs149 - Create a folder inside your home directory named
lab02
.
mkdir lab02 - Move into the lab02 directory:
cd lab02 - Copy the file welcome.py by doing a Right-Click Save-As and saving it to your lab02 folder. OR here is the code:
print ("Welcome to CS149!") print("It's fun.")
cut and paste this into a file called welcome.py and save it in the lab02 folder.
6. Run welcome.py:
$ python3 welcome.py
- Congratulations! You've successfully executed your first Python program.
Editing Files in the Terminal¶
Normally, we will be using an Integrated Development Environment (IDE) to edit and compile Python programs. However, it can sometimes be convenient to edit a file directly in the terminal. There are many terminal-based editors. Today we'll try nano
because it is easy to use for beginners.
- Open
welcome.py
usingnano
:
$ nano welcome.py
You should see something like the following:
The two lines of text at the bottom show the set of actions available in the editor. The "^" symbol indicates the "Ctrl" key. For example, pressing Ctrl-O will "WriteOut" (save) any changes you have made to the file.
- Edit the file so that the welcome message says
"It's REALLY fun."
instead of"It's fun."
. Save your changes and exit. - Try executing your program again:
$ python3 welcome.py
- You should see the following output
Welcome to CS149! It's REALLY fun.
Thonny IDE¶
Objectives¶
- Use an IDE (Integrated Development Environment).
- Edit, save, compile, and run a simple Python program.
- Recognize and correct syntax errors in a Python program.
Key Terms¶
- source file
- the Python program as written by the programmer
- syntax error
- mistake in the source code that prevents compilation
- logic error
- mistake in the program that causes incorrect behavior
- execute
- the process of running a program on a computer
Part 1: Thonny Editor¶
Thonny is a text editor designed to simplify the process of editing, debugging and executing Python programs.
- Open Thonny and click "File –> Open" from the menu and select your welcome.py file from above.
- Press the Green play button on the toolbar to execute the Python file(this is the same
thing as running the >>python welcome.py in the terminal. -
>>> %Run welcome.py
Welcome to CS149!
It's REALLY fun. - Edit the file in Thonny to print another line that prints
I have edited a file in Thonny. - Save your results by pressing the Floppy disk icon then run the program again by pressing the Green play button.
- You have just run and edited a program using Thonny.
Submission for Lab2¶
- Via https://www.gradescope.com submit the Word of the Day in Canvas and your welcome.py file to Gradescope.
Acknowledgments
This activity is based on a lab developed by Nathan Sprague based on a lab originally developed by Chris Mayfield.