|
1 | 1 | +++ |
2 | | -title = "The Python Programming Language" |
| 2 | +title = "Formal and natural languages" |
3 | 3 | weight = 101 |
4 | 4 | +++ |
5 | 5 |
|
6 | | -The programming language you will be learning is Python. Python is an example |
7 | | -of a **high-level language**; other high-level languages you might have heard |
8 | | -of are C++, PHP, Pascal, C#, and Java. |
| 6 | +As noted in the introduction, this course teaches you how to think like a |
| 7 | +computer scientist. A crucial aspect of a computer scientist's job is to |
| 8 | +communicate solutions to people and machines. We use two languages to communicate. |
9 | 9 |
|
10 | | -As you might infer from the name high-level language, there are also |
11 | | -**low-level languages**, sometimes referred to as machine languages or assembly |
12 | | -languages. Loosely speaking, computers can only execute programs written in |
13 | | -low-level languages. Thus, programs written in a high-level language have to be |
14 | | -translated into something more suitable before they can run. |
| 10 | +## Natural and Formal Languages |
15 | 11 |
|
16 | | -Here is an example of a Python program that calculates the sum of the first ```n``` natural numbers. |
17 | | - |
18 | | -``` |
19 | | -def sum(n): |
20 | | - sum = 0 |
21 | | - for i in range(n): |
22 | | - sum += i |
23 | | - return i |
24 | | -``` |
25 | | - |
26 | | -A low-level programming language version of the same program is shown below. |
27 | | - |
28 | | -``` |
29 | | - sum: |
30 | | -.LFB0: |
31 | | - .cfi_startproc |
32 | | - endbr64 |
33 | | - pushq %rbp |
34 | | - .cfi_def_cfa_offset 16 |
35 | | - .cfi_offset 6, -16 |
36 | | - movq %rsp, %rbp v |
37 | | - .cfi_def_cfa_register 6 |
38 | | - movl %edi, -20(%rbp) |
39 | | - movl $0, -4(%rbp) |
40 | | - movl $0, -8(%rbp) |
41 | | - jmp .L2 |
42 | | -.L3: |
43 | | - movl -8(%rbp), %eax |
44 | | - addl %eax, -4(%rbp) |
45 | | - addl $1, -8(%rbp) |
46 | | -.L2: |
47 | | - movl -8(%rbp), %eax |
48 | | - cmpl -20(%rbp), %eax |
49 | | - jl .L3 |
50 | | - movl -4(%rbp), %eax |
51 | | - popq %rbp |
52 | | - .cfi_def_cfa 7, 8 |
53 | | - ret |
54 | | - .cfi_endproc |
55 | | -
|
56 | | -``` |
57 | | - |
58 | | -Almost all programs are written in high-level languages because of their advantages. |
59 | | -It is much easier to program in a |
60 | | -high-level language so programs take less time |
61 | | -to write, they are shorter and easier to read, and they are more likely to be |
62 | | -correct. Second, high-level languages are **portable**, meaning that they can |
63 | | -run on different kinds of computers with few or no modifications. |
| 12 | +**Natural languages** are the languages that people speak, such as English, |
| 13 | +Spanish, and French. They were not designed by people (although people try to |
| 14 | +impose some order on them); they evolved naturally. |
64 | 15 |
|
| 16 | +**Formal languages** are languages that are designed by people for specific |
| 17 | +applications. For example, the notation that mathematicians use is a formal |
| 18 | +language that is particularly good at denoting relationships among numbers and |
| 19 | +symbols. Chemists use formal language to represent the chemical structure of |
| 20 | +molecules. |
65 | 21 |
|
| 22 | +Natural and formal languages are alike in important ways. They both have rules |
| 23 | +to describe what words are part of the language, and what word combinations are valid. |
| 24 | +We call these rules **syntax**. While some sentences are correctly constructed according |
| 25 | +to the syntactic rules, only some sentences have a meaning in the language. |
66 | 26 |
|
67 | | -The engine that translates and runs Python is called the **Python Interpreter**: |
68 | | -There are two ways to use it: *immediate mode* and *script |
69 | | -mode*. In immediate mode, which we also call *shell mode*, you type Python expressions |
70 | | -into the Python interpreter window (which is also called the **shell**), |
71 | | -and the interpreter immediately shows the result. |
| 27 | +Formal languages tend to have strict rules about syntax. For example, ```3+3=6``` |
| 28 | +is a syntactically correct mathematical statement, but ```3=+6``` is not. |
| 29 | +The statement is structurally illegal because you cannot place a plus sign immediately after an equal sign. |
72 | 30 |
|
73 | | -In the Blue Ridge Boost classes, we will use the IDLE application that comes built-in with most Python |
74 | | -installations. In IDLE, the Python interpreter window is labeled **Python Shell**. |
| 31 | +H<sub>2</sub>O is a syntactically correct chemical name, but <sub>2</sub>Zz is |
| 32 | +not. There is no chemical element with the symbol Zz. In addition, molecular formulas |
| 33 | +have to have subscripts after the element name, not before. The formula <sub>2</sub>Zz |
| 34 | +is also structurally illegal. |
75 | 35 |
|
76 | | -Instructions for installing Python on your computer are including in the *Course Introduction* document |
77 | | -on the AoPS class homepage, and are also posted on the class message board. When you start up |
78 | | -IDLE on your computer, it should look something like this: |
| 36 | +Although formal and natural languages have many features in common, including |
| 37 | +structure, syntax, and semantics, there are many differences. |
79 | 38 |
|
| 39 | +Natural languages are full of **ambiguity**, which people deal with by |
| 40 | +using contextual clues and other information. Formal languages are |
| 41 | +designed to be nearly or completely unambiguous, which means that any |
| 42 | +statement has exactly one meaning, regardless of context. |
80 | 43 |
|
81 | | -<img src="/pythonbook/images/shell_sshot.png" alt="Screen shot of shell"> |
| 44 | +To make up for ambiguity and reduce misunderstandings, natural |
| 45 | +languages employ **redundancy**. As a result, they are often |
| 46 | +verbose. Formal languages are less redundant and more concise. |
82 | 47 |
|
83 | | -The ```>>``` is called the **Python prompt**. The interpreter uses the prompt to indicate that it is ready for instructions. |
84 | | -We typed ```2+2```, and the interpreter evaluated our expression, and replied ```4```, |
85 | | -and on the next line it gave a new prompt, indicating that it is ready for more. |
| 48 | +Formal languages mean exactly what they say. On the other hand, natural languages |
| 49 | +use idioms and metaphors. If someone says, ``The |
| 50 | +other shoe fell'', there is probably no shoe and nothing falling. |
86 | 51 |
|
87 | | -Alternatively, you can write a program in a file and use the interpreter to |
88 | | -run the lines of code in the file. Such a file is called a **script**. Scripts have the advantage |
89 | | -that they can be saved, printed, and so on. A Python script is often called a **module**. |
| 52 | +The meaning of a computer program is unambiguous and literal and can |
| 53 | +be understood entirely by analysis of the tokens and structure. |
90 | 54 |
|
91 | | -For example, we can created a file named ```firstprogram.py``` using IDLE. |
92 | | -By convention, files that contain Python programs have names that end with |
93 | | -```.py```. To do this, select “New File” or “New Window” from the File menu of IDLE, and then type |
94 | | -the following two lines into the new window that appears: |
| 55 | +Here are some suggestions for reading programs (and other formal languages). |
| 56 | +First, remember that formal languages are much more dense than natural |
| 57 | +languages, so it takes longer to read them. Also, the structure is very |
| 58 | +important, so it is usually not a good idea to read from top to bottom, left to |
| 59 | +right. Instead, learn to parse the program in your head, identifying the tokens |
| 60 | +and interpreting the structure. Finally, the details matter. Little things |
| 61 | +like spelling errors and bad punctuation, which you can get away with in |
| 62 | +natural languages, can make a big difference in a formal language. |
95 | 63 |
|
96 | | - |
97 | | -To run the program, we can select the “Run Module” or “Run Program” option from the “Run” menu of IDLE (or |
98 | | -alternatively you can press the F5 key). IDLE will ask you to save your program before it |
99 | | -can run it. Save it using the name ```firstprogram.py```. Then your program will run, and it |
100 | | -should look something like the screenshot below: |
| 64 | +## Programming languages |
101 | 65 |
|
102 | | - |
103 | | -Notice that the program always runs in the Shell window. |
| 66 | +Programming languages are formal languages that have been designed to |
| 67 | +express computations. |
104 | 68 |
|
105 | | -Don’t worry, most programs are more interesting than this one. |
| 69 | +Tokens are the basic elements of the language, such as words, numbers, parentheses, |
| 70 | +commas, and so on. |
106 | 71 |
|
107 | | -Working directly in the interpreter is convenient for testing short bits of code because you |
108 | | -get immediate feedback. Think of it as scratch paper used to help you work out |
109 | | -problems. Anything longer than a few lines should be put into a script. |
110 | | - |
111 | | -There’s a third way that you can run Python using this ebook. In many places in this ebook, |
112 | | -we’ll have a Python interpreter built directly into the text. For example, our program |
113 | | -from above can appear directly in the book as shown below: |
| 72 | +Traditionally, the first program written in a new language is called *Hello, |
| 73 | +World!* because all it does is display the words, Hello, World! In Python, the script |
| 74 | +looks like this: |
114 | 75 |
|
115 | 76 | ``` |
116 | | -print('My first program adds two numbers.') |
117 | | -print(2+3) |
| 77 | +print("Hello World!") |
118 | 78 | ``` |
119 | 79 |
|
| 80 | +The script has six tokens: |
| 81 | +1. a function name: ```print``` |
| 82 | +2. an open parenthesis (round bracket): ```(``` |
| 83 | +3. a string: ```"Hello World!"``` |
| 84 | +4. a close parenthesis: ```)```. |
| 85 | + |
| 86 | +If we change the two parentheses around to say ```print)"Hello World!"( |
| 87 | +``` our statement would still |
| 88 | +have five legal and valid tokens, but the structure is illegal. |
| 89 | +
|
| 90 | +When you read a sentence in English or a statement in a formal language, you |
| 91 | +have to figure out what the structure of the sentence is (although in a natural |
| 92 | +language you do this subconsciously). This process is called **parsing**. |
120 | 93 |
|
121 | | -If you click the “Run” button, the program will run and you’ll see its output above directly in the ebook. |
122 | | -You can also edit the code directly in the window above. If you click the “Reset” button (or refresh |
123 | | -your browser window), the code will return to what it originally was (that is, any edits that you’ve |
124 | | -made will be removed.) The little numbers to the left are **line numbers**, and you don’t |
125 | | -type them in. They appear automatically in our ebook, so that it’s easier for us to discuss our |
126 | | -program. For example, we can say “line 1 of our program prints a sentence”. |
| 94 | +The pram is an example of using the **print function**, which displays a value on the screen. |
127 | 95 |
|
| 96 | +The quotation marks in the program mark the beginning and end of the value; |
| 97 | +they do not appear in the result. |
0 commit comments