Chapter 1 Introduction to Python
1.2 Python and Computer Programming¶
The programming language you will be learning is Python. Python is an example of a high-level language; other high-level languages you might have heard of are Java and C++.
Python was published in 1991, named after the comedy show Monte Python’s Flying Circus. Due to its simplicity and elegance, it gained popularity among programmers quickly, and was adopted by large companies, such as Google Inc. Compared with other programming languages, such as Java and C++, Python is particularly useful for learning computer programming.
Fig. 1.1 shows a comparison between Python, Java and C++, three popular programming languages. Among the three, C++ was invented the earliest. Its statements were the closest to hardware instructions. As a result, C++ programs run very fast, and can be optimized in many ways. However, mastering C++ requires knowledge in computer hardware. Java was invented decades after C++. It offers higher levels of abstraction compared to C++, making it easier to program. As a trade-off, Java programs run slower than C++. Python takes one step further in abstracting away hardware details, offering a conceptually very simple system for statement execution. As a result, Python is the easiest to learn and fully master, but Python programs are typically the slowest to run.
![Fig. 1.1 Comparison of Python, Java and C++](../_images/fig1-1.png)
Fig. 1.1 Comparison of Python, Java and C++.¶
In terms of functionalities, Python is as powerful as Java and C++. It supports all the functionalities that typical computer hardware supports. In addition, libraries provided by Python and open-source contributers enrich the power of Python, making it the most convenient choice for many applications, such as text processing. Python libraries such as numpy and scipy are implemented in C++ in the lower level, offering very fast ways to perform scientific computation using Python.
As you might infer from the name high-level language, there are also low-level languages, sometimes referred to as machine languages or assembly languages. Machine language is the encoding of instructions in binary so that they can be directly executed by the computer. Assembly language uses a slightly easier format to refer to the low level instructions.
Loosely speaking, computers can only execute programs written in low-level languages. To be exact, computers can actually only execute programs written in machine language. Thus, programs written in a high-level language (and even those in assembly language) have to be processed before they can run. This extra processing takes some time, which is a small disadvantage of high-level languages. However, the advantages to high-level languages are enormous.
First, it is much easier to program in a high-level language. Programs written in a high-level language take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can run on only one kind of computer and have to be rewritten to run on another.
Due to these advantages, almost all programs are written in high-level languages. Low-level languages are used only for a few specialized applications.
Two kinds of programs process high-level languages into low-level languages: interpreters and compilers. An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.
Fig. 1.2 Interpreter.¶
A compiler reads the program and translates it completely before the program starts running. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation.
Fig. 1.3 Compiler.¶
Many modern languages use both processes. They are first compiled into a lower level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language.
There are two ways to use the Python interpreter: shell mode and program mode. In shell mode, you type Python expressions into the Python shell, and the interpreter immediately shows the result. The example below shows the Python shell at work.
$ python
Python 3.6.9 (default, Dec 8 2021, 21:08:43)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>>
The >>>
is called the Python prompt. The interpreter uses the prompt to
indicate that it is ready for instructions. You type 2 + 3
. The
interpreter evaluates the expression and replies 5
. On the next line it gives a new prompt indicating that it is ready for more input.
Working directly in the interpreter is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems.
Alternatively, you can write an entire program by placing lines of Python instructions in a file and then use the interpreter to
execute the contents of the file as a whole. Such a file is often referred to as source code. For example, you can use a text editor to create a source code file named firstprogram.py
with
the following contents:
print("My first program adds two numbers, 2 and 3:")
print(2 + 3)
By convention, files that contain Python programs have names that end with .py
. Following this convention will help your operating system and other
programs identify a file as containing python code.
$ python firstprogram.py
My first program adds two numbers, 2 and 3:
5
These examples show Python being run from a Unix command line. In other development environments, the details of executing programs may differ. Also, most programs are more interesting than this one.
Want to learn more about Python?
If you would like to learn more about installing and using Python, Installing Python for Windows shows you how to install the Python environment under Windows.
Check your understanding
- the instructions in a program, stored in a file.
- The file that contains the instructions written in the high level language is called the source code file.
- the language that you are programming in (e.g., Python).
- This language is simply called the programming language, or simply the language.
- the environment/tool in which you are programming.
- The environment may be called the IDE, or integrated development environment, though not always.
- the number (or “code”) that you must input at the top of each program to tell the computer how to execute your program.
- There is no such number that you must type in at the start of your program.
1.2Q-1: Source code is another name for:
- It is high-level if you are standing and low-level if you are sitting.
- In this case high and low have nothing to do with altitude.
- It is high-level if you are programming for a computer and low-level if you are programming for a phone or mobile device.
- High and low have nothing to do with the type of device you are programming for. Instead, look at what it takes to run the program written in the language.
- It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
- Python is a high level language but must be interpreted into machine code (binary) before it can be executed.
- It is high-level if it easy to program in and is very short; it is low-level if it is really hard to program in and the programs are really long.
- While it is true that it is generally easier to program in a high-level language and programs written in a high-level language are usually shorter, this is not always the case.
1.2Q-2: What is the difference between a high-level programming language and a low-level programming language?
- 1 = a process, 2 = a function
- Compiling is a software process, and running the interpreter is invoking a function, but how is a process different than a function?
- 1 = translating an entire book, 2 = translating a line at a time
- Compilers take the entire source code and produce object code or the executable and interpreters execute the code line by line.
- 1 = software, 2 = hardware
- Both compilers and interpreters are software.
- 1 = object code, 2 = byte code
- Compilers can produce object code or byte code depending on the language. An interpreter produces neither.
1.2Q-3: Pick the best replacements for 1 and 2 in the following sentence: When comparing compilers and interpreters, a compiler is like 1 while an interpreter is like 2.
© Copyright 2024 GS Ng.