Learn / Beginner / Lesson 2

Your first program

8 min Tested on Python 3.15Updated By
In short

A Python program is a text file ending in .py that Python runs from top to bottom, one line at a time. The print() function shows output, # starts a comment that Python ignores, and you run a file with python hello.py.

Python
print("Hello, world!")
print("Python runs", "line by line")
print()
print("The empty print() above made a blank line")
Output
Hello, world!
Python runs line by line

The empty print() above made a blank line

Give print() several values separated by commas and it puts a space between them. Text in quotes is called a string — you'll learn more about strings soon.

Comments

Anything after # on a line is a comment. Python skips it. Use comments to explain why the code does something, not what every line does.

Python
# Greet the user
print("Welcome!")  # comments can also follow code
Output
Welcome!

Running a .py file

  • Create a file called hello.py in any editor — VS Code and PyCharm are popular and free.
  • Write print("Hello, world!") in it and save.
  • Open a terminal in the same folder and run python hello.py (or python3 hello.py).

The interactive shell

Type python on its own to open the interactive shell, shown by >>>. It runs each line as soon as you press Enter — perfect for quick experiments. Type exit() to leave.

terminal
>>> 10 * 3
30
>>> print("hi")
hi

Your first error

Errors are normal — every programmer sees them all day. Run this and read the last line of the message.

Python
print("Missing a closing bracket"

Practice

Print a three-line greeting card: Happy birthday!, then an empty line, then From Python.

Expected output:
Happy birthday!

From Python
Your code

Quick check

1. What does Python do with a line that starts with #?

2. What does print("a", "b") show?

3. Where should you look first in an error message?

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →