graph TD
accTitle: Examples of source, assembly, and machine code
accDescr: One decision appears as high-level source code, assembly-like instructions, and binary machine code.
A["High-level source code<br><code style='display: block; text-align: left;'>if count == 10:<br> print('Done')<br>else:<br> count += 1</code>"]
B["Assembly-like code<br><code>CMP R1, #10<br>BEQ label_done<br>ADD R1, R1, #1<br>label_done: CALL print</code>"]
C["Machine code<br><code>0011 0001 0000 1010<br>0100 0010 0001 0101<br>0010 0001 0001 0001<br>1000 0011 0010 0000</code>"]
A -- "lower-level representation" --> B
B -- "encoded for a processor" --> C
3.1 Introduction to programming
An algorithm becomes a program when its steps are expressed in a programming language and prepared for execution. The algorithm describes the procedure; the program provides the computer instructions and related information needed to carry it out.
Programming is about getting a computer to do what you (the user) want it to do. It's about trying to make the future less painful. It's about making things easier for our teammates. It's about getting things wrong and being able to bounce back. It's about forming good habits. It's about understanding your toolset. The Pragmatic Programmer, 2020
Python is a programming language that is widely used in various applications, including web development, data analysis, artificial intelligence, and scientific computing. Python is a powerful tool that can help you automate tasks, create custom functions, and build complex applications.
But how does a programming language like Python work? What are the key concepts and features of Python? This chapter introduces the basics of programming and shows how an algorithm becomes Python code that a computer can execute.
3.1.1 Algorithms, programs, and code
An algorithm is a clear sequence of steps that takes allowed input, produces the required result, and stops. The same algorithm can be carried out by a person or implemented in different programming languages.
A program implements one or more algorithms in a particular programming language. It is executable when suitable software and hardware can run it.
Code is notation written in a programming language as part of a program. Source code is the human-readable code supplied to the tools that prepare and execute a program.
Consider a procedure for greeting a visitor by name:
- receive the visitor's name;
- create a greeting that includes the name; and
- display the greeting.
These steps describe an algorithm. A Python file that carries out the steps is a program. The Python text saved in that file is its source code.
3.1.2 Programming languages and syntax
A programming language has precise words, symbols, and rules for writing computer instructions. Its syntax determines how those words and symbols must be arranged.
The Python source code in Listing 1 stores text under the name message and displays it. Select Run Code to execute the program in your browser. You can change the text and run it again.
The first line is an assignment statement. The second line asks Python to display the value named message.
Different programming languages can express the same algorithm with different syntax. A language is chosen according to the task, the available software, and the computer systems that must run the program.
3.1.3 How programming languages work
Programming languages are used to write instructions that tell a computer what to do. These instructions are written using a specific syntax. When you write code in a programming language, you give the computer a set of commands to follow.
Programming languages can be classified based on several criteria, including their level of abstraction, how they are executed, and how they handle data types. The following characteristics explain how they apply to Python.
High-level and low-level languages
A high-level language is closer to human language and is easier to read and write. Examples include Python, Java, and JavaScript. These languages abstract away many complexities of the underlying hardware.
A low-level language is closer to machine language and is more difficult to read and write. Examples include assembly language and machine code. These languages interact more directly with hardware and require a deeper understanding of computer architecture.
Python is therefore a high-level language. Its notation allows programmers to focus on the intended calculation or procedure rather than individual processor instructions.
Compilation and interpretation
Programmers write source code in a human-readable programming language. Computers ultimately execute machine instructions. Compilation and interpretation prepare and execute the program.
A compiler translates source code before it runs. Its output may be machine code or an intermediate form used by another program.
An interpreter manages program execution from source code or an intermediate form. It does not necessarily translate and execute one source line at a time.
Figure 1 compares source code, assembly-like code, and machine code. The arrows indicate decreasing levels of abstraction. They do not mean that every Python program follows this exact translation path.
Compilation and interpretation can be combined. For this course, the important distinction is between the Python source code students write and the machine instructions eventually executed by the computer.
Dynamic typing
Programming languages can also differ in when they check data types. A statically typed language checks many type constraints before a program runs. A dynamically typed language checks types while the program runs.
Python is dynamically typed. A variable name is not permanently declared as one data type, but every value has a type at runtime. Assigning a new value can therefore make the same variable name refer to a value of another type.
The terms strong typing and weak typing concern whether a language quietly converts values between otherwise incompatible types. These terms do not have one universally accepted definition. Python is commonly described as strongly typed because it usually rejects incompatible operations instead of silently converting the values.
Compile time is when source code is translated before execution. Runtime is when a program executes, processes data, and produces results.