@@ -3,7 +3,7 @@ of the textbook
33<a href =" https://sourceacademy.org/sicpjs " >Structure and Interpretation
44of Computer Programs, JavaScript Adaptation</a > (SICP JS).
55
6- ## What names are predeclared in Source §1?
6+ ## What names are predeclared in Python §1?
77
88On the right, you see all predeclared names of Python §1, in alphabetical
99order. Click on a name to see how it is defined and used.
@@ -48,17 +48,18 @@ Within expressions, you can let a <EM>predicate</EM> determine whether
4848a <EM >consequent expression</EM >
4949gets evaluated or an <EM >alternative expression</EM >. This is done by writing,
5050for example
51- <PRE ><CODE >return p(x) ? 7 : f(y); </CODE ></PRE >
51+ <PRE ><CODE >return 7 if p() else f(y)</CODE ></PRE >
5252Read more on conditional expressions in
5353<a href =" https://sourceacademy.org/sicpjs/1.1.6 " >section 1.1.6
5454Conditional Expressions and Predicates</a >.
5555<EM >Conditional evaluation</EM > is also possible within statements, for
5656example the body of a function declaration. For that, you can use <EM >conditional
57- statements</EM >, for example:<PRE ><CODE >if (p(x)) {
58- return 7;
59- } else {
60- return f(y);
61- }</CODE ></PRE >
57+ statements</EM >, for example:
58+ <PRE ><CODE >if p(x):
59+ return 7
60+ else:
61+ return f(y)
62+ </CODE ></PRE >
6263Read about <EM >conditional statements</EM > in
6364<a href =" https://sourceacademy.org/sicpjs/1.3.2 " >section 1.3.2
6465Function Definition Expressions</a >.
@@ -67,56 +68,57 @@ Function Definition Expressions</a>.
6768
6869A function declaration is a statement that declares a name and binds it
6970to a function. For example
70- <PRE ><CODE >function square(x) {
71- return x * x;
72- } </CODE >
71+ <PRE ><CODE >def square(x):
72+ return x * x
73+ </CODE >
7374</PRE >
7475declares the name ` square ` and binds it to a squaring function, so that it can be applied
75- as in ` square(5); ` . You can read about function declaration statements in textbook
76+ as in ` square(5) ` . You can read about function declaration statements in textbook
7677<a href =" https://sourceacademy.org/sicpjs/1.1.4 " >section 1.1.4 Functions</a >.
7778
7879Sometimes, it's not necessary to give a name to a function: You may
7980want to create a function only to pass it to some other function as argument.
80- For that, Source
81+ For that, Python §
8182supports function definition expressions. For example
82- <PRE ><CODE >(x => x * x)(3); // returns 9</CODE >
83+ <PRE ><CODE >(lambda x: x * x)(3) // returns 9</CODE >
8384</PRE >
8485creates a square function just like the function declaration above,
8586but does not give it a name.
8687Its only purpose it to be applied to the number 3. See also
8788textbook
8889<a href =" https://sourceacademy.org/sicpjs/1.3.2 " >section 1.3.2 Function Definition Expressions</a >.
8990
90- ### Blocks
91-
92- Blocks make up the bodies of functions and the consequent and alternative statements of
93- conditional statements. You can use blocks also elsewhere in your program, if you
94- want to declare constants local to a specific scope. For example in this program
95- <PRE ><CODE >const a = 1;
96- {
97- const a = 2;
98- display(a);
99- }
100- display(a);</CODE >
91+ ### Scope of declaration
92+
93+ A variable declared outside a function, the variable has a global scope. If you want to
94+ declare a variable local to a specific scope, you can declare it in a function.
95+ For example in this program
96+ <PRE ><CODE >a = 1
97+
98+ def local_scope():
99+ a = 2
100+ print(a)
101+ local_scope()
102+ print(a)</CODE >
101103</PRE >
102- the first application of ` display ` shows the value 2, because the
103- declaration <B >const </B > ` a = 2; ` re-declares the constant ` a ` .
104+ the first application of ` print ` shows the value 2, because the
105+ <B >assignment </B > ` a = 2 ` re-declares the variable ` a ` .
104106However, the second application
105- of ` display ` shows the value 1, because
106- the declaration <B >const </B > ` a = 2; ` is limited in scope by its surrounding block .
107- You can read more about <EM >blocks </EM > in
107+ of ` print ` shows the value 1, because
108+ the <B >assignment </B > ` a = 2 ` is limited in local scope by defining it in the function .
109+ You can read more about <EM >scope </EM > in
108110<a href =" https://sourceacademy.org/sicpjs/1.1.8 " >section 1.1.8
109111Functions as Black-Box Abstractions</a >.
110112
111113### Boolean operators
112114
113- Boolean operators in Source have a special meaning. Usually, an operator combination
115+ Boolean operators in Python § have a special meaning. Usually, an operator combination
114116evaluates all its arguments and then applies the operation to which the operator refers.
115- For example, ` (2 * 3) + (4 * 5) ` evaluates ` 2 * 3 ` and ` 4 * 5 ` first, before the addition
116- is carried out. However, the operator ` && ` works differently. An expression
117- ` e1 && e2 ` should be seen as an abbreviation for ` e1 ? e2 : false ` . The expression
118- ` e2 ` only gets evaluated if ` e1 ` evaluates to ` true ` . The behaviour of ` || ` is similar:
119- ` e1 || e2 ` should be seen as an abbreviation for ` e1 ? true : e2` . More on these
117+ For example, ` (2 * 3) + (4 * 5) ` evaluates to ` 2 * 3 ` and ` 4 * 5 ` first, before the addition
118+ is carried out. However, the operator < B >and</ B > works differently. An expression
119+ ` e1 and e2 ` should be seen as an abbreviation for ` e2 if e1 else False ` . The expression
120+ ` e2 ` only gets evaluated if ` e1 ` evaluates to ` True ` . The behaviour of < B >or</ B > operator is similar:
121+ ` e1 or e2 ` should be seen as an abbreviation for ` True if e1 else e2` . More on these
120122two boolean operators in textbook
121123<a href =" https://sourceacademy.org/sicpjs/1.1.6 " >section 1.1.6 Conditional
122124Expressions and Predicates</a >.
@@ -125,12 +127,12 @@ Expressions and Predicates</a>.
125127
126128A program or the body of a block does not need to consist of a single statement.
127129You can write multiple statements in a row. In the REPL ("Read-Eval-Print-Loop")
128- of a Source implementation, you can write
129- <PRE ><CODE >cube(7);
130- square(5); </CODE ></PRE >
130+ of a Python § implementation, you can write
131+ <PRE ><CODE >cube(7)
132+ square(5)</CODE ></PRE >
131133The statements in such a sequence are evaluated in the given order. The
132134result of evaluating the sequence is the result of evaluating the last
133- statement in the sequence, in this case ` square(5); ` .
135+ statement in the sequence, in this case ` square(5) ` .
134136Read more about sequences in
135137<a href =" https://sourceacademy.org/sicpjs/1.1.2 " >section 1.1.2
136138Naming and the Environment</a > of the textbook.
@@ -139,6 +141,6 @@ Naming and the Environment</a> of the textbook.
139141
140142For our development team, we are maintaining a definitive description
141143of the language, called the
142- <a href =" ../source_1 .pdf " >Specification of Source §1</a >. Feel free to
144+ <a href =" ../python_1 .pdf " >Specification of Python §1</a >. Feel free to
143145take a peek.
144146
0 commit comments