PYTHON / GETTING STARTED
Run your first Python program
Save Python code in a .py file, run it from a terminal, and predict its output from top-to-bottom execution.
What you will learn
- Create a .py file and run it with python hello.py from the right directory
- Trace execution order: the interpreter runs statements top to bottom, once
- Know that only print() produces visible output in a script file
- Pass arguments on the command line and read them with sys.argv
Understanding Run your first Python program
A Python program is nothing more than a plain text file whose name ends in .py. When you type python hello.py, the interpreter opens that file, translates the whole text into bytecode, then executes the resulting statements one after another from the first line to the last. Nothing is compiled to a separate binary you keep, and there is no build step to remember: the file you edit is the program you run.
Because the entire file is parsed before any of it runs, a punctuation error on the last line stops the first line from ever printing. This is the difference between compile time and run time in Python, and it explains an outcome beginners find surprising: a script can produce zero output even though its first statement looks fine. Errors that depend on values, such as dividing by zero, behave differently, since those only surface when execution actually reaches that statement.
Running a file is not the same as typing at a prompt. A file never echoes results, so a line containing only 2 + 2 computes four and throws it away silently. If you want to see something, you must ask for it with print(). The other half of running a program is location: python hello.py means "the file hello.py in my current directory", so the shell's working directory decides whether the interpreter finds your file at all.
print("first line runs first")
message = "saved to a file, then executed"
print(message)
print("last line runs last")A .py file is a script the interpreter parses in full and then executes top to bottom, showing only what you explicitly print.
Worked examples
A script does not echo results
Shows that only print() produces output when code runs from a file.
2 + 2
print(2 + 2)
total = 10 * 3
print(total)Example explained
Line 1Line 1 is a valid statement: the sum is computed and the value 4 is discarded, so nothing appears.
Line 2Line 2 passes the same value to print(), which writes it to standard output followed by a newline.
Line 3Line 3 binds 30 to the name total; an assignment never displays anything by itself.
Line 4Line 4 prints the stored value, so the whole script produces exactly two lines.
Arguments from the command line
Demonstrates how words typed after the filename reach the running program, assuming you invoke it as python greet.py Ada 42.
import sys
print("script path:", sys.argv[0])
print("first argument:", sys.argv[1])
print("all extras:", sys.argv[1:])Example explained
Line 1sys.argv[0] holds the path exactly as you typed it, which is why it prints greet.py and not an absolute path.
Line 2sys.argv[1] is the first word after the filename, so the program can react to input without asking a question.
Line 3sys.argv[1:] is a list of the remaining words, and 42 appears quoted because arguments always arrive as strings.
Line 4Running the file with no arguments would fail at sys.argv[1], since that position does not exist.
Important notes
On systems that ship both interpreters, python may point to Python 2 or to nothing at all; if python hello.py fails with a command-not-found message, use python3.
Saving the file while it is running has no effect on the current run; Python reads the source once at startup, so you must run the command again.
Common mistakes
Running python hello.py from a different directory than the file, which produces "can't open file 'hello.py': [Errno 2] No such file or directory" instead of any Python error.
Naming the file random.py, math.py, or json.py, so a later import in the same folder picks up your own file and the standard library module becomes unreachable.
Expecting a bare expression such as name.upper() on its own line to display something; the value is computed and dropped, and the script looks broken because it prints nothing.
Try it yourself
Change, predict, then run
Write a three-line script that stores your birth year in a variable, then prints one sentence containing that variable and your age computed with subtraction. Predict the exact output line before you run it, then run it and compare.
Open the Python workspaceCheck your understanding
A file's line 1 is print("start") and its line 3 is missing a closing parenthesis. What happens when you run the file?
- Nothing is printed; the interpreter reports a syntax error before executing any line
- start is printed, then the syntax error is reported when execution reaches line 3
- start is printed and line 3 is skipped, since Python ignores lines it cannot parse
- The file runs normally because Python adds the missing parenthesis at the end of the line
Show answer
Python parses the whole file into bytecode before running a single statement, so an unbalanced parenthesis anywhere prevents all execution. Option two is tempting because that is how runtime errors behave, such as dividing by zero, but those are detected while the code is executing rather than while it is being parsed.