Writing and Running Your First Python Program
The recommended approach is to build a simple version first, test it thoroughly, verify that it works, and then add features incrementally.
From Idea to Working Program
Writing software is more than typing instructions. It is a structured journey from an idea to a working program that solves a problem. In this course, you will repeatedly move through a simple pattern: understand what you want to build, write a small piece of Python, run it, check the result, and then decide what to add next.
The recommended approach is to build a simple version first, test it thoroughly, verify that it works, and then add features incrementally.
Starting small reduces risk. A simple version is easier to test, helps you discover whether your basic approach works, and gives you a stable foundation for later improvements. If the approach is wrong, you have invested less time before correcting it.
The Five Development Phases
The software development process has five phases. Each phase has a different purpose, and using them in order helps you plan work, find problems, and create software that can be used and refined.
| Phase | Purpose |
|---|---|
| What | Analysis: decide what the program should do |
| Do It | Implementation: write the code |
| Test | Testing and Debugging: run the program with test data and check for errors |
| Use | Operation or Deployment: use the program with real data to verify that it works |
| Maintain | Refinement: improve the program based on what you learn |
The five software development phases
For a small Python script, What might mean deciding the problem the script should solve. Do It means writing the script. Test means running it with test data and checking for errors. Use means applying it to real data to ensure it works. Maintain means refining the script based on what you learn.
The Do It-Test-Use Cycle
Although all five phases belong to the complete process, practical programming usually proceeds iteratively. You analyze and design a small part, implement it, test and debug it, use it to verify correctness, and then add a feature. The cycle repeats as the program grows.
Planning a Small Message Program
Apply the five phases to a simple Python program that displays a message.
- 1What
Decide that the program should display a message.
- 2Do It
Write a simple print statement.
- 3Test
Run the statement and check whether the expected message appears.
- 4Use
Use the working program when the message is needed.
- 5Maintain
Refine the program if the message or its behavior needs to change.
The program is developed as a working small version before further features are added.
Where Your Python Runs
Python is a programming language: a formal set of instructions that a computer can understand and execute. In this course, you write Python code in your browser, and the code runs in the cloud on a server. The server's Python interpreter reads and executes the code, then sends the result back to your browser.
| Mode | How code is organized | Useful for |
|---|---|---|
| REPL | Python reads and runs one line at a time, displays the result, and waits for the next line | Experimenting and testing small ideas quickly |
| Script | A saved file contains multiple lines of Python code and runs as a complete program | Writing and running longer programs |
Two ways to run Python code
Writing Versus Running
Writing code means entering a Python instruction or placing it in a script. Running code means sending that instruction to the Python environment so the interpreter can read and execute it. The two actions are connected, but they are not the same action.
In the REPL, the distinction happens quickly: you enter one line, run it, see the result, and then enter another line. In a script, you can write several lines first and run the complete saved file. Both modes use the same Python language; they differ in how code is organized and executed.
Your First Python Statement
A Python statement is a complete instruction. The simplest statement in this lesson is a call to the print function. The function displays the text supplied to it.
Hello, PythonEvery function call follows the pattern function name, parentheses, and input inside the parentheses. In this statement, print is the function name. The parentheses show that the function is being called, and the text inside them is the argument given to the function. When the interpreter runs the statement, the text appears on the screen.
Names and Values
After printing a fixed message, you can give values names. A variable is a name that holds a value. Assignment uses the equals sign to associate a value with a variable name so that you can use the name later.
name = "Alice"
age = 25
print(name)
print(age)- 1The first line gives the name variable the text Alice.
- 2The second line gives the age variable the number 25.
- 3The third line asks Python to display the value stored under name.
- 4The fourth line asks Python to display the value stored under age.
- The variable name is assigned the text Alice.
- The variable age is assigned the number 25.
print(name)displays the value associated with name.print(age)displays the value associated with age.
Alice
25Alice
25When you use a variable name, Python retrieves the value associated with that name. This makes code readable and reusable because you can use a name instead of repeatedly typing the same value.
Mistakes Beginners Make
Common mistake Treating writing code as the same as running code
What it looks likeEntering print("Hello, Python") without sending it to the Python environment
Why it failsAn instruction produces output only when the interpreter reads and executes it.
Do this insteadAfter entering or saving the instruction, run it in the course environment.
Common mistake Trying to build every feature before testing
What it looks likeWriting an entire program before checking whether its basic approach works
Why it failsProblems may be discovered late, after more time and complexity have been added.
Do this insteadBuild a simple version, test it thoroughly, use it, and then add the next feature.
Common mistake Confusing a function name with a function call
What it looks likeWriting print without parentheses when the intention is to display a value
Why it failsParentheses indicate that the function is being called and contain its input.
Do this insteadUse the function name followed by parentheses with the argument inside.
Common mistake Expecting a variable name to display its value without an instruction
What it looks likeCreating age = 25 but not using print(age) when display is needed
Why it failsAssignment associates a value with a name; print is the statement described for displaying a value.
Do this insteadUse print with the variable name when you want the value displayed.
Practice the Full Process
Plan a tiny Python program that displays a message. Describe what you would do in the What, Do It, Test, Use, and Maintain phases. Then write and run a print statement for the first version.
Hints
- Start by identifying the message the program should display.
- Use the print function with the message as its argument.
- Run the statement and compare the displayed result with what you expected.
- Think of one small refinement you could make after the first version works.
What do you think happens?
What output do you expect after running the following statement?
Reveal answer
The text Hello, Python
print is a function call, and the text inside its parentheses is the argument that the function displays.
What to Remember
- 1The five software development phases are What, Do It, Test, Use, and Maintain.
- 2The recommended iterative approach builds a simple version, tests it, uses it to verify correctness, and then adds features.
- 3In this course, Python runs in a cloud-based interpreter through your browser, so no installation is needed.
- 4The REPL runs Python one line at a time, while a script is a saved file containing multiple lines of code.
- 5A print statement contains a function name, parentheses, and an argument; running it makes the argument appear as output.
- 6Writing code and running code are different: writing creates the instruction, while running sends it to the interpreter for execution.
At a glance
- Software development follows five phases: What, Do It, Test, Use, and Maintain.
- The practical development cycle is iterative: implement a small version, test it, use it, and repeat with new features.
- This course sends Python code from your browser to a cloud-based interpreter and displays the result back in the browser.
- Python can run interactively in the REPL or as a complete script file.
- A simple print statement demonstrates how a Python instruction is written, executed, and turned into output.