Module 1 · Introduction to Python — topic 11 of 14
Writing your first Python program
Write, save and run a complete first program that greets the user, does a calculation and prints a formatted result, and learn the edit–run–fix cycle.
Learning objectives
- Write a multi-line Python program in a file
- Use print() with several values
- Practise the edit–run–fix cycle
Definition
Write, save and run a complete first program that greets the user, does a calculation and prints a formatted result, and learn the edit–run–fix cycle.
Introduction
Your first program is a milestone: a file that you wrote, that the computer runs, and that produces the result you planned. It will be short, but it uses the same steps as every larger program.
Create a file called first_program.py in your course folder and type the examples yourself rather than copying them. Typing builds the habit of noticing details such as quotes and brackets.
Why it matters
Every program, however large, is built from small, tested steps. Learning to write a few lines, run them and fix any problem quickly is the core skill you will use throughout the course.
Detailed explanation
Python runs a file from top to bottom, one statement at a time. This is called sequential execution. The order of lines matters: a value must be created before it is used.
print() can take several values separated by commas. It puts a single space between them and moves to a new line at the end. Two optional settings change this: sep sets the separator and end sets what is printed at the end.
If something goes wrong, Python stops and prints an error message called a traceback. Read the last line first: it names the error and usually explains the problem.
Syntax
- value1, value2: any number of values to display.
- sep: text placed between values (a space by default).
- end: text printed after the last value (a new line by default).
Code examples
Printing several values
Course: Python Basics
Changing sep and end
2026-11-22 Loading...done
More examples
first_program.py
Welcome, Asha! You have finished 3 of 14 lessons. That is 21.4% of Module 1.
Step-by-step explanation
- The first line starts with #, so it is a comment that Python ignores.
- Three variables store the name and two numbers.
- percent divides and multiplies by 100; Python follows normal maths order.
- name + "!" joins two strings with no space, so the exclamation mark sits next to the name.
- The f-string inserts percent rounded to one decimal place.
Expected output
Running “first_program.py” prints exactly:
Practical applications
- Command-line tools print progress messages exactly like this.
- Scripts that generate reports start by computing values and printing a summary.
- Developers add temporary print lines to check values while fixing bugs.
Best practices
- Start each file with a short comment saying what it does.
- Run the program after every few lines you add.
- Read error messages from the last line upwards.
Common mistakes
Using a variable before creating it
Avoid
Prefer
Python runs top to bottom. On the first line total doesn’t exist yet, so you get a NameError.
Mismatched quotes
Avoid
Prefer
A string must start and end with the same kind of quote; otherwise Python reports a SyntaxError.
Key takeaways
- Python runs a file from top to bottom.
- print() accepts several values and the sep and end options.
- Fix errors by reading the traceback’s last line first.
Try it yourself
- 1Change first_program.py to use your own name and progress numbers.
- 2Print today’s date as day/month/year using three separate values and sep="/".
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?