Skip to content
Happy Programming Guide
Start learning
Python

Your First Python Program (Line by Line)

Write, save and run your first Python program, then extend it. Every line explained, plus the three errors beginners hit in the first ten minutes.

Here is your first Python program. Three lines, and it does something real.

Python
name = input("What is your name? ")
year = int(input("What year were you born? "))
print(f"Hi {name}, you are about {2026 - year} years old.")

The rest of this guide explains every part of it and shows you how to run it.

Step 1: create the file#

Make a folder called python-practice. Inside it, create a file named greet.py. The .py ending is what marks it as Python.

Any text editor works. VS Code is the usual choice — see VS Code setup for beginners. Do not use a word processor; it adds formatting that breaks code.

Step 2: type the program#

Python
print("Hello, world!")

Type it rather than pasting it. Typing is where the muscle memory comes from.

Step 3: run it#

Open a terminal in that folder and run:

Terminal
python greet.py

On macOS or Linux, use python3 greet.py. If neither is found, see how to install Python.

Making it interactive#

Now replace the file’s contents:

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

Line by line:

  • input("What is your name? ") shows the question and waits for the person to type and press Enter.
  • name = stores whatever they typed under the name name. See variables explained.
  • print(...) displays the result. The + joins pieces of text together.

A nicer way to build text: f-strings#

Joining with + gets ugly fast. Put an f before the quotes and use braces instead:

Python
name = "Ada"
age = 36
print(f"{name} is {age} years old.")

You can even do arithmetic inside the braces: f"Next year: {age + 1}".

The finished program#

Python
name = input("What is your name? ")
year = int(input("What year were you born? "))

age = 2026 - year

print(f"Hi {name}, you are about {age} years old.")

if age >= 18:
    print("You can vote.")
else:
    print(f"You can vote in {18 - age} years.")

That uses input, conversion, a variable, arithmetic, an f-string and a condition — the core of the language in nine lines.

Comments#

Anything after a # is ignored by Python and written for humans.

Python
# Ask the user for their details
name = input("Name: ")   # this part runs

Use comments to explain why something is done, not what. The code already says what.

Questions people ask#

Do I need a semicolon at the end of lines?

No. Python ends statements with a new line. Semicolons are legal but almost nobody uses them.

Why does my program close instantly on Windows?

If you double-click the file, the window closes as soon as the program ends. Run it from a terminal instead, or add input("Press Enter to close") at the bottom.

Where should I save my files?

Anywhere you can find again, in a folder with a plain name and no spaces. Avoid your Desktop root once you have more than a few projects.

How do I stop a program that will not finish?

Press Ctrl + C in the terminal. That usually means you have written an infinite loop.

Where to go next#

Next lessonPython variables explained

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *