Introduction to Programming Languages and Python

Introduction to Programming Languages and Python

Introduction to Programming Languages and Python

Table of Contents1. Introduction:2. Classification of Programming Languages:2.1 Machine Language2.2 Assembly Language2.3 High-Level Language2.3.1 Compiled (e.g., C Language):2.3.2 Interpreted (e.g., Python):2.4 Summary3. Introduction to Python3.1 History of Python Interpreters3.2 Types of Python Interpreters?4. Installing CPython Interpreter5. First Python Program5.2 Comments6. Using the IDE Tool PyCharm6.1 What is an IDE? Why Do We Need an IDE6.2 Installing PyCharm6.3 Creating Folders in PyCharm6.4 How to Create Files and Execute Programs

Introduction to Programming Languages and Python

#1. Introduction

Based on what we learned in the previous chapter, with computer hardware in place and the operating system installed, we now have a platform for running applications. Our next task is to learn how to use a programming language to develop applications.

This chapter aims to give you an overview of programming languages, focusing specifically on Python.

Illustration: Funny Image 01

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

2. Classification of Programming Languages:

2.1 Machine Language

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

Machine language is the language that a computer (slave) can understand, meaning it is the binary instructions that the computer can directly process. Therefore, machine language is low-level language, which directly manipulates hardware. The term low-level refers to its proximity to the computer hardware, requiring detailed knowledge of hardware details and direct control over it. Here’s a detailed explanation:

# Machine Language
   Instructions described in binary code 0 and 1 are called machine instructions. Since computers operate based on binary instructions, machine language directly controls computer hardware.

   When writing programs in machine language, programmers must first memorize all instruction codes and their meanings for the computer they are using. Then, while writing the program, they must manage each instruction and data storage allocation and input/output by themselves, and remember the state of each working unit used in the programming process. This is a very tedious job, often taking dozens or hundreds of times longer than the actual running time. Moreover, the programs consist solely of 0s and 1s, which are not intuitive, hard to read and write, and prone to errors, heavily dependent on specific computer hardware models, and thus very limited. Apart from professionals from computer manufacturers, most programmers no longer learn machine language.

   Machine language is understood and used by microprocessors, and there can be as many as 100,000 types of machine language instructions. Below are some simple examples:

   # Example of Instruction Parts
   0000 represents Load (LOAD)
   0001 represents Store (STORE)
  ...

   # Example of Register Parts
   0000 represents Register A
   0001 represents Register B
  ...

   # Example of Memory Parts
   000000000000 represents memory address 0
   000000000001 represents memory address 1
   000000010000 represents memory address 16
   100000000000 represents memory address 2^11

   # Integrated Example
   0000,0000,000000010000 represents LOAD A, 16
   0000,0001,000000000001 represents LOAD B, 1
   0001,0001,000000010000 represents STORE B, 16
   0001,0001,000000000001 represents STORE B, 1[1]

Summary of Machine Language

# 1. Highest Execution Efficiency
Programs written in machine language can be understood and run directly by computers, leading to high execution efficiency.

# 2. Lowest Development Efficiency
Complex and low development efficiency.

# 3. Poor Cross-Platform Compatibility
Close to and dependent on specific hardware, poor cross-platform compatibility.

Illustration: Funny Image 02

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

2.2 Assembly Language

Assembly language uses English labels to represent a set of binary instructions. Undoubtedly, assembly language is an improvement over machine language, but its essence remains direct hardware manipulation, making it a low-level language close to computer hardware.

Illustration: Funny Image 03

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

# Assembly Language
The essence of assembly language is the same as machine language; it directly operates on hardware, but the instructions use English abbreviations for easier recognition and memory. It also requires programmers to write every specific operation in command form. Each instruction in assembly programs corresponds to a very fine action in the actual operation process, such as moving or incrementing. Therefore, assembly source programs are generally lengthy, complex, and prone to errors, and using assembly language requires more computer expertise. However, the advantages of assembly language are also evident; operations that can be completed using assembly language cannot generally be achieved with high-level languages, and executable files generated from assembly source code are not only smaller but also run faster.

An assembly hello world program that prints "hello world" requires writing over ten lines, as shown below:

; hello.asm 
section .data           ; Data segment declaration
       msg db "Hello, world!", 0xA     ; String to output
       len equ $ - msg                 ; String length
       section .text           ; Code segment declaration
       global _start           ; Specify entry function
       _start:                 ; Display a string on screen
       mov edx, len     ; Argument 3: string length
       mov ecx, msg     ; Argument 2: string to display
       mov ebx, 1       ; Argument 1: file descriptor (stdout)
       mov eax, 4       ; System call number (sys_write)
       int 0x80         ; Call kernel function
                        ; Exit program
       mov ebx, 0       ; Argument 1: exit code
       mov eax, 1       ; System call number (sys_exit)
       int 0x80         ; Call kernel function

Summary of Assembly Language:

# 1. High Execution Efficiency
Using English labels to write programs is relatively simple, and execution efficiency is high, but slightly lower than machine language.

# 2. Low Development Efficiency:
Still directly operating on hardware, complexity is slightly lower than machine language, but development efficiency remains low.

# 3. Poor Cross-Platform Compatibility
Also dependent on specific hardware, poor cross-platform compatibility.

2.3 High-Level Language

High-level languages are written from the perspective of humans (masters), using human-readable characters to send instructions to the operating system rather than directly manipulating hardware. Therefore, high-level languages interact with the operating system, and the term high-level refers to the abstraction from hardware details, significantly improving development efficiency. However, because high-level languages are further from hardware and closer to human language, which humans can understand, computers need to translate them, resulting in lower execution efficiency compared to low-level languages.

High-level languages can be classified into two types based on their translation methods:

2.3.1 Compiled (e.g., C Language):

Introduction to Programming Languages and Python

Similar to Google Translate, it compiles all program code into binary instructions that the computer can recognize. The operating system then uses the compiled binary instructions to directly manipulate hardware. Detailed explanation as follows:

# 1. High Execution Efficiency
Compilation means translating the program source code into target code (i.e., machine language) before the application source program is executed, allowing the target program to run independently of its language environment, making it convenient and efficient.

# 2. Low Development Efficiency:
Once an application needs modification, the source code must be changed, then recompiled to generate a new target file for execution. If only the target file exists without the source code, modifications become inconvenient. Thus, development efficiency is lower than interpreted languages.

# 3. Poor Cross-Platform Compatibility
Compiled code is translated for a specific platform. The result of the current platform's translation cannot be used on a different platform, requiring recompilation for different platforms, hence poor cross-platform compatibility.

# Other
Most programming languages today are compiled. After compiling, the source program is saved in another file, allowing it to run independently on the computer multiple times. Most software products are distributed to users in target program form, making it easy to run directly while making it difficult for others to steal the underlying technology. C, C++, Ada, and Pascal are all implemented in a compiled manner.

2.3.2 Interpreted (e.g., Python):

Introduction to Programming Languages and Python

Similar to simultaneous translation, an interpreter reads the program code, translating and executing it simultaneously. Detailed explanation as follows:

# 1. Low Execution Efficiency
In interpreted languages, the interpreter does not produce target machine code but generates intermediate code that is easier to execute. This intermediate code differs from machine code, and its interpretation is supported by software, which cannot be used directly by hardware. Software interpreters usually lead to lower execution efficiency.

# 2. High Development Efficiency
Programs written in interpreted languages are executed by another program that understands the intermediate code. Unlike compiled programs, interpreters translate source program statements into executable machine instructions one by one, eliminating the need to translate the source program into target code before execution. The advantage of interpreters is that when a syntax error occurs, it immediately alerts the programmer, allowing for corrections during program development.

# 3. Strong Cross-Platform Compatibility
Code execution depends on the interpreter, and different platforms have corresponding versions of the interpreter, making interpreted languages highly cross-platform.

# Other
For interpreted Basic languages, a special interpreter is needed to interpret and execute Basic programs. Each statement is translated only during execution, resulting in low efficiency. Generally, dynamic languages are interpreted, such as Tcl, Perl, Ruby, VBScript, JavaScript, etc.

PS: Mixed Languages

Java is a special type of programming language. Java programs also need to be compiled, but they are not compiled directly into machine language; instead, they are compiled into bytecode, which is then executed in the Java Virtual Machine in an interpreted manner.

2.4 Summary

In summary, comparing different programming languages for application development:

# 1. Execution Efficiency: Machine Language > Assembly Language > High-Level Language (Compiled > Interpreted)

# 2. Development Efficiency: Machine Language < Assembly Language < High-Level Language (Compiled < Interpreted)

# 3. Cross-Platform Compatibility: Interpreted languages have strong cross-platform compatibility.

Since we are developing applications, and applications must run on a specific platform, the speed of applications is limited by the platform (just like F1 racing cars and BMW cars running on the same Beijing Fifth Ring road, both limited by road conditions, with speeds being similar), and cannot be improved solely from the language level. Therefore, development efficiency and cross-platform compatibility are the priority considerations, which is why we choose Python.

Illustration: Funny Image 04

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

3. Introduction to Python

When discussing Python, it involves two aspects: one represents the syntax style of the Python language, and the other represents the application program specifically designed to interpret that syntax style: the Python interpreter.

The founder of Python is Guido van Rossum. The name Python comes from Guido’s favorite TV show, Monty Python’s Flying Circus. He hoped that this new language called Python would meet his ideals: to create a language that is simple and easy to learn and use, with strong extensibility, while retaining the powerful features of C. Thus, on Christmas in 1989, Guido began writing an interpreter that could interpret Python language syntax.

Python advocates beauty, clarity, and simplicity, and is an excellent and widely used language. According to the latest TIOBE index, Python has soared to third place in the world.

Python can be applied in various fields, such as artificial intelligence, data analysis, web scraping, quantitative finance, cloud computing, web development, automated operations/testing, game development, network services, image processing, and more. Almost all medium to large internet companies in the industry use Python, including YouTube, Dropbox, BT, Quora (Zhihu in China), Douban, Zhihu, Google, Yahoo!, Facebook, NASA, Baidu, Tencent, AutoHome, Meituan, etc.

Illustration: Funny Image 05

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

3.1 History of Python Interpreters

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

In 1989, Guido began writing the compiler for the Python language.

In 1991, the first Python compiler was born. It was implemented in C and could call C library files. From its inception, Python included classes, functions, exception handling, core data types including lists and dictionaries, and a module-based extension system.

Granddaddy of Python web frameworks, Zope 1 was released in 1999

Python 1.0 – January 1994 added lambda, map, filter, and reduce.

Python 2.0 – October 16, 2000, introduced garbage collection, forming the foundation of the current Python language framework.

Python 2.4 – November 30, 2004, the most popular web framework Django was born in the same year.

Python 2.5 – September 19, 2006

Python 2.6 – October 1, 2008

Python 2.7 – July 3, 2010

In November 2014, it was announced that Python 2.7 would be supported until 2020, and there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible.

Python 3.0 – December 3, 2008 (careful readers will notice that 3.0 was released in 2008, but 2.7 was released in 2010? This is because 3.0 is not backward compatible with 2.0, and many companies had already developed a large number of programs based on version 2.0, investing significant resources. This led to a refusal to upgrade to 3.0, forcing the official release of 2.7 as a transition version. We should adopt the 3.0 interpreter for program development, but we will specifically point out the differences between the two versions when we encounter them for the convenience of readers maintaining software on version 2.0.)

Python 3.1 – June 27, 2009

Python 3.2 – February 20, 2011

Python 3.3 – September 29, 2012

Python 3.4 – March 16, 2014

Python 3.5 – September 13, 2015

Python 3.6 – December 23, 2016, released python 3.6.0 version.

3.2 Types of Python Interpreters?

The official Python interpreter is essentially a software developed in C language. Its function is to read the content of files ending in .py and translate and execute the corresponding code according to the syntax and rules defined by Guido. This C-based interpreter is called CPython, which is the best-performing and most widely used interpreter in the Python field. The interpreters we refer to later are all CPython interpreters. However, interpreters as application software can be developed in other languages as long as they can interpret the syntax of Python. Here are some types of Python interpreters for a brief understanding:

# Jython
JPython interpreter is a Python interpreter written in JAVA, which can directly compile Python code into Java bytecode and execute it. It makes it possible to embed Python scripts in Java-based projects and also allows Java programs to be introduced into Python programs.

# IPython
IPython is an interactive interpreter based on CPython, meaning that IPython enhances the interactive mode but has the same functionality as CPython in executing Python code. This is similar to many domestic browsers that, although different in appearance, actually call IE as their core.
CPython uses >>> as the prompt, while IPython uses In [number]: as the prompt.

# PyPy
PyPy is a Python interpreter developed by Python developers to better hack Python, implemented in Python. PyPy provides a JIT compiler and sandbox functionality, dynamically compiling Python code (note that it is not interpreted), thus running faster than CPython.

# IronPython
IronPython is similar to Jython, but IronPython is a Python interpreter running on the Microsoft .NET platform, which can directly compile Python code into .NET bytecode.

Illustration: Funny Image 06

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

4. Installing CPython Interpreter

The Python interpreter currently supports all major operating systems. It comes pre-installed on Linux, Unix, and Mac systems, while Windows requires a separate installation. The specific steps are as follows:

## 4.1 Downloading the Python Interpreter

Open the official website https://www.python.org, and follow the prompts to download.

Illustration: Downloading Interpreter 1

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

Illustration: Downloading Interpreter 2

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

Illustration: Downloading Interpreter 3

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

## 4.2 Installing the Python Interpreter

Illustration: Installing Interpreter 1

Introduction to Programming Languages and Python

Illustration: Installing Interpreter 2

Introduction to Programming Languages and Python

Illustration: Installing Interpreter 3

Introduction to Programming Languages and Python

## 4.3 Testing Installation Success

Windows –> Run –> Type cmd and press Enter, then the cmd program will pop up. Type python; if you can enter the interactive environment, it means the installation was successful.

Illustration 1: CMD Check Installation 1

Introduction to Programming Languages and Python

Illustration 1: CMD Check Installation 2

Introduction to Programming Languages and Python

5. First Python Program

## 5.1 There Are Two Ways to Run a Python Program

Method 1: Interactive Mode

Illustration: Interactive Environment Test

Introduction to Programming Languages and Python

Method 2: Script File

# 1. Open a text editing tool, write the following code, and save the file. The file path here is D:\test.py. Note: The Python interpreter executes programs via interpretation, meaning it opens the file to read content; thus, the file extension is not strictly required, but it is commonly defined as .py.
print('hello world')

# 2. Open CMD and run the command, as shown in the image.

Illustration: Script File Test

Introduction to Programming Languages and Python

Summary:

# 1. In interactive mode, you can get immediate execution results, making debugging very convenient.
# 2. If you want to permanently save the code, you must write it into a file.
# 3. In the future, we will mainly write code into files, occasionally needing to open interactive mode to debug certain code or validate results.

5.2 Comments

Before formally learning Python syntax, we must introduce a very important syntax: comments.

1. What Are Comments

Comments are explanations of the code, and the content of comments will not be executed as code.

2. Why Comment

Enhance code readability.

3. How to Use Comments?

Code comments can be single-line and multi-line comments.

1. Single-line comments use the # symbol, which can be placed above or after the code.

2. Multi-line comments can use triple double quotes """ """.

Illustration: Usage of Comments

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

Introduction to Programming Languages and Python4. Principles of Code Comments:

1. Do not add comments to everything; only add comments to parts you find important or hard to understand.
2. Comments can be in Chinese or English, but avoid using pinyin.

6. Using the IDE Tool PyCharm

6.1 What is an IDE? Why Do We Need an IDE

When writing the first Python program, the following issues arise that severely impact development efficiency:

Issue 1: We learned that a Python program requires at least two software applications to operate from development to execution:

1. Open a software: a text editor, to create text for writing the program.
2. Open CMD, then enter commands to execute the Python program.

Issue 2: During development, there is no code suggestion or error correction functionality.

In summary, if there is a tool that can integrate the functions of multiple software applications, along with code suggestions and error correction features, it would greatly enhance the development efficiency of programmers. This is the origin of IDE, which stands for Integrated Development Environment. The best IDE for developing Python programs is PyCharm.

6.2 Installing PyCharm

# Download link: https://www.jetbrains.com/pycharm/download Select the Professional version.

After installation, register, and then start. You will be prompted to create a project, which is essentially a folder where we will store our code in the future.

Illustration: Creating a Project in PyCharm

Introduction to Programming Languages and Python

6.3 Creating Folders in PyCharm

Creating folders is a good way to manage files.

Introduction to Programming Languages and Python

6.4 How to Create Files and Execute Programs

Create a Python file named test.py.

Introduction to Programming Languages and Python

In test.py, write code. You can use the tab key to auto-complete keywords and receive error prompts.

Illustration: Executing Files in PyCharm

Introduction to Programming Languages and PythonIntroduction to Programming Languages and Python

Introduction to Programming Languages and Python

Introduction to Programming Languages and Python
END

Long press the QR code below to remember to follow me~

Introduction to Programming Languages and Python

Leave a Comment