Course 2.3Lesson 3 of 4
A Python program is a plain text file whose name ends in .py, such as hello.py. Write it in an editor such as IDLE, Thonny or Visual Studio Code. In IDLE, choose File, New File, type the program, save it, and press F5 to run it. In a terminal, go to the folder with cd and run python hello.py on Windows, or python3 hello.py on macOS and Linux. Check that the file name does not end in .txt, and remember that input() waits for an answer in the terminal.
Try the code and the simulations beside the lesson.
After this lesson you can
cd, andA Python program is a plain text file whose name ends in .py, such as hello.py. Python reads the file and follows the lines from top to bottom, exactly as in the code boxes. The only difference is where you write it and how you start it.
Here is a first program for a file.
You can write the file in any editor that makes plain text. These are good for a beginner.
| Tool | Notes |
|---|---|
| IDLE | Comes with Python on Windows and macOS. Simple, and a good first tool. |
| Thonny | Free, made for beginners, with easy buttons and a view of the variables. |
| Visual Studio Code | Free, and used by professionals. It needs the Python extension. |
| Notepad or TextEdit | Possible, but be careful with the file name (see below). |
A word processor, such as Word, is not a good choice. It adds formatting to the file and changes the quotation marks.
print("Hello!").hello.py. Remember the folder where you save it.cd. For example, if the file is in Documents, type cd Documents and press Enter.python hello.py on Windows (or py hello.py), and python3 hello.py on macOS and Linux.Some terminal commands help you to find the way.
| Command | What it does |
|---|---|
cd folder | goes into a folder |
cd .. | goes one folder up |
dir (Windows) or ls (macOS, Linux) | lists the files in the folder |
The terminal shows the folder that you are in. The program can only be found when you are in its folder, or when you write its full name, for example python Documents/hello.py.
hello.py.txt. Windows hides the ending of known file types, and Notepad may add .txt when you save. In File Explorer, turn on File name extensions in the View menu. In Notepad, set Save as type to All files. Run this check for different names:The terminal always works inside one folder. To run hello.py, first go to the folder that holds it.
On this site, you type the answers of input() in the Input box before you press Run. In a terminal, a program with input() stops and waits. You type the answer and press Enter. The program then goes on, to the next line. It feels like a conversation.
Type your name and your city on two lines of the Input box, and run it. Then save the same program as card.py on your computer and run it from a terminal. Notice that, in the terminal, you type each answer when the question appears.
.txt. The terminal then says that this is not a Python file, or it runs nothing.cd and dir or ls.random.py, math.py or print.py. It hides the real module with the same name, and gives strange errors. Choose names such as hello.py or my_card.py.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
.py.python hello.py or python3 hello.py, from the folder of the file.cd goes into a folder, dir or ls shows the files..txt. Do not use the name of a module for your file.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
hello.py in IDLE?hello.py.txt a problem?input() work in a terminal?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.