Write your first
Python script.
One file. Five lines. Real output. By the end of this page you'll have run actual Python code, saved it to a file, executed that file from the terminal, and understood every character you typed. No prior programming experience needed.
Your first Python script is one line: print("Hello, world"). Save it as hello.py, open a terminal in that folder, and run python hello.py (or python3 hello.py on Mac/Linux). That's it — you're programming. Everything else in Python is built on this same pattern: write code in a file, run the file, see the output.
Before this lesson: Python must be installed and working. If python --version shows an error, complete the install guide first — everything below will fail without it.
▶️ Step 1: Run one line of Python
No file yet. Just the interpreter. This proves Python works.
Open a terminal
Windows: press Win + R, type cmd, press Enter.
Mac: press Cmd + Space, type "Terminal", press Enter.
Linux: press Ctrl + Alt + T.
Start the Python interpreter
Type this and press Enter. On Mac/Linux use python3 instead of python.
python
Python 3.12.4 (main, Jun 6 2024, 12:00:00)
[GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
You'll see >>> — that's the Python prompt. It means Python is listening. Type Python here and it runs immediately.
Print something
Type this at the >>> prompt and press Enter:
print("Hello, world")
Hello, world
That's your first line of Python. print is a function — it takes a value and displays it. The quotes mark a string, which is just text.
Exit the interpreter
Type exit() and press Enter. Or press Ctrl + D (Mac/Linux) / Ctrl + Z then Enter (Windows).
The REPL is for quick tests, not real code. Anything you type in the >>> prompt is gone when you exit. For real scripts, you save code to a file — that's the next step.
📄 Step 2: Save it to a file and run it
This is how real Python code works — in a file, run from the terminal.
Create a folder for your project
# Anywhere you want — Desktop, Documents, whatever
mkdir python-practice
cd python-practice
Create a file called hello.py
Any text editor works — Notepad, TextEdit, VS Code, whatever you have. Open it, type the code below, save the file as hello.py inside the python-practice folder.
Watch out: on Windows, Notepad adds .txt to the end of your filename. Save as hello.py and set "Save as type" to "All Files" to prevent hello.py.txt. If your file ends in .txt, Python will not find it.
print("Hello, world")
Run the file
Make sure you're in the python-practice folder (the cd in step 1 put you there). Run:
# Windows
python hello.py
# Mac / Linux
python3 hello.py
Hello, world
That's it. You just ran a Python program. The print function executed, the string was displayed, the program finished.
Why not just double-click the file? On Windows, double-clicking runs the script in a window that closes as soon as the program finishes — you see a flash and nothing else. Always run Python from the terminal so you see the output. This is the single most common confusion for absolute beginners.
🏷️ Step 3: Variables and strings
The next 5 lines you'll write. Variables give names to values so you can reuse them.
A variable is a name that points to a value. In Python you create one with name = value. No type declaration, no var, no let — just assign and go.
# A variable holding a string
name = "Alice"
# A variable holding a number
age = 25
# Use them in an f-string (the "f" prefix enables {} interpolation)
print(f"Hello, {name}!")
print(f"You are {age} years old.")
# A f-string can also do math inline
print(f"In 10 years you will be {age + 10}.")
Save the file and run it again with python hello.py (or python3 hello.py). Output:
Hello, Alice!
You are 25 years old.
In 10 years you will be 35.
Strings
Text surrounded by quotes. Single 'like this' or double "like this" — both work, just be consistent.
Numbers
Integers have no decimal point (25). Floats do (3.14). Python figures out the type automatically.
f-strings
Prefix a string with f and wrap variables in {} to embed them. Fastest and cleanest way to format strings.
Comments
Lines starting with # are ignored by Python. Write them for the human reading the code — including future-you.
🔍 What just happened
Why the code did what it did — no magic.
Python read the file top to bottom
Unlike HTML which gets parsed into a tree, Python executes line by line from top to bottom. Line 1 runs, then line 2, then line 3. When it hits the bottom, the program ends. That's the entire execution model — no hidden entry point, no main() required.
print() wrote to stdout
Each print() call writes its arguments to standard output (stdout), which is displayed in your terminal. It also adds a newline at the end automatically, which is why each print appears on its own line. If you want no newline, use print("x", end="").
Variables lived in memory during execution
name and age were created in memory when their assignment lines ran. They existed until the program ended, then were gone. Nothing is saved to disk — variables live only as long as the program runs.
The f-string was built before printing
When Python sees f"Hello, {name}!", it evaluates the expression inside {}, substitutes the value, and produces a new string "Hello, Alice!". Only then does print() receive it. The f-string is a shortcut — the same result would come from "Hello, " + name + "!", but the f-string is cleaner.
🏆 Challenge
One small task. Do it in your own file, not the browser.
Your turn — 2 minutes
Modify your hello.py to do this:
1. Create variables for your first_name, favorite_language, and years_coding (a number).
2. Print three sentences using f-strings — something like "My name is ...", "I love coding in ...", and "I've been coding for ... years."
3. Add a comment above each print explaining what it does.
Save the file. Run it. Fix any errors that appear — that's the whole point. If nothing breaks, make it break on purpose: what does print(f"I have {years_coding + "many"} years") do? Read the error. It tells you exactly what's wrong.
⚠️ Common mistakes (right now)
The errors you'll hit in the next 24 hours, and how to fix them.
Python isn't on your PATH. On Windows, you didn't check "Add Python to PATH" during install. See the install guide — error #1 explains the 30-second fix.
Windows Notepad adds .txt when you save as hello.py unless you set "Save as type" to "All Files". Check with dir hello*. If the file is .txt, rename it or resave it properly.
Usually a missing colon, quote, or parenthesis. Python points to the line with SyntaxError. Check that line and the one before it — an unterminated string on line 2 will report the error on line 3.
You used a variable before defining it, or misspelled it. name = "Alice" creates the variable name, not Name or alice. Python is case-sensitive.
You did "age: " + age where age is an integer. Fix with an f-string: f"age: {age}". The error message is telling you exactly what went wrong — read it.
You're running python hello.py from a folder that doesn't contain the file, and Python is silently running an older hello.py somewhere else. Fix: run pwd (Mac/Linux) or cd (Windows) to see where you are, then dir or ls to confirm the file is there.
You added leading spaces at the start of a line that shouldn't have them. At the top level of a script, lines start at column 0. Indentation matters inside functions, loops, and conditionals — not at the top.
➡️ What's next
You've written your first script. Here's the natural next step.
❓ Frequently asked questions
How do I run a Python script?
Open a terminal, navigate to the folder containing your .py file, and run 'python filename.py' on Windows or 'python3 filename.py' on Mac/Linux. The output appears in the terminal. You can also use an IDE like VS Code with the Python extension and click the Run button.
What is the difference between print() and return?
print() displays a value on screen for the user to see. return sends a value back from a function to the code that called it. print() is for output; return is for passing data. Inside a function, using print() instead of return is one of the most common beginner mistakes.
Do I need a .py extension for Python files?
Yes. Python files must end in .py for the interpreter to recognise them. You can run a file without the extension using 'python myfile' but the convention is always .py. This is also how editors know to apply Python syntax highlighting.
Why does my Python script close immediately on Windows?
Double-clicking a .py file on Windows runs it in a window that closes as soon as the script finishes. Open Command Prompt or PowerShell instead, navigate to the folder, and run 'python filename.py'. The output stays visible. This is why we always run scripts from the terminal.
What is a variable in Python?
A variable is a name that points to a value in memory. In Python you create one with 'name = value' — no type declaration needed. The type is inferred from the value. You can reassign a variable to a value of a different type at any time, though it's usually a mistake to do so.
What does f-string mean in Python?
An f-string is a string prefixed with 'f' that lets you embed expressions inside curly braces. 'f"Hello, {name}"' produces 'Hello, Alice' if name is 'Alice'. F-strings were added in Python 3.6 and are the modern way to format strings. They are faster and more readable than .format() or concatenation.
What is the difference between an integer and a float?
An integer is a whole number with no decimal point (5, -3, 0). A float has a decimal point (5.0, 3.14, -0.5). In Python, 5 and 5.0 are different types. Division with '/' always returns a float, even when the result is a whole number. Use '//' for integer division.
Why does print('hello' + 5) fail?
Because you can't concatenate a string and an integer with '+'. Python raises a TypeError. Fix it by converting: print('hello' + str(5)) or use an f-string: print(f'hello {5}'). The type error is Python protecting you from silent bugs — languages that auto-convert are hiding problems.