Writing Your First Python Program
Python is case-sensitive: print and Print are different, and only print is the built-in output function.
From Instruction to Output
A Python program is a set of instructions that the Python interpreter can read and execute. In this course, you write those instructions in your browser and run them in a cloud-based Python environment. You do not need to install Python or configure your computer before trying your first statement.
The first instruction is deliberately small: call the print function with a message. The interpreter reads the instruction, executes print with the text you provide, and sends the resulting output back to your browser.
Hello, PythonA Python statement is a complete instruction. A call to print is one of the simplest Python statements.
The Course Execution Path
Writing code and running code are separate actions. Writing means placing Python instructions in the course editor. Running means pressing Run so the code is sent to the cloud server, read and executed by the Python interpreter there, and returned to your browser as output.
| Mode | What happens | Useful for |
|---|---|---|
| REPL | Python reads and runs one line, displays the result, and waits for the next line. | Experimenting and testing small ideas quickly |
| Complete script | Python runs a saved file containing multiple lines of code. | Organizing and executing longer programs |
Anatomy of a Print Call
A function call follows a simple pattern: the function name comes first, followed by parentheses. The input goes inside the parentheses; this input is called an argument. The parentheses tell Python that you are calling the function rather than merely mentioning its name.
| Part | Role |
|---|---|
| The name of the built-in output function | |
| ( ) | The parentheses that mark a function call |
| "Hello, Python" | The text supplied as the argument |
| Output | The text displayed after the interpreter executes the call |
The main parts of a simple print instruction.
Reading a Simple Instruction
Determine what happens when Python runs print("Hello, Python").
Find the function name: The name print identifies the built-in function that displays output.
Read the parentheses: The parentheses show that print is being called.
Read the argument: The text Hello, Python is supplied as the function's input.
Observe the result: The interpreter executes the call and the text appears on the screen.
The output is Hello, Python.
Names and Capitalization
Python is case-sensitive. That means uppercase and lowercase letters are significant when Python reads a name. The name print and the name Print are different names. Only print is the built-in output function described in this lesson.
The first line uses the exact built-in name print. The second line uses Print, which is a different name because its first letter is uppercase. If Python has no definition for that different name, the line can produce a NameError. A person may understand that the two names look related, but the interpreter follows the exact spelling and capitalization.
What do you think happens?
Which line uses the built-in output function?
Reveal answer
Answer: print("Hello, Python")
Python treats print and Print as different names, and only print is the built-in output function described in this lesson.
Whitespace and Code Structure
Leading whitespace means spaces or tabs placed at the beginning of a line. At the top level, a Python statement must begin at column 1. Adding leading spaces or tabs to a top-level statement causes an IndentationError.
Indentation is not decoration in Python. It makes code structure visible and helps show which statements belong together. At the top level, however, an accidental space or Tab before a statement is an error rather than a harmless visual change.
The second line has leading whitespace at the top level and causes an IndentationError.A Reliable Debugging Check
Changing the capitalization of print
Python treats Print as a different name from print, and only print is the built-in output function.
Fix:
Use the exact lowercase name print.Leaving an accidental space or Tab before a top-level statement
Top-level statements must start at column 1. Leading whitespace causes an IndentationError.
Fix:
Remove all leading spaces and tabs from the line.Assuming a human reader will infer the intended name
Python follows exact naming rules rather than interpreting what a human reader probably meant.
Fix:
Check the spelling and capitalization of the name character by character.
When a simple program fails, first read the error message and inspect the line it identifies. For a NameError, check the exact spelling and capitalization of function and variable names. For an IndentationError, inspect the beginning of the line for unwanted spaces or tabs.
Python's strictness serves a purpose. Case-sensitivity lets names such as score and Score remain distinct. Indentation makes code structure visible. Together, these rules prevent code from having multiple possible interpretations and help ensure that a program means one clear thing rather than depending on a reader's guess.
Practice in the Environment
Run a statement that displays the text Welcome to Python. Then deliberately test the two common mistakes: change print to Print, and add one leading space before print. Observe which error appears for each change, and restore the working statement.
Hints
- The working function name is lowercase print.
- A top-level statement should begin at column 1.
- Use the error message to identify the line that needs inspection.
Write a second print statement with a different message. Run both statements in the REPL and then consider how the same lines would be organized if they were saved as a complete script.
Hints
- Each print call is a complete Python statement.
- The REPL executes a line at a time.
- A script can contain multiple lines that are run as a complete file.
What You Can Now Do
- Run Python code in this course through a browser-based, cloud-based interpreter without installing Python.
- Recognize a simple print call as a statement made from a function name, parentheses, and an argument.
- Remember that print and Print are different names because Python is case-sensitive.
- Keep top-level statements at column 1; accidental leading spaces or tabs cause an IndentationError.
- Use NameError and IndentationError messages as clues when checking capitalization and whitespace.
- Understand that strict naming and indentation rules make Python code readable and unambiguous.
Key Takeaways
- Python runs formal instructions through an interpreter; in this course, the interpreter runs on a cloud server and returns output to your browser.
- A print statement uses a function name, parentheses, and an argument to display text.
- Python is case-sensitive, so print and Print are different names.
- Leading whitespace at the start of a top-level line causes an IndentationError.
- Checking exact capitalization and leading whitespace is a practical first step when debugging simple Python programs.