Introduction to programming in the Python language (1) What is programming?

Machine Learning Ontology Clojure Python Digital Transformation Artificial Intelligence Probabilistic Generative Model  Natural Language Processing Intelligent information technology Python Navigation of this blog

Summary

Python will be a general-purpose programming language with many excellent features, such as being easy to learn, easy to write readable code, and usable for a wide range of applications Python was developed by Guido van Rossum in 1991.

As a relatively new language, Python can utilize a variety of effective programming techniques, including object-oriented programming, procedural programming, and functional programming. It is also widely used in web applications, desktop applications, scientific and technical computing, machine learning, artificial intelligence, and other fields because of the many libraries and frameworks available. Furthermore, it is cross-platform and runs on many operating systems such as Windows, Mac, and Linux, etc. Because Python is an interpreted language, it does not need to be compiled and has a REPL-like structure, which speeds up the development cycle.

This section discusses topics related to Pyhton based on the “World Standard MIT Textbook Introduction to Programming with the Python Language, Second Edition: Data Science and Applications.

Before discussing Python in this article, I will discuss programming and computers.

Introduction to programming in the Python language (1) What is programming?

A computer does two things (and only two things). A computer does two things (and only two things): it does calculations, and it remembers the results of those calculations. However, computers can do both of these things very well. Even a typical computer performs about one billion calculations per second. (This means that if you drop a ball from a height of one meter off the floor, you can do more than one billion calculations before the ball reaches the floor.) Also, the hundreds of gigabytes of capacity of a typical computer, for example, weighs more than several hundred thousand tons if you imagine each byte as 1 gram, which is the equivalent of tens of thousands of African elephants.

For most of human history, the speed of computation has been limited by the speed of the human brain, and the recording of computational results has been limited by the human ability to describe them. This corresponds to the fact that only very small problems could be solved by calculation. With the use of modern computers, this problem processing capability has been greatly expanded.

We will now consider “computational thinking” to solve problems computationally.

All knowledge can be classified as either declarative or imperative. Declarative knowledge consists of statements of fact. For example, “The square root of x is y satisfying yxy=x” is declarative knowledge. For example, “The square root of x is y that satisfies yxy=x” is declarative knowledge. This is a statement of fact and says nothing about how to find the square root.

In contrast, imperative knowledge is “how-to” knowledge, which is a recipe for deriving information. Heron of Alexandria derived the following method for calculating the square root of a number.

  1. Start with an appropriate estimate of the square root, g
  2. If gxg is close enough to x, stop and report g as a turn.
  3. Otherwise, take g and the flat of x/g, i.e., (g+x/g)/2, as the new estimate
  4. Set the new estimate to g and repeat until gxg is close enough to x

As an example, find the square root of 25.

  1. Set the estimate g to the appropriate number 3.
  2. Decide that 3×3=9 is not close enough to 25
  3. Set g=(3+(25/3)/2=5.67
  4. Determine that 5.67×5.67=32.15 is not close enough to 25
  5. Set g=(5.67+25/5.67)/2=5.04
  6. Decide that 5.04×5.04=25.4 is close enough to 25 and stop, setting 5.04 as the square root of 25

The description of the method consists of simple examples of steps and the flow control of when to perform the steps. This kind of description is called an algorithm.

This algorithm is an example of a trial-and-error (quess-and-check algorithm). It is based on the fact that it is easy to check whether an estimate is good or not. A bit more formally, an algorithm is a finite list of instructions describing a computatiom (changing a given input into a well-defined state and eventually outputting a result).

So how do we convert such a list of instructions into a mechanical process? One way is to design a dedicated machine to calculate the square root. In fact, early computers were fixed-program computers. (An early computer built by Atansoff and Berry in 1941 could solve linear equations but not much else, and a cryptanalyzer developed by Alan Turing during World War II was designed only to break the German Enigma Ango Aki.)

The first truly modern computer would be the Manchester Mark 1. Unlike its predecessors, it is what is called a stored-program computer, with components that store (and manipulate) sequences of instructions and execute them. By creating a basic design for a set of instructions and then refining the computation method as a sequence of instructions (which becomes a program), highly flexible opportunities can be built. By treating these sequences of instructions like data, a program-integrated computer can easily change its program.

The heart of the computer is the program (the program interpreter), which can execute the correct set of instructions, allowing it to compute anything that can be described by the basic set of instructions.

Both programs and data are stationed in memory. Usually, there is a counter in the program that indicates a specific location (number) in memory, and the computation is started by executing the instruction at that number. In most cases, the interpreter simply executes the next instruction, but not always. In some cases, it will perform some test and jump to another point in the sequence of instructions based on the result. This is called flow of control, and it is essential for drawing programs to perform complex tasks.

A good programmer combines a few basic elements to create countless useful programs, just as a good cook combines the same ingredients to create countless delicious dishes. This is the reason why programming requires a great deal of effort.

In order to create a recipe (a sequence of instructions), you need a programming language to describe the cockerel and tell the computer to march on.

In 1936, the British mathematician Alan Turing proposed a cremation calculator that later became known as the universal Turing machine. The machine consisted of an infinite memory in the form of a tape that could be written as 0 or 1, and a few simple basic instructions for moving and reading the tape. The Church-Turing thesis asserts that if a function is computable, then it can be programmed and computed by a Turing machine.

The “if” part of the Church-Turing thesis is important. Not every problem has a computational solution. As an example, Turing showed that given an arbitrary program (let’s call it P), it is impossible to write a program that outputs true if and only if P keeps turning forever. This is what is called the halting problem.

From Church-Turing’s proposal, the following notion of Turing completeness is derived. A programming language is said to be Turing complete if it can simulate the universal Turing machine (have equivalent computing power). All modern programming languages are Turing complete. What is programmable in one programming language is also programmable in other programming languages. Of course, some problems are easier to write in one programming language than another, but all programming languages are essentially equivalent in terms of their computational power.

Fortunately, it is not necessary to write programming using only the basic instructions of the Turing machine; in fact, modern programming languages provide a much larger set of useful basic instructions. No matter what basic instructions you have, and no matter what methods you have for using them, the fundamentals of programming will be the same in that the computer will only do what it is told to do.

There are hundreds of programming languages in the world, but there is no such thing as the best programming language. (Some of the worst may be nominated.) The superiority of programming languages varies depending on the application. For example, MATLAB is a good language for vector and matrix manipulation, C is a good language for blogs that control data networks, PHP is a good language for building websites. And python can be one of the good general purpose programming.

Each programming language has its own basic building blocks, syntax, static semantics, and semantics. In analogy with natural languages, the basic building blocks are words, and grammar refers to the connection of words to form a correct sentence. Static semantics defines what sentences make sense, semantics defines the meaning of multiple sentences, etc. In Python, the building blocks are literals and inflix operators.

The syntax of a language defines the correct placement of a sequence of letters or symbols. For example, the string “Cat dog boy” in English is not grammatically correct because English grammar does not allow <noun><noun><noun> sentences. In Python, “3.2+3.2” is grammatically correct, but “3.2 3.2” is not.

Static semantics defines whether a grammatically correct sequence of characters has meaning. For example, in English, “I runs fast” is grammatically correct because it has the type <subject><verb><adverb>. However, this is not correct English. However, this is not correct English, because the subject “I” is singular, and the verb “runs” is used in the third person singular. In Python, “3.2/’abc'” is grammatically correct because it has the type <literal><operator><literal>. In Python, “3.2/’abc'” is grammatically correct because it has the type <literal><operator><literal>, but it is an error in static semantics because it does not make sense to use a string to represent a number.

The semantics of a language approaches “meaning” as a string of grammatically correct characters and free of errors in static semantics. The semantics of a sentence in a natural language can be ambiguous. For example, the sentence “I can’t speak too highly of this student” can be taken as either a compliment or a put-down. In programming languages, a correct program (statement) is designed to have a single meaning.

Grammatical errors are more common (for beginning learners), but they are the least dangerous. All serious programming languages have features for detecting grammatical errors and do not allow you to run a program that contains even a single grammatical error. Furthermore, in most cases, the programming language system will clearly point out where the error is so that you know what to do to fix it.

The case of errors in static semantics is a bit more complicated. In some programming languages (e.g. Java), a lot of static semantics checking is done before the program is executed. In other languages (e.g. C, Python), there is not much checking of the static semantics before the program is executed. In other languages (e.g., C and Python), not much checking of static semantics is done before the program is executed; instead, in Python, etc., a sufficient number of checks of static semantics are done during program execution.

The term “has semantic errors” is not often used in programs. When a program contains no grammatical errors or errors in static semantics, it has “meaning” (in other words, semantics). It does not guarantee that it has the meaning intended by its creator. When a program does not work as the author intended, the following things happen.

  • Crashing. Produces some obvious sign of crashing, and the program stops
  • Keeps spinning and does not stop.
  • The program finishes and calculates some result, but you don’t know if it is correct or not

The above are all bad situations, but the last one is the worst. Bad things can happen if the program is doing the right thing, but the result is not correct. For example, property can be lost, planes can crash, etc. If a program doesn’t work, one of the first steps in good programming is to make it as self-explanatory as possible.

In the next article, we will discuss the features of the Python language.

コメント

タイトルとURLをコピーしました