Introduction to Python

Published by Brayan K

Python is a high-level, general-purpose programming language known for its clean, readable syntax and a huge ecosystem of libraries used for web development, data analysis, automation, and AI.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Check that Python is installed

You can start this course without installing anything: this lesson's examples have a Try it Yourself button that opens an editor running Python 3.11 in your browser. Later lessons identify examples that need local Python, extra packages or services. To run programs saved on your own computer, you need Python there as well. Open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and ask Python for its version:

python --version
python3 --version

An answer that starts with Python 3 means you're ready. If the command isn't found, download Python from python.org/downloads. On Windows, the python.org installer also gives you py, a shorter command that starts the same Python.

Run a program from a file

A Python program is a plain-text file whose name ends in .py. To write and run one:

Python reads the file and prints its greeting. Here is the same program, to run in the browser and compare:

print("Hello, World!")

# ✅ Expected output:
# Hello, World!

A file does not display bare calculations

Type python (or python3) without a file name and Python opens its interactive prompt, >>>. Each line you type there runs straight away, and if the line works out a value, Python shows it: enter 2 + 3 and 5 appears below it. A line that stores a value instead, such as x = 5 (Variables explains these), shows nothing. And print("hi") shows hi just once, because the value print() itself gives back is None, which the prompt never shows. That makes the prompt a quick calculator and a good place to try out a single line. Type exit() to leave.

A file works differently. Python still evaluates bare calculations, but does not automatically display their results. Here print() explicitly displays a value. The first two results are discarded, and only the third shows anything:

2 + 3
10 * 4
print(2 + 3)

# ✅ Expected output:
# 5

Showing several values with print()

Give print() several values, separated by commas, and it shows them on one line with a space between each. Two optional settings change that. sep is what goes between the values, and end is what goes after the last one; unless you say otherwise, that's a line break. A print() with nothing inside prints an empty line.

print("Total:", 3, "items")
print("2026", "10", "04", sep="-")
print("Loading", end="...")
print("done")
print()
print("Bye")

# ✅ Expected output:
# Total: 3 items
# 2026-10-04
# Loading...done
#
# Bye

Text goes inside quotes

Python reads anything outside quotes as code: a number, a name or an instruction. Text you want to show must sit inside quotes, double or single, as long as both ends match. Each of the next four programs breaks one rule, and the last line of Python's message says which.

Without quotes, Hello is a name for Python to look up, and nothing has that name:

print(Hello)

# ✅ Expected error:
# NameError: name 'Hello' is not defined

Leaving out the parentheses was how Python 2 printed, and old tutorials still do it. Python 3 spots the habit and suggests the fix:

print "Hello"

# ✅ Expected error:
# SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Forget the closing parenthesis and Python reaches the end of the file still waiting for it:

print("Hello"

# ✅ Expected error:
# SyntaxError: '(' was never closed

An apostrophe inside single quotes ends the text early, leaving the rest of the line unreadable:

print('It's broken')

# ✅ Expected error:
# SyntaxError: unterminated string literal (detected at line 1)

Each of these stopped before printing anything. A mistake in how the code is written, a SyntaxError, means Python can't read the file, so no line of it runs. With the quotes right, all three kinds of text print:

print("Hello")
print('Hello')
print("It's fine")

# ✅ Expected output:
# Hello
# Hello
# It's fine

Text and numbers are different things

"5" in quotes is text; 5 without quotes is a number. The + sign joins text and adds numbers, so two lines that look alike give different answers:

print("5" + "3")
print(5 + 3)

# ✅ Expected output:
# 53
# 8

Mixing the two is an error, because Python won't guess whether you meant to join or to add:

print("Age: " + 25)

# ✅ Expected error:
# TypeError: can only concatenate str (not "int") to str

For now, the fix is a comma, which lets print() show the two side by side. Turning a number into text with str(), and text into a number with int(), is part of Variables and Data Types, and f-strings, which put values inside text, have their own lesson, f-strings & String Formatting.

print("Age:", 25)

# ✅ Expected output:
# Age: 25

Python runs from the top and stops at the first error

Python runs a file one line at a time, from the top. When a line fails, it stops there: the lines above it have already run, and the lines below never do. Here three has no quotes, so Python looks for something called three, finds nothing, and stops before the last line:

print("one")
print("two")
print(three)
print("four")

# ✅ Expected output:
# one
# two

# ✅ Expected error:
# NameError: name 'three' is not defined

Reading an error message

When a program stops, Python prints a report called a traceback. Read it from the bottom up:

The editor on this site prints a shorter report: it calls the file <exec> and doesn't repeat the line, but the last line reads the same.

The error's name says what kind of mistake it is. A SyntaxError is code Python can't read at all, so nothing in the file runs. A NameError is a name it can't find, and a TypeError is something the values can't do, like adding text to a number.

Notes Python ignores: comments

Python ignores everything from a # to the end of the line. Comments are for people: use them to say why a line is there. A # in front of a line also switches it off without deleting it:

# Prices are stored in pence, so 250 means £2.50
print(250)
# print("This line is switched off")
print("Done")

# ✅ Expected output:
# 250
# Done

Asking a question with input()

input() shows a question, waits for the user to type an answer and press Enter, and gives back what they typed. Try this one in a terminal, where it can wait for you:

name = input("What's your name? ")
print("Hello,", name)

The answer always comes back as text, even when the user types digits. Turning "42" into the number 42 with int() is covered in Variables and Data Types.

Practice: separators and line endings

Replace the two blanks so the program prints exactly what its panel shows. The hint lines under the code give the values, in order.

print("Tea", "Cake", "Juice", sep=___)
print("Total", end=___)
print(": 7.50")

# 👉 replace ___ with ", "
# 👉 replace ___ with ""

# ✅ Expected output:
# Tea, Cake, Juice
# Total: 7.50

On your own: a name tag

Write a program that prints your name and the language you're learning on one line, with " | " between them, and then the word Done on a line of its own. Write it in the editor (any example's Try it Yourself button opens one) or in a file, and compare with this solution when you're done:

print("Ada", "Python", sep=" | ")
print("Done")

# ✅ Expected output:
# Ada | Python
# Done

Practice quiz

A file contains only the line 2 + 3. What does running it show?

  • 5
  • Nothing at all
  • 2 + 3
  • An error

Answer: Nothing at all. A bare calculation in a file does not automatically display its result. Typed at the >>> prompt instead, 2 + 3 would show 5.

What does print("a", "b", sep="-") show?

  • a b
  • ab
  • a-b
  • a - b

Answer: a-b. sep replaces the space that print() puts between values.

print("Hi", end="!") is followed by print("Bye"). What appears?

  • Hi!Bye
  • Hi! Bye
  • Hi! on one line and Bye on the next
  • Hi!

Answer: Hi!Bye. end replaces the line break after Hi, so Bye carries on the same line.

Which line stops with a NameError?

  • print(5)
  • print('Hello')
  • print("Hello")
  • print(Hello)

Answer: print(Hello). Without quotes, Hello is a name for Python to look up, and nothing has that name.

What does print("5" + "3") show?

  • 53
  • 8
  • 5 3
  • An error

Answer: 53. Both values are text, so + joins them instead of adding.

A file's second line is print(three), and nothing called three exists. What happens?

  • Nothing is printed at all
  • Every line runs, and the error is shown at the end
  • The first line runs, then the program stops with a NameError
  • Python skips the bad line and carries on

Answer: The first line runs, then the program stops with a NameError. Python runs from the top and stops at the first error. Lines after it never run.

Which error does print "Hi" give in Python 3?

  • NameError
  • SyntaxError
  • TypeError
  • None: it prints Hi

Answer: SyntaxError. Python 3 needs the parentheses, print("Hi"), and its message suggests exactly that.

A Python 3 file starts with print("before"), then has print "after" on its second line. What happens?

  • It prints before, then after
  • It prints before, then stops with NameError
  • It skips the second line
  • It stops with SyntaxError before either line runs

Answer: It stops with SyntaxError before either line runs. Python checks the whole file's syntax before running it. The missing parentheses on the second line prevent even the first line from running.

Continue this course

Frequently asked questions

Should I learn Python 2 or Python 3?

Python 3. Python 2 stopped receiving updates, security fixes included, on 1 January 2020. Old tutorials still show Python 2 code such as print "Hello", which Python 3 rejects with a SyntaxError.

Which editor should I write Python in?

Any plain-text editor works, because a Python file is only text. IDLE, a simple editor, comes with Python from python.org, and code editors such as VS Code add colours and point out mistakes as you type. A word processor such as Word won't do: it saves formatting along with the text.

Is Python free to use?

Yes. Python is open-source software released under the Python Software Foundation License, so you can use it without paying, for paid work too.

Related lessons