Python §1 is a small programming language, designed for the first chapter of the textbook Structure and Interpretation of Computer Programs, JavaScript Adaptation (SICP JS).
What names are predeclared in Python §1?
On the right, you see all predeclared names of Python §1, in alphabetical order. Click on a name to see how it is defined and used. They come in these groups:
What can you do in Python §1?
You can use all features that are introduced in chapter 1 of the textbook. Below is the list of features, each with a link to the textbook section that introduces it and a small example.
Literal values
Literal values are simple expressions that directly evaluate to values. These
include numbers in the usual decimal notation, the two boolean values
True and False, and the value None.
More on literal values in section
1.1 The Elements of Programming of the textbook.
Declaration assignments
Variables are declared in Python §1 with declaration assignments such as
my_name = x + 2.
In this statement, the variable my_name is bound to the result
of evaluating x + 2.
You can read more about the scope of names in
section 1.1.8
Functions as Black-Box Abstractions.
Conditional statements and conditional expressions
Within expressions, you can let a predicate determine whether a consequent expression gets evaluated or an alternative expression. This is done by writing, for example
return 7 if p() else f(y)
Read more on conditional expressions in section 1.1.6 Conditional Expressions and Predicates. Conditional evaluation is also possible in statements, for example the body of a function definition. For that, you can use conditional statements, for example:
if p(x):
return 7
else:
return f(y)
Read about conditional statements in section 1.3.2 Constructing Functions using Lambda Expressions.
Function definitions and lambda expressions
A function declaration is a statement that declares a name and binds it to a function. For example
def square(x):
return x * x
declares the name square and binds it to a squaring function, so that it can be applied
as in square(5). You can read about function declaration statements in textbook
section 1.1.4 Functions.
Sometimes, it's not necessary to give a name to a function: You may want to create a function only to pass it to some other function as argument. For that, Python § supports function definition expressions. For example
(lambda x: x * x)(3) // returns 9
creates a square function just like the function declaration above, but does not give it a name. Its only purpose it to be applied to the number 3. See also textbook section 1.3.2 Function Definition Expressions.
Scope of declaration
A variable declared outside a function, the variable has a global scope. If you want to declare a variable local to a specific scope, you can declare it in a function. For example in this program
a = 1
def local_scope():
a = 2
print(a)
local_scope()
print(a)
the first application of print shows the value 2, because the
assignment a = 2 re-declares the variable a.
However, the second application
of print shows the value 1, because
the assignment a = 2 is limited in local scope by defining it in the function.
You can read more about scope in
section 1.1.8
Functions as Black-Box Abstractions.
Boolean operators
Boolean operators in Python § have a special meaning. Usually, an operator combination
evaluates all its arguments and then applies the operation to which the operator refers.
For example, (2 * 3) + (4 * 5) evaluates to 2 * 3 and 4 * 5 first, before the addition
is carried out. However, the operator and works differently. An expression
e1 and e2 should be seen as an abbreviation for e2 if e1 else False. The expression
e2 only gets evaluated if e1 evaluates to True. The behaviour of or operator is similar:
e1 or e2 should be seen as an abbreviation for True if e1 else e2. More on these
two boolean operators in textbook
section 1.1.6 Conditional
Expressions and Predicates.
Sequences
A program or the body of a block does not need to consist of a single statement. You can write multiple statements in a row. In the REPL ("Read-Eval-Print-Loop") of a Python § implementation, you can write
cube(7)
square(5)
The statements in such a sequence are evaluated in the given order. The
result of evaluating the sequence is the result of evaluating the last
statement in the sequence, in this case square(5).
Read more about sequences in
section 1.1.2
Naming and the Environment of the textbook.
You want the definitive specs?
For our development team, we are maintaining a definitive description of the language, called the Specification of Python §1. Feel free to take a peek.