Coding Tutorials

Your first Python program: install, run and fix common errors

Flat illustration of a beginner writing a first Python program on a laptop, with simple code shapes on screen

A first Python program is a short text file that Python reads from top to bottom and runs. You do not need a large project, a database, or a framework to start. You need Python installed, a text editor, and one command that tells Python which file to run.

The simplest useful first program prints a line of text. That one line is enough to check the whole setup. If the text appears, the install works, the file is saved in the right place, and the command points at the right file. If it does not appear, the error message usually points at the exact step that went wrong, and this guide shows how to read it.

What you need before you start

Start with three things. First, Python itself, downloaded from the official Python site. Second, a place to write text, such as a plain text editor or a code editor. Third, a command line window, which is called Terminal on macOS and Linux and Command Prompt or PowerShell on Windows. The Python tutorial in the official docs uses the same idea: write code in a file, then run it with the Python interpreter.

Install and check Python

Download Python from python.org and follow the installer for your system. After the install finishes, open a command line window and type a command that asks Python to report itself. On many systems the command is python, and on some systems it is python3. The exact command depends on how Python was installed, so try python first and then python3 if the first command is not found.

python --version
python3 --version

Either command should print a line that starts with the word Python and then a version. If you see that line, the install is ready. If the window says the command is not found, Python may not be on the command line path yet. In that case, close the window, reopen it after the install, and check the installer option that adds Python to the path. Reinstalling with that option set is a normal fix, and it is better than copying commands from a random forum post.

Write and run your first program

Create a new file and save it with a name that ends in .py, for example hello.py. The .py ending tells you and your editor that the file holds Python code. Put the file in a folder you can find again, such as a folder called python-practice on your desktop or in your documents. Write one line in the file, then save it before you run it. Running an unsaved file is a quiet cause of confusion, because Python runs the saved version, not the text still sitting in the editor.

print("Hello, world!")

Now move to that folder in the command line and run the file. The command has two parts: the Python command, and the file name. Python reads hello.py, runs the print line, and shows the text on the screen.

python hello.py
python3 hello.py

The output should be a single line.

Hello, world!

What print does, and how to change it

The print function takes a value inside brackets and shows it. Text goes inside quotes, and numbers can go without quotes. The examples below show the difference, and they also show that print can show more than one value at once when the values are separated by commas.

print("Learning Python")
print(42)
print("Age:", 42)

Once print works, the next step is to hold values so a program can reuse them. That is the job of variables and data types in Python, which explains names, numbers, text, and true or false values in plain terms. After that, conditions in Python let a program choose between paths, and functions in Python let you reuse a block of code under a name.

Read an error message instead of fearing it

An error message in Python is a report, and its last line is usually the most useful part. The lines above it show where the problem was found, with a file name and a line number. Start at the last line, read the error name, then look at the line number it points to. Many beginners read the whole traceback from the top and feel lost, when the last line already names the problem in plain terms.

Error nameWhat it usually meansWhat to check first
NameErrorA name was used that Python does not recognise in that spotSpelling of the name, and whether it was defined before use
SyntaxErrorThe line does not follow Python grammarMissing bracket, quote, or colon on or just before the reported line
IndentationErrorSpacing at the start of lines is inconsistentWhether lines in the same block use the same indentation
FileNotFoundErrorThe file name or folder in the command does not match a real fileThe current folder in the command line, and the spelling of the file name

Here is a small example. The line below is missing a closing bracket, and Python reports it as a syntax error with the line number. Fixing it means adding the bracket, saving, and running again. No other part of the program needs to change.

print("Hello, world!"
# Missing closing bracket: add ) at the end, save, run again

Common mistakes to watch for

Running the file before saving it is the most common early mistake, and it produces a confusing result because the output matches the older saved version. Save first, then run, and do that every time. A related mistake is saving the file in one folder and running the command from a different folder, which leads Python to report that the file cannot be found. Check the folder in the command line with the command that lists files, and move to the right folder before running.

Spelling mistakes in function names are common, because Python treats print and pirnt as different names. The error name in that case is NameError, and the fix is to correct the spelling. Missing quotes around text cause a different error, because Python then tries to read the text as code instead of as a value to show. Quotes tell Python where the text starts and ends, so a missing quote at the end of a line breaks the whole line.

Inconsistent indentation causes errors even when the words are right. Python uses indentation to show which lines belong together, so lines in the same block must line up. Mixing tabs and spaces in the same file is a known source of this error, so use one method and keep it consistent. The official tutorial explains indentation as part of how Python groups statements, and it is worth reading that section once before you write longer programs.

Limits of this guide

This guide covers a local install, a single file, and the print function. It does not cover installing third party packages, setting up a virtual environment, or configuring an editor in depth, because those steps are not needed for a first program and they add failure points before the basics work. It also does not cover how to choose between operating systems or how to fix a broken system install, which needs system specific help. If the installer itself fails, the official Python site and its installation docs are the right next source, not a guess based on an old blog post.

A second program that asks for input

Once print works, a natural next step is a program that asks the reader for a value and then uses it. The input function shows a prompt, waits for typed text, and hands that text back to the program as a string. That behaviour is worth learning early, because it shows a program taking in data rather than only showing fixed text. The example below asks for a name, stores the answer, and prints a greeting that includes it.

name = input("What is your name? ")
print("Hello,", name)

Run the program, type a name when the prompt appears, and press Enter. The greeting should show the name you typed, which proves the value travelled from the keyboard into a variable and then into print. If the program seems to wait with no output, it is usually waiting at the input prompt, so type something and press Enter before deciding anything is broken.

One detail catches beginners here. Input always gives back text, even when the reader types digits. Typing 42 gives the string "42", and adding that string to a number will fail, because Python will not mix text and numbers in addition. The fix is to convert the text with int when a whole number is needed, and to check that conversion in a separate step before the program does maths with the value.

Walk through a program line by line

Reading code slowly is a skill of its own, and a short program is a good place to practise it. Take the greeting program above and read it as a set of steps in order. First the program reaches the input call, shows the prompt, and waits. Then it stores the typed text under the name name. Then it reaches print, builds the greeting from the fixed text and the stored value, and shows the result.

That order matters, because Python runs lines from the top down unless a statement changes the order. A line cannot use a value before that value has been created, and a print call shows the value a variable holds at that moment, rather than a value it will hold later. If you move the print line above the input line, the program will fail, because the name does not exist yet at that point in the run.

Try a small change on purpose and watch what happens. Change the greeting text, save, and run again, then change the prompt text and run again. Each run should show only the change you made, which teaches a useful habit. Change one thing at a time, run the program, and read the result before changing anything else, because that habit makes the cause of any new error easy to spot.

Work with files in a folder you can find again

As programs grow past one file, the folder layout starts to matter as much as the code. Keep practice files in one folder, give each file a plain name with no spaces, and run the command from inside that folder. Spaces in file names force extra quoting on the command line, and a file saved on the desktop while the command line sits in a different folder is a common reason for a file not found error.

It also helps to keep a short note beside the code while learning. A comment line starting with # is ignored by Python, so it can record what a line is for, in plain words. Comments do not change how the program runs, and they are most useful when they explain why a line exists, rather than repeating what the code already says in an obvious way.

# Ask for a name, then greet that name.
name = input("What is your name? ")
print("Hello,", name)

When a program stops working after a change, the folder and the file name are still worth checking first. Confirm the file was saved, confirm the command line is in the same folder, and confirm the file name in the command matches the saved name exactly, including capital letters. Those three checks solve a large share of first program problems without touching the code at all.

Troubleshooting when nothing appears

A blank result has a few likely causes, and they can be checked in order. First, check whether the program is waiting for input, as described above. Second, check whether the file was saved after the last edit, because an unsaved edit never reaches Python. Third, check whether the command ran a different file with a similar name, which can happen when two practice files sit in the same folder.

If an error does appear, read the last line first, then the line number. Open the file at that line and look at that line and the line just above it, because a missing bracket or quote on one line is often reported on the next line. Fix the one problem, save, and run again. Fixing several things at once hides which change actually helped, and it can add a new error while the old one is still present.

When the output is present yet wrong, add a print call that shows a value at a key point in the program. Print the variable right after it is set, and again right before it is used, and compare the two values. That simple check often shows that a variable holds a different value than expected, or that a line ran in a different order than assumed.

Frequently asked questions

Can I practise Python without installing anything?

Yes, an online Python editor in a browser can run short programs, but a local install gives you files you can keep, edit and run again. The steps in this guide use a local install because that is the setup you will use for larger programs.

Does the editor I choose change how Python runs?

No, the editor only holds the text. Python reads the saved file and runs it. A plain text editor works, and a code editor adds colour and line numbers without changing the program itself.

What should I learn after print works?

Learn variables next, so a program can hold values and reuse them, then conditions, so it can make decisions. The variables guide on this site is a good next step.

Why does my program wait and show no output?

It is usually waiting at an input prompt, so type a value and press Enter. A program that asks for input will not continue until the reader answers, and that pause can look like a frozen program when the prompt is easy to miss.

How do I turn typed input into a number?

Use int for a whole number or float for a decimal value, and store the converted result in a variable before doing maths with it. Input on its own always gives back text, so the conversion step is needed whenever the program must add, subtract, or compare numbers.

Should I keep all practice files in one folder?

One folder for practice files makes the command line step easier, because the run command and the saved file stay in the same place. Give files plain names with no spaces, and check the folder before running when a file cannot be found.

Sources