Running Your First Script
Writing a .py file, running it from the terminal, and reading Python's error messages.
2 min read
With Python installed, it's time to move from the interactive shell to an actual file — the way real programs are written and run.
Writing the file
Create a file named hello.py in any folder and add:
print("Hello from Python!")
name = input("What's your name? ")
print("Nice to meet you, " + name + "!")print() writes text to the terminal. input() pauses the program, shows a prompt, and waits for the user to type something and press Enter — whatever they type comes back as a string.
Running it
From a terminal, navigate to the folder containing the file and run:
python hello.py(or python3 hello.py, depending on your system — see the previous lesson). You should see the greeting print, then be prompted for your name, then see the personalized message.
Reading errors
Mistakes are inevitable, and Python's error messages — called tracebacks — are more helpful than they first look. Try running this broken version:
print("Hello"You'll get something like:
File "hello.py", line 1
print("Hello"
^
SyntaxError: '(' was never closed
Read a traceback from the bottom up: the last line names the error type (SyntaxError) and a description. Above that, Python shows you the exact file, line number, and often a ^ pointing at the problem. For a runtime error (one that happens while the program is executing rather than before it starts), the traceback also shows the chain of function calls that led there — useful once your programs are more than a few lines long.
Comments
Anything after a # on a line is ignored by Python entirely — use it to explain why code does something, not to restate what it obviously does:
# Convert to lowercase so the comparison ignores capitalization
name = name.lower()Running code interactively vs. as a script
The interactive shell (python with no filename) is great for quick experiments, but it doesn't save anything once you close it. A .py file is how you build anything meant to be run more than once, shared with someone else, or version-controlled with Git. As this course moves into variables, control flow, and functions, every example is meant to be typed into a file and run with python filename.py — don't just read the code, run it and change it.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.