One-sentence summary
In this lesson we set up a place to run Python code, learn the difference between the interactive shell and a file, and write our first small program.
Why does it matter?
In the previous lesson we saw what Python is and where it is used. But you cannot learn a language just by reading about it; you need to write and run it.
To write code, we need somewhere to write it. We call this a development environment. A development environment is the program where we write code, run it and see its errors. Choosing a good one makes the first steps much easier.
When I started programming, I got stuck on a simple question: "Which program should I open first?" This lesson is here to answer that question clearly.
Ways to run Python
There is more than one way to run Python code. They all use the same Python language; only the place where we write the code changes.
1. Thonny
Thonny is a free program made for beginners. It is easy to install and comes with Python, so you do not need to add anything else. The screen has an area where you write code and an area where the result appears.
I suggest trying this lesson's examples in Thonny. It is a good idea to do the installation together with an adult, just to be safe.
2. IDLE
IDLE is a tool that comes automatically when you install Python on a computer. It has a plain look. If you cannot install Thonny, IDLE will do the job.
3. Online editor
If you do not want to install anything, there are online editors that run inside a web browser. You open a website, type your code and press a "run" button. This is a handy option when you cannot install programs on a school computer.
Short note: Whichever environment you choose, the Python code you write is the same. The environment is only a tool.
The interactive shell versus a file
We use Python in two different ways. Understanding the difference between them is very important.
The interactive shell
The interactive shell is where you type a single line and see its result right away. It usually starts with a >>> sign. You can think of it like a calculator.
Type this in the shell and press Enter:
>>> 5 + 3
8
Let's try one more line:
>>> print("Hello")
Hello
The shell is great for trying small things quickly. But what you type is not saved permanently.
A file
A file is where you write many lines of code together and save them. Python files end with .py. For example, first_program.py is a Python file.
You save the code in a file once, and then you can run it again whenever you want. Real programs are written as files.
| Feature | Interactive shell | File |
|---|---|---|
| When to use | Quick tries | A real program |
| Is it saved | No | Yes |
| Number of lines | Usually one | Many |
| Its sign | >>> | .py ending |
Comments and indentation
Before writing code, we need to learn two basic rules: comments and indentation.
Comments (#)
A comment is a note that Python does not run; it is only there for people to read. If you start a line with #, that line becomes a comment.
# This is a comment, Python does not run it
print("Hello world") # This note is a comment too
Comments help you remember what your code does and explain it to others. It is a good habit.
Indentation
Indentation is the space at the start of a line. In many languages, indentation is only there to look tidy. In Python, indentation has a meaning: it shows which lines belong together.
In Python, the standard is to use 4 spaces for indentation. In the example below, the print line is indented because it belongs inside the if line:
age = 12
if age >= 10:
print("This lesson is right for you")
print("Let's get started")
If you forget the indentation, Python gives an error:
if age >= 10:
print("Error!")
This code produces an IndentationError. Python expects the lines inside an if to be indented.
Mini practice
Now let's write our first small program. Open a new file in Thonny or the environment you chose and save it as first_program.py.
Type the following code:
# My first Python program
print("Hello, I am learning Python!")
name = input("What is your name? ")
print("Nice to meet you, " + name)
Here the print command displays text on the screen. The input command asks you to type something and stores what you typed. The text we put between quotation marks is called a string.
Save and run. The program will ask for your name, and when you type it and press Enter, it will greet you by name. If you run the same program a second time with a different name, you will see the result change too. This is the power of writing a program once and running it many times.
Now make a small addition. Add this to the end of the program:
age = int(input("How old are you? "))
if age >= 13:
print("You are a teenager")
else:
print("You are younger than 13")
Here the word int turns the text you enter into a number. With if and else we made a decision. Try the program with different ages.
Common mistakes
Forgetting the indentation
If you do not indent the lines that come after if, else or a loop, Python gives an error. Always use 4 spaces.
Mixing spaces and Tab
Do not mix spaces and the Tab key for indentation. This causes errors you cannot see with your eyes. Use only spaces; most editors add spaces automatically when you press Tab.
Not closing a quotation mark
If you forget to close a quote, like print("Hello), Python gives an error. Remember to close every quote you open.
Running a file without saving it
If you do not save your latest changes, the old version runs. Always save before you run.
Safety note
You can find ready-made Python code on the internet. Be careful before running it.
- Only run code from sources you trust. Before running code from a site you do not know, show it to an adult.
- Do not type personal information such as your address, phone number or passwords into your programs.
- Ask an adult for help when installing a new program on your computer.
Code tells a computer what to do. That is why it matters to understand what you are running. When you meet a line you do not understand, stopping to think about it is a good programmer's habit.
Lesson summary
- We can run Python with Thonny, IDLE or an online editor; the code is the same in every environment.
- The interactive shell is for quick tries; a file is for saving real programs.
- Lines starting with
#are comments, and Python does not run them. - In Python, indentation is a rule; it shows which lines belong together, and we use 4 spaces.
- In our first program we used the
print,input,ifandelsecommands.
Check questions
- What ending do Python files have?
- What is the main difference between the interactive shell and a file?
- What is the
#sign used for? - Why is indentation important in Python?
- What should you do before running code you found on the internet?
Answers
- The
.pyending. For example,first_program.py. - The interactive shell runs a single line right away but does not save it; a file saves many lines and can be run again and again.
#starts a comment. Python does not run that line; comments are only for people to read.- Indentation shows which lines belong to a block (for example, inside an
if). If it is missing, Python gives anIndentationError. The standard is 4 spaces. - Make sure the source is trustworthy; if I am not sure, show it to an adult, and never type my personal information into the code.
Source and verification note
For “Your Setup and First Program”, verification focuses on whether the relationship between Ways to run Python and 2. IDLE remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Variables and Data Types: We learn how to put information into named boxes and work with types such as numbers, text and true/false.