The fastest way to get comfortable with Python is to write a tiny program, run it, and see what happens. This lesson walks through that loop using a small e-commerce program as the running example. You'll see two different ways to run Python code, write your first script, and learn how to read the error messages you'll inevitably hit.
Open a plain text editor or your IDE and create a file named hello.py. Type this single line:
Save it. Open a terminal, change into the directory that holds hello.py, and run:
That's a complete Python program. One line. No class, no main method, no semicolons, no boilerplate. The print() function writes its argument to the terminal and adds a newline at the end.
A few things to notice before we go further:
print is a built-in, Print is not.The pipeline from your source file to terminal output looks like this:
You give python a file, the interpreter reads each line in order, and anything you print shows up in your terminal. There's no separate compile step, no .class file, no extra command.
You don't always need a file. Python ships with an interactive shell called the REPL, which stands for Read, Eval, Print, Loop. Type an expression, press Enter, and Python evaluates it and shows the result. It's the fastest way to try out small snippets.
Start it by running python with no arguments:
You'll see something like this:
The >>> is the REPL prompt. It means Python is waiting for you to type something. Try a few things:
Notice two things here. First, you can print() exactly like you would in a script. Second, when you type an expression by itself (like 2 + 3 or "Wireless Mouse"), the REPL evaluates it and shows the result, no print() needed. That's the "Print" part of REPL: anything you type that produces a value gets echoed back.
This shortcut only works in the REPL. Inside a .py script, 2 + 3 on its own line does nothing visible. You'd have to write print(2 + 3) to see the result.
To leave the REPL, type exit() and press Enter, or press Ctrl+D (on macOS and Linux) or Ctrl+Z then Enter (on Windows).
The REPL is great for quick experiments: testing what a function returns, checking the type of something, or trying a one-liner before pasting it into a script.
The REPL is fine for one-offs, but real programs live in files. A Python script is just a text file with a .py extension. The naming convention is lowercase with underscores, like hello.py or order_summary.py.
Here's the workflow:
.py.python filename.py.Let's say hello.py contains:
Running it:
A few practical points:
python /Users/you/projects/hello.py.python3 instead of python. If python doesn't work or points to an older version, use python3.This is the main difference from many other languages. There's no required entry-point function, no required class, no boilerplate. A Python file with one line is a complete program.
| Mode | How you start it | When to use |
|---|---|---|
| REPL | python | Quick experiments, exploring a library, testing a one-liner |
| Script | python file.py | Anything you want to save, share, or run more than once |
Cost: Starting the Python interpreter takes a moment (typically 30-100ms). For tiny snippets you'd run hundreds of times, prefer the REPL or a long-running process. For everything else, a script is fine.
One line of output is fine, but real programs do something with data. Here's a slightly bigger example: a tiny order summary that prints the contents of a customer's cart.
Create a file named order_summary.py:
Run it:
Walking through it line by line:
product_name, quantity, and price. For now think of = as "give this name the value on the right".total = quantity * price multiplies the quantity by the price and stores the result in total. The arithmetic happens before the assignment.print("Order Summary") writes the header line.print(product_name, "x", quantity) is the interesting one. print() can take multiple arguments, separated by commas. It prints them in order with a single space between each. So Wireless Mouse, then a space, then x, then a space, then 2.print("Total:", total) works the same way. It prints Total:, a space, then the number stored in total.That default space between arguments is the value of print()'s sep parameter, which is " " (a single space) unless you override it. You can pass sep="|" or sep="" if you want something else.
Each print() call also adds a newline at the end. That's why each piece of output appears on its own line. That newline is controlled by print()'s end parameter, which defaults to "\n". If you pass end="", the next print() continues on the same line.
You don't need semicolons between statements. The end of a line is the end of a statement. You also don't need to declare the types of product_name, quantity, or price. Python figures out that product_name is text, quantity is a whole number, and price is a decimal number from the values you assign.
You will make typos. Everyone does. The good news is that Python's error messages are usually short and point right at the problem. Learning to read them saves a lot of time. Here are three you'll meet in your first week.
NameError from a typoWhat's wrong with this code?
Python prints:
The traceback tells you exactly what happened. Line 1 of hello.py uses the name Print, but Python doesn't know what Print is. The built-in function is print with a lowercase p. Python is case-sensitive, so Print, PRINT, and print are three different names.
Fix: Use lowercase print:
NameError: name 'X' is not defined almost always means a typo in a name or a variable you haven't created yet. Read the line, find the name Python is pointing at, and check the spelling and capitalization.
SyntaxError from missing parenthesesWhat's wrong with this code?
This is what Python 2 used to allow, but Python 3 requires parentheses around the argument. Python reports:
The error happens before any code runs. Python tries to parse the file and gives up because the syntax is wrong. The Did you mean print(...)? hint is the interpreter trying to help.
Fix: Wrap the argument in parentheses:
A SyntaxError always means the file doesn't follow Python's grammar. None of your code runs until the whole file parses successfully.
IndentationError from a stray spacePython uses indentation to group code, so leading whitespace is significant. A line that's indented when it shouldn't be triggers an error.
What's wrong with this code?
The second line starts with four spaces for no reason. Python reports:
Fix: Remove the leading spaces so both lines start at the same column:
You'll meet IndentationError more often once you start writing functions, loops, and if statements, since those rely on indentation to mark their bodies. For now, the rule is simple: top-level lines start at column zero, with no leading spaces or tabs.
You'll see lines starting with # in many Python examples. Anything from # to the end of the line is a comment. Python ignores it.
Use comments sparingly, mostly to explain why something is happening, not what. The next section covers comments and docstrings in more detail.
One more piece of trivia worth knowing: on Unix-like systems (macOS, Linux), the very first line of a script can be a shebang like #!/usr/bin/env python3. It tells the operating system which interpreter to use when the script is run directly as an executable (e.g., ./hello.py). It's optional, doesn't affect python hello.py, and isn't needed on Windows. You'll see it in older codebases and command-line tools, but you can ignore it for the rest of this course.
10 quizzes