Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Introduction to Python

Python is a programming language known for code that is easy to read and write, which makes it a good first language.

In this chapter you will learn:

  • Where Python came from and the ideas behind its design
  • The features that make Python popular
  • Which version of Python this book uses
  • How to install Python and choose an editor
  • How to write and run your first program, and how to use the interactive prompt

History of Python

Python is a high-level programming language, first released in 1991. Guido van Rossum, a Dutch computer scientist, created it while he was working at the National Research Institute for Mathematics and Computer Science (CWI) in the Netherlands.

Van Rossum named the language after the British comedy group Monty Python, not the snake. He wanted a language that was easy to read and fun to use. “The Zen of Python”, a short list of guiding principles for the language written by long-time Python developer Tim Peters, sums up that goal. Two of its best-known lines are “Readability counts.” and “There should be one– and preferably only one –obvious way to do it.” In other words, Python code should be clear, and there should usually be one natural way to solve a problem. You can read the whole list by running this one-line program:

import this

Python soon became popular because it was simple and readable, and because its extensive standard library let programmers do common tasks without writing all the code themselves. A large and active community of developers has helped it keep growing.

Python 3 was released in 2008. It was not fully compatible with the older Python 2, which was retired in 2020. This book uses Python 3 only. If you find an old tutorial that writes print "hello" without parentheses, it was written for Python 2.

Today Python is used in many fields, including web development, scientific computing, data analysis, automation, and artificial intelligence. It is one of the most popular programming languages in the world, with wide use in industries such as finance, education, and government.

Key features of Python

Python is known for being simple, readable, and flexible, which appeals to beginners and experienced programmers alike. These are its main features:

  1. Interpreted language: You run Python code directly, without a separate build step. Behind the scenes, the standard Python program (called CPython) first translates your code into bytecode, a simpler set of instructions, and then its interpreter runs that bytecode. This happens automatically every time you run a program, so you can change a line, run it again, and see the result right away. It also means you can type code into Python and get answers one line at a time.
  2. Dynamic typing: You don’t have to declare what type of data a variable will hold. Python works out the type from the value you assign. This keeps code short and flexible.
  3. High-level language: Python hides the details of the computer’s hardware and operating system. This makes code easier to write and understand, and the same program usually runs on Windows, macOS, and Linux without changes.
  4. Extensive standard library: Python comes with modules for tasks such as reading and writing files, working with dates and times, and connecting to web servers.
  5. Large and active community: There are many free resources for learning Python, including tutorials, books, and forums, and thousands of third-party packages that add extra features.
  6. Object-oriented programming: Python lets you build reusable code with classes and objects. You will learn about this in Chapter 5.
  7. Versatility: Python can be used for a wide range of tasks, from small scripts that automate a chore to large web applications and data science projects.

Setting up a Python development environment

To write and run Python programs, you need two things: the Python interpreter and a text editor or integrated development environment (IDE) to write your code in.

Which version to install

This book targets Python 3.10 or newer, and its examples are checked with Python 3.13. Any Python 3 release from 3.10 on will run them. When in doubt, install the newest stable release offered on the Python website.

Install Python

Download Python from the official website: https://www.python.org/downloads/. The site usually suggests the right download for your operating system.

  • Windows: Run the installer. If it offers an option to add Python to PATH (for example a checkbox labeled “Add python.exe to PATH”), turn it on before you continue. PATH is the list of folders your computer searches for programs, so this lets you run Python from any command prompt window. The installer also gives you the py launcher, which you can use to run Python.
  • macOS: Run the installer package you downloaded and follow the steps.
  • Linux: Most distributions already include Python 3. If yours doesn’t, install it with your package manager, for example sudo apt install python3 on Ubuntu or Debian.

To check that Python is installed, open a terminal (Terminal on macOS and Linux, Command Prompt or PowerShell on Windows) and ask Python for its version. On macOS and Linux, type:

python3 --version

On Windows, type:

py --version

Python answers with its version number, for example Python 3.13.1. Make sure it is 3.10 or newer.

Choose an editor

You can write Python in any plain text editor, but an editor made for code makes life easier. A good choice is Visual Studio Code (VS Code) with the Python extension from Microsoft, which you can install from the Extensions view inside VS Code. It colors your code, points out mistakes as you type, and can run your programs with one click.

IDLE, the simple editor that comes with Python, is also fine for the examples in this book. PyCharm is another popular IDE made just for Python.

Your first program

Your first program will print a message on the screen.

  1. Create a folder for your Python files, for example python-practice.
  2. In your editor, create a new file in that folder called hello.py. The .py ending tells your computer and your editor that the file contains Python code.
  3. Type this line into the file and save it:
print("Hello, world!")

print() is a built-in function that shows a value on the screen. The text inside the quotes is a string, which you’ll learn about in Chapter 1.

To run the program, open a terminal and use the cd command to move to your folder:

cd python-practice

Then run the file. On macOS and Linux, type:

python3 hello.py

On Windows, type:

py hello.py

You should see:

Hello, world!

In VS Code, you can also run the file by clicking the Run Python File button (the triangle) in the top-right corner of the editor.

When something goes wrong

If you make a mistake, Python stops and prints an error message called a traceback. For example, if you misspell print as prnt, you will see something like this (the file path will match your own folder):

Traceback (most recent call last):
  File "/Users/you/python-practice/hello.py", line 1, in <module>
    prnt("Hello, world!")
    ^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?

Read the last line first: it names the type of error and usually explains what went wrong. The lines above it tell you which file and line caused the problem. Fix the mistake, save the file, and run it again.

The interactive prompt

You can also use Python interactively. Type python3 (or py on Windows) in a terminal without a file name, and Python starts its interactive prompt, also called the REPL. The prompt looks like >>>. Type a line of code, press Enter, and Python runs it straight away and shows the result:

>>> 2 + 3
5
>>> print("Hello, world!")
Hello, world!

The interactive prompt is handy for trying out small ideas. Notice that it shows the value of 2 + 3 without print(). In a program saved in a file, Python only shows what you print. That is why the examples in this book use print() and show what it prints in a comment:

print(2 + 3)  # Output: 5

To leave the interactive prompt, type exit() and press Enter. On macOS and Linux you can also press Ctrl+D.

Key takeaways

  • Python was created by Guido van Rossum and first released in 1991. Its design puts readability first.
  • Python compiles your code to bytecode and runs it automatically, so you can edit and rerun programs quickly.
  • This book uses Python 3.10 or newer. Check your version with python3 --version on macOS and Linux or py --version on Windows.
  • Save programs in .py files and run them with python3 file.py on macOS and Linux or py file.py on Windows.
  • The interactive prompt (>>>) is a quick way to try out code. Leave it with exit().
  • When you get an error, read the last line of the traceback first.