Книга: Practical Programming, Fourth Edition
Назад: Chapter 2: Hello, Python
Дальше: Expressions and Values: Arithmetic in Python

How Does a Computer Run a Python Program?

To understand what happens when you’re programming, it helps to have a mental model of how a computer executes a program.

A modern computer is built from several hardware components, including a processor (Central Processing Unit, a CPU) that runs programs and performs calculations, storage such as a solid-state drive (SSD) to hold data, and other essential parts, such as a touchscreen or monitor, keyboard, and Wi-Fi or cellular connectivity for getting online.

An operating system is a program that manages your computer’s hardware

To manage all of these components, every computer runs an operating system (OS), such as Microsoft Windows, Linux, or macOS. An operating system is one of the few programs that has direct access to the hardware. When any other application (such as your browser, a spreadsheet program, or a game) wants to draw on the screen, find out what key was just pressed on the keyboard, or fetch data from storage, it sends a request to the OS:

Basic OS

This arrangement may seem like a convoluted approach to getting things done, such as displaying images on the screen or responding to your actions. Yet, it means that only the people writing the OS have to worry about the differences between one graphics card and another, as well as whether the computer is connected to a network via Ethernet or wireless. The rest of us, who are analyzing scientific data, creating mobile apps, editing videos, or exploring virtual worlds, only have to learn our way around the OS, and our programs will then run on thousands of different kinds of hardware.

Today, it’s common to add another intermediary (a layer) between the programmer and the computer’s hardware. When you write a program in Python, Java, or Visual Basic, the program doesn’t run directly in contact with the OS. Instead, another program called an interpreter or virtual machine translates your program’s commands into a language the OS understands. Command interpretation is easier, more secure, and more portable across operating systems than direct execution, although the latter is somewhat faster:

OS

Programs are made up of statements

There are two ways to use the Python interpreter. One is to use a command to execute a Python program saved in a file with a py extension. The other is to interact with the interpreter in a program called a shell, where you type statements one at a time. The interpreter will execute each statement when you type it, do what the statement says to do, and show any output as text, all in one window. You will explore Python in this chapter using a Python shell.

Why Is a CPU “Central”?

icon indicating an aside

Older computers from the 1960s had more than one processing unit, including a central processing unit and peripheral processing units, also known as input/output processors. Modern computers also have more than one processing unit, the others usually being a graphics processing unit (GPU) or a digital signal processor (DSP).

Назад: Chapter 2: Hello, Python
Дальше: Expressions and Values: Arithmetic in Python