BeginnerLesson 1 of 305 min read

What is Python? Install it and write your first program

Learn what Python is, install it, run your first program with print, use comments and understand indentation and error messages.

In this lesson you will

  • Explain what Python is used for
  • Install Python and run a file
  • Use print and comments
  • Read a simple error message

About 15 minutes

The friendliest way to start programming

Imagine you could give instructions to a computer in something very close to plain English. print("Good morning") shows a greeting. if age >= 18: makes a decision. Python is that language. It was created in 1991 with one idea at its heart: code should be easy to read. That is why schools teach it, why scientists use it, and why companies such as Google, Netflix, Instagram and NASA rely on it.

The same language that prints a greeting on your first day can later analyse a million rows of sales data, run a website, automate boring office work, or train an artificial intelligence. You learn the basics once, and they carry you into all of those worlds.

What people build with Python

  • Automation: renaming thousands of files, filling forms, sending reports.
  • Data analysis: spreadsheets on steroids with libraries such as pandas.
  • Websites and APIs: with frameworks such as Django and Flask.
  • Artificial intelligence: nearly all modern machine learning tools are Python first.
  • Games, scripts, tools: small programs that save you time every day.

Setting up Python

  1. Go to python.org/downloads and download the latest Python 3 for your system.
  2. On Windows, tick “Add python.exe to PATH” on the first installer screen. This single checkbox prevents the most common beginner problem.
  3. Open a terminal (on Windows, search for “Command Prompt” or “PowerShell”) and type python --version. You should see something like Python 3.13.1. On Mac or Linux the command may be python3 --version.
  4. Install Visual Studio Code (free) and its Python extension. Any text editor works, but VS Code highlights errors and runs your files with one click.
  5. Create a folder for your practice files, for example python-practice.
Good to know

You can also practise without installing anything, using a free online Python runner such as the one on python.org (“Launch interactive shell”) or Google Colab. The code in this course works the same everywhere.

Your first program

Create a file called hello.py and type this single line:

print("Hello, world!")
Hello, world!

Save the file, open your terminal in that folder, and run it:

python hello.py

Let us read the line. print is a built-in function, a ready-made action that shows something on the screen. The brackets hold what you want it to work on, called the argument. The text inside the quotes is a string, which is Python’s word for text. Python runs a file from the top to the bottom, one line at a time.

Printing more than one thing

print("Learning Python")
print("It is friendly")
print("Line", "after", "line")
print(2026)
print(7 + 3)
print("7 + 3 =", 7 + 3)
Learning Python
It is friendly
Line after line
2026
10
7 + 3 = 10

Each print starts a new line. If you give it several items separated by commas, it prints them with a space between. Numbers need no quotes, and Python can even do the maths for you: 7 + 3 becomes 10. Compare the last two lines. The first prints the answer. The second prints your words and the answer together.

Practice

Write a program that prints your name on one line, your city on the next, and the result of 12 multiplied by 12 on the third line. Use * for multiplication.

Show one possible answer
print("Sara")
print("Lahore")
print(12 * 12)
Sara
Lahore
144

The interactive shell: a calculator that talks back

Type just python in the terminal and you enter the interactive shell, shown by the >>> prompt. Here you can type a line and see the answer immediately, without making a file. It is wonderful for experiments.

>>> 5 * 6
30
>>> "Python" + " rocks"
'Python rocks'
>>> 10 / 4
2.5
>>> exit()

Comments: notes for humans

# This line is a comment. Python ignores it.
print("Visible")   # A comment can follow code

# print("This line is switched off")
print("Done")
Visible
Done

Anything after a # is ignored. Use comments to explain why you did something, and to switch lines off while testing.

Indentation: the rule that makes Python Python

In many languages, curly brackets group code together. Python uses indentation, the spaces at the start of a line. Lines that belong together are indented by the same amount (four spaces is the standard). You will see this when we meet if and loops. For now, remember: do not indent a line unless Python expects it.

print("This works")
    print("This line is indented for no reason")
Traceback (most recent call last):
  ...
IndentationError: unexpected indent

Reading error messages

Every programmer sees errors daily, so learn to read them calmly. The last line tells you the kind of error and the problem. The lines above it point to the file and line number.

print("Start")
prnt("Typo here")
Start
Traceback (most recent call last):
  ...
NameError: name 'prnt' is not defined. Did you mean: 'print'?

Here Python says NameError: it does not know any name called prnt. The fix is simply to correct the spelling. Most beginner errors are typos, a missing quote or bracket, or wrong indentation.

Python is case-sensitive

print works but Print does not. Names, spelling and capital letters must match exactly. This is the other common cause of mysterious errors.

A good habit from day one

  • Type the code yourself instead of copying and pasting. Your fingers learn too.
  • Run your code after every few lines, not at the end.
  • Change a value and predict what will happen before you run it.
  • When you get an error, read the last line first.
Common mistake

Using curly “smart” quotes copied from a word processor or a web page. Python only understands straight quotes: " and '. If code you pasted fails with a strange error about a character, look at the quotes.

Run, tweak, repeat

Learning to program is mostly experimenting. After every example in this course, change a number or a word and run it again. Breaking things safely is how you understand them.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does print("Hi") do?

  1. Deletes Hi
  2. Shows Hi on the screen
  3. Saves Hi to a file
  4. Prints on paper

print displays its argument.

How do you write a comment in Python?

  1. // comment
  2. <!-- comment -->
  3. ** comment **
  4. # comment

A hash sign starts a comment.

What groups lines of code together in Python?

  1. Indentation
  2. Curly brackets
  3. Semicolons
  4. Parentheses

Python uses indentation for blocks.

What kind of error does prnt("Hi") cause?

  1. ZeroDivisionError
  2. ImportError
  3. NameError
  4. No error

Python does not know the name prnt.

Which prints 10?

  1. print("7 + 3")
  2. print 7 + 3
  3. echo(7 + 3)
  4. print(7 + 3)

Without quotes, Python does the maths.

Summary

  • Python is a readable language used for automation, data, web and AI.
  • print() shows output.
  • Indentation groups code, comments start with #.
  • Read the last line of an error first.

Next lesson

Next: variables, the labelled boxes that hold information.

Questions

Do I need to pay for Python?

No. Python and its main tools are free and open source.

Python 2 or Python 3?

Always Python 3. Python 2 is retired and no longer supported.

Do I need a powerful computer?

No. Any computer from the last ten years runs Python comfortably.

Progress is saved in this browser.