Getting Started · Lesson 3 of 39
Your First Python Program
Time to write real code. In this lesson you will create a Python file, run it, print text in a few different ways, add comments and – just as important – learn to read the error messages Python shows when something goes wrong.
In this lesson
- Create and run
hello.py - Use
print()with several values,sepandend - Write comments and understand indentation
- Read a traceback and fix common errors
Hello, World!
In your python-course folder create a file called hello.py and type:
print("Hello, World!")
Hello, World!
Run it with the ▷ Run button in VS Code, or from the terminal:
python hello.py
That single line is a complete program. print is a function – a named piece of code that does a job. The brackets hold what you give it, and the quotes mark Hello, World! as text (a string).
How a script runs: top to bottom
Python reads your file from the first line to the last, running one statement at a time. Nothing is skipped unless you tell it to.
print("Step 1: wake up")
print("Step 2: open laptop")
print("Step 3: learn Python")
Step 1: wake up Step 2: open laptop Step 3: learn Python
Getting more out of print()
You can pass several values separated by commas. Python puts a space between them and a new line at the end:
print("Price:", 499, "rupees")
print(10, 20, 30, sep=" | ")
print("Loading", end="...")
print("done")
print() # an empty line
print("Total:", 2 * 499)
Price: 499 rupees 10 | 20 | 30 Loading...done Total: 998
sepchanges the separator between values (default: one space).endchanges what is printed at the end (default: a new line\n).- Numbers do not need quotes, and Python can calculate inside
print().
Quotes: single, double and triple
print('Single quotes work')
print("So do double quotes – handy for it's and don't")
print("""Triple quotes
span several
lines""")
Single quotes work So do double quotes – handy for it's and don't Triple quotes span several lines
Comments: notes for humans
A # starts a comment. Python ignores everything after it on that line. Use comments to explain why you did something, not to repeat what the code obviously does.
# Calculate the bill for a group dinner
total = 2400
people = 4
print(total / people) # each person's share
600.0
Indentation is part of the language
Most languages use braces { } to group lines. Python uses indentation – the spaces at the start of a line. Lines indented under a statement ending in : belong to it. The standard is 4 spaces (VS Code inserts them when you press Tab).
age = 20
if age >= 18:
print("You can vote.") # inside the if
print("Don't forget your ID.")
print("This always prints.") # back outside
You can vote. Don't forget your ID. This always prints.
How Python sees the block
- if age >= 18:The colon opens a block
- 4 spaces → inside the blockRuns only when the condition is true
- Back to column 0 → block endsRuns every time
Reading error messages
Every programmer sees errors all day; the skill is reading them calmly. Python prints a traceback. Read it from the bottom up: the last line names the error, the lines above tell you where it happened.
print("Hello"
File "main.py", line 1
print("Hello"
^
SyntaxError: '(' was never closedThe three errors you will meet first:
| Error | Typical cause | Fix |
|---|---|---|
SyntaxError | Missing bracket, quote or colon | Check the line shown and the line just above it |
IndentationError | Spaces don't line up, or a block is empty | Use 4 spaces consistently; never mix tabs and spaces |
NameError | Misspelt name, e.g. Print or pritn | Python is case-sensitive: print is not Print |
Print("hi")
Traceback (most recent call last):
File "main.py", line 1, in <module>
Print("hi")
NameError: name 'Print' is not defined. Did you mean: 'print'?Tip
Modern Python versions add hints such as Did you mean: 'print'? Read the whole last line – very often it tells you the fix.
Practice
Try it yourself
Print your name, city and favourite language on one line, separated by a dash with spaces around it.
Show solution
print("Rohan", "Pune", "Python", sep=" - ")Rohan - Pune - Python
Print a small box using three
print()calls.Show solution
print("+-------+") print("| Hi! |") print("+-------+")+-------+ | Hi! | +-------+
Fix this program:
print("Total:" 5 + 5)Show solution
A comma is missing between the two values:
print("Total:", 5 + 5)Total: 10
Quick check
What does print("a", "b", sep="") print?
ab – the separator is an empty string.
Why is indentation important in Python?
It defines which lines belong to a block (an if, a loop, a function). Wrong indentation changes the meaning or causes an IndentationError.
Where should you start reading a traceback?
At the bottom: the last line names the error and usually explains it.
Summary
- A Python program is a
.pyfile that runs top to bottom. print()shows values;sepandendcontrol spacing.#starts a comment; indentation (4 spaces) defines blocks.- Read errors from the bottom line up – they usually point to the fix.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.