Your first program
Understand what a program is, run a Python file, and distinguish code from its output.
On this page
The concept
A program is a sequence of instructions that a Python interpreter executes. A .py file stores those instructions; saving the file does not execute it. The terminal runs commands such as python3 lesson.py. The Python interactive prompt, shown as >>>, accepts Python expressions instead. Do not type a terminal command at that prompt.
print(...) displays a value. Text surrounded by quotes is a string: Python displays its contents rather than treating the words as instructions. An unquoted expression such as 2 + 3 is evaluated before it is printed. This small distinction explains why a program can display either a calculation or the literal text of a calculation.
Worked example
print("Hello, Python!")
print("This is my first program.")
print(2 + 3)Trace the result
- The first print call displays Hello, Python!; the quotation marks delimit the string and are not part of the displayed message.
- The second call is another instruction, executed after the first. Swapping these lines changes the order of the messages.
- The final call evaluates 2 + 3 and displays 5. Replace it temporarily with print("2 + 3") to observe the difference between data and an expression.
Hello, Python!
This is my first program.
5Line-by-line walkthrough
Line 1: print("Hello, Python!")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 2: print("This is my first program.")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 3: print(2 + 3)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
File-not-found errors usually mean the terminal is in the wrong directory or the path is misspelled. Check the path before changing the Python code.