Home · Academy · Robotics & Coding · Python Fundamentals · Your Setup and First Program

Your Setup and First Program

Learn how to run Python, the indentation rules, and how to write your first small program.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Variables and Data Types: We learn how to put information into named boxes and work with types such as numbers, text and true/false.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteWhat Is Python
ContentStandard lesson · 1,410 words
Last updated

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.

A file table
FeatureInteractive shellFile
When to useQuick triesA real program
Is it savedNoYes
Number of linesUsually oneMany
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.

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

Check questions

  1. What ending do Python files have?
  2. What is the main difference between the interactive shell and a file?
  3. What is the # sign used for?
  4. Why is indentation important in Python?
  5. What should you do before running code you found on the internet?

Answers

  1. The .py ending. For example, first_program.py.
  2. 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.
  3. # starts a comment. Python does not run that line; comments are only for people to read.
  4. Indentation shows which lines belong to a block (for example, inside an if). If it is missing, Python gives an IndentationError. The standard is 4 spaces.
  5. 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.

Start QuizBack to Python Fundamentals
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.