Skip to content

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.

Beginner 15 min read·Lesson 11 of 306 in Python Programming

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

Python
print(value1, value2, sep=" ", end="\n")
  • 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

Python
print("Course:", "Python", "Basics")
Output
Course: Python Basics

Changing sep and end

Python
print("2026", "11", "22", sep="-")
print("Loading", end="...")
print("done")
Output
2026-11-22
Loading...done

More examples

first_program.py

Python
# My first Python program
name = "Asha"
lessons_done = 3
total_lessons = 14
percent = lessons_done / total_lessons * 100
print("Welcome,", name + "!")
print("You have finished", lessons_done, "of", total_lessons, "lessons.")
print(f"That is {percent:.1f}% of Module 1.")
Output
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:

TextOutput
Welcome, Asha!
You have finished 3 of 14 lessons.
That is 21.4% of Module 1.

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

print(total)
total = 10

Prefer

total = 10
print(total)

Python runs top to bottom. On the first line total doesn’t exist yet, so you get a NameError.

Mismatched quotes

Avoid

print("Hello')

Prefer

print("Hello")

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

  1. 1
    Change first_program.py to use your own name and progress numbers.
  2. 2
    Print 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?
Practise this topic