LearnFrenzy
Sign in Take a free test

Getting Started · Lesson 3 of 39

Your First Python Program

5 min read Updated Nov 7, 2026 Saurabh Samir

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, sep and end
  • 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!")

Output

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")

Output

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)

Output

Price: 499 rupees
10 | 20 | 30
Loading...done

Total: 998
  • sep changes the separator between values (default: one space).
  • end changes 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""")

Output

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

Output

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

Output

You can vote.
Don't forget your ID.
This always prints.

How Python sees the block

  1. if age >= 18:The colon opens a block
  2. 4 spaces → inside the blockRuns only when the condition is true
  3. 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"

Output

  File "main.py", line 1
    print("Hello"
         ^
SyntaxError: '(' was never closed

The three errors you will meet first:

ErrorTypical causeFix
SyntaxErrorMissing bracket, quote or colonCheck the line shown and the line just above it
IndentationErrorSpaces don't line up, or a block is emptyUse 4 spaces consistently; never mix tabs and spaces
NameErrorMisspelt name, e.g. Print or pritnPython is case-sensitive: print is not Print
Print("hi")

Output

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

  1. 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=" - ")

    Output

    Rohan - Pune - Python
  2. Print a small box using three print() calls.

    Show solution
    print("+-------+")
    print("|  Hi!  |")
    print("+-------+")

    Output

    +-------+
    |  Hi!  |
    +-------+
  3. Fix this program: print("Total:" 5 + 5)

    Show solution

    A comma is missing between the two values:

    print("Total:", 5 + 5)

    Output

    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 .py file that runs top to bottom.
  • print() shows values; sep and end control 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.

Next lesson

Search LearnFrenzy

Esc

Quick links