Python Introduction

What Python is, how to install it and how to write and run your first program.

Lesson 1 of 107Beginner18 min read

Python is a programming language: a way of writing instructions that a computer can follow. You write the instructions as plain text, and a program called the Python interpreter reads them from top to bottom and carries them out.

Python is famous for being easy to read. A line like print("Hello") does exactly what it looks like. That makes it a popular first language, but it's also a serious tool used by companies like Google, Netflix and NASA for data analysis, automation, websites and machine learning.

In this lesson you'll install Python, write your first program, and learn the two things every program needs from day one: showing output with print() and leaving notes with comments.

Installing Python

  1. Go to python.org/downloads and download the latest Python 3 for your system.
  2. On Windows, tick "Add python.exe to PATH" on the first screen of the installer. This lets you run Python from any folder.
  3. Open a terminal (Command Prompt, PowerShell or Terminal on macOS) and type:
Terminal
python --version

If you see something like Python 3.12.4, Python is installed. On macOS and Linux the command is often python3 instead of python.

Always use Python 3. Python 2 stopped receiving updates in 2020, and old tutorials written for it won't always work.

Two ways to run Python

The interactive shell. Type python in a terminal and press Enter. You'll see a >>> prompt. Type a line of Python, press Enter, and the result appears straight away. It's perfect for quick experiments. Type exit() to leave.

Python
>>> 2 + 3
5
>>> print("Hi")
Hi

A script file. For anything longer than a line or two, write your code in a file that ends in .py, such as hello.py, using an editor like VS Code. Then run the whole file from the terminal:

Terminal
python hello.py

Most real programs are script files, so that's what the rest of this tutorial uses.

Showing output with print()

print() displays whatever you put between the brackets. Text must be wrapped in quotes, either "double" or 'single'. Numbers don't need quotes.

Python
print("Welcome to Python")
print(42)
print("Total:", 7 + 3)
  • You can print several things at once by separating them with commas. Python puts a space between them.
  • Each print() starts on a new line.
  • print() with nothing inside prints an empty line, handy for spacing out output.

Changing how print() separates and ends lines

print() has two optional settings that control its layout. sep sets what goes between the values (a space by default), and end sets what is printed after the last value (a newline by default).

Python
print("2026", "10", "11", sep="-")
print("Loading", end="...")
print("done")

The first line prints 2026-10-11. The next two lines print Loading...done on a single line, because the first call ended with ... instead of starting a new line. You will use end="" later when printing several items from a loop on one line.

Comments

A comment starts with #. Python ignores everything from the # to the end of the line, so comments are notes for humans.

Python
# Work out the price for three tickets
price = 12
print(price * 3)  # 36

Use comments to explain why the code does something, not to repeat what it obviously does. You can also put # in front of a line to switch it off temporarily while testing.

How Python runs your program

When you type python hello.py, the interpreter reads the file, checks that every line follows Python's grammar, and then runs the statements one at a time from the top. If it meets a problem it stops and prints a traceback: a short report naming the file, the line number and the type of error.

Text
  File "hello.py", line 2
    print("Hi)
          ^
SyntaxError: unterminated string literal (detected at line 2)

Read a traceback from the bottom up. The last line says what went wrong (SyntaxError, NameError, IndentationError), and the lines above it say where. Beginners often panic at red text, but these messages are the fastest way to find a typo. Go to the line number, compare it with a working example, and look for a missing quote, bracket or colon.

Python Syntax

print() and comments

# This is a comment
print("text in quotes")
print(value1, value2, value3)
print()  # empty line

Python Summary

  • Python is a readable, general-purpose programming language.
  • Install Python 3 from python.org and check it with python --version.
  • Use the >>> shell for quick experiments and .py files for real programs.
  • print() shows output. Text goes in quotes; commas separate several values.
  • # starts a comment that Python ignores.

Python Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Your first program

Save this as hello.py and run it with python hello.py.

Python
# My first Python program
print("Hello, world!")
print("I'm learning Python.")
print()
print("2 + 3 =", 2 + 3)
Output
Hello, world!
I'm learning Python.

2 + 3 = 5
Line-by-line explanation
  1. Line 1A comment. Python skips it.
  2. Lines 2 to 3Each print() shows one line of text.
  3. Line 4An empty print() leaves a blank line.
  4. Line 5Text and a calculation printed together, separated by a space.
Example 2

Python as a calculator

Python
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 4)
print(10 // 4)
print(10 % 4)
print(2 ** 10)
Output
15
5
50
2.5
2
2
1024
Line-by-line explanation
  1. Line 4/ always gives a decimal result.
  2. Line 5// divides and rounds down to a whole number.
  3. Line 6% gives the remainder: 10 divided by 4 is 2, remainder 2.
  4. Line 7** means "to the power of".
Example 3

A receipt with comments

Python
# Prices for a small coffee order
print("Flat white:", 3.20)
print("Croissant:", 2.50)
print("-----------")
print("Total:", 3.20 + 2.50)
Output
Flat white: 3.2
Croissant: 2.5
-----------
Total: 5.7
Line-by-line explanation
  1. Lines 2 to 3Python drops trailing zeros from decimals, so 3.20 prints as 3.2. You'll learn to format money properly later.
  2. Line 5The sum is worked out first, then printed.

Common Python Mistakes

Forgetting the quotes around text

Don't
print(Hello)
Do
print("Hello")

Without quotes, Python thinks Hello is the name of a variable and stops with NameError: name 'Hello' is not defined.

Mixing quote types

Don't
print("It's sunny')
Do
print("It's sunny")

A string must start and end with the same kind of quote. Use double quotes when the text contains an apostrophe.

A stray space at the start of a line

Don't
print("one")
 print("two")
Do
print("one")
print("two")

Python reads leading spaces as indentation, so the second line causes IndentationError: unexpected indent.

Python in Real Projects

Where you'll meet Python

Data analysts use Python with libraries like pandas to clean and chart spreadsheets. System administrators write short Python scripts to rename thousands of files or back up folders. Web developers build sites with Django and Flask, and scientists use it to process measurements. The print() you learned today is the first tool all of them reach for when checking what a program is doing.

Python FAQ

Is Python a good first programming language?

Yes. Its syntax uses plain words and indentation instead of braces and semicolons, so you spend more time on ideas such as variables, loops and functions and less on punctuation. Those ideas transfer directly to Java, JavaScript, PHP and other languages when you move on.

Should I type python or python3?

Use whichever command prints a Python 3 version when you add --version. On Windows the installer usually provides python (and the py launcher). On macOS and many Linux systems python3 is the safe choice, because python may be missing or point to an old Python 2.

Which editor should I use for Python?

Any plain-text editor works, because a Python program is just a text file. VS Code with the official Python extension is a popular free choice: it highlights mistakes, runs files with one click and includes a debugger. IDLE, which comes with Python, is enough for your first few lessons.

Is Python compiled or interpreted?

Python is usually described as interpreted. The standard implementation, CPython, first translates your file into bytecode and then runs that bytecode on a virtual machine. This happens automatically, so you never run a separate compile step as you would with Java or C.

Why does my program close straight away when I double-click it?

On Windows, double-clicking a .py file opens a console window that closes as soon as the program finishes. Run the file from a terminal with python hello.py instead, so the output stays on screen and you can read any error messages.

Python Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the line so it shows the text Python is fun on screen.

("Python is fun")
Predict the outputeasy

What does this program print?

print("Score:", 4 * 5)
Multiple choiceeasy

Which line is a comment in Python?

Predict the outputmedium

What does this print?

print(17 // 5, 17 % 5)
Predict the outputmedium

What does this print?

print("Total:", 2 + 3)

Python Quiz

5 questions to check what you learned.

  1. Which command checks which version of Python is installed?

    • python -check
    • print(version)
    • python install
    • python --version
  2. What file extension do Python script files use?

    • .pt
    • .pyt
    • .py
    • .python
  3. What does print(10 / 4) show?

    • 2.5
    • 2
    • 2.0
    • 2 remainder 2
  4. What happens if a line starts with an unexpected space?

    • Python ignores the space
    • Python raises an IndentationError
    • The line becomes a comment
    • The line runs twice
  5. Which file extension do Python programs use?

    • .pt
    • .pyt
    • .python
    • .py