The most important idea in Computer Science is that complicated, useful things can be built by putting together simple parts according to simple rules. Python code is an important example of this principle. Once you understand the basic rules, you can code with confidence. These exercises are designed to give you some practice with those rules.
First, a brief review of sub-expressions.
You can take any Python expression that has a value and combine it with other expressions. For example, you can combine two number-valued expressions by putting a + between them to add their values together. This forms a new, larger expression called a compound expression. The expressions that were combined together are called sub-expressions. Any smaller part of an expression, that is in itself an expression, is a subexpression.
You can tell if something is a sub-expression by checking whether it would make sense to write it in a line by itself. For example, in the expression 2 * 3, 2 is a sub-expression, but 2 * isn't, because 2 * isn't a valid expression. (Try executing it!)
Question 1. List all the sub-expressions of the following expression:
2 + 3
Put each sub-expression on its own line in the next cell.
Hint: There are two of them.
...
...
Question 2. Expressions with variables.
Let us say that I have defined two variables, like this:
a = 10
b = 5
Now list all the sub-expressions of the following expression:
a + b
Put each sub-expression on its own line in the next cell.
...
...
Question 3. List all the sub-expressions of the following expression:
(a + 3) / b
Here is a list of almost all the sub-expressions of that expression. One is missing.
a
3
b
In the next cell, write the missing expression.
...
Question 4. Consider the following expression:
(1 + 2) * ((3 / 4) ** 5)
Here is a list of almost all the sub-expressions of that expression. One is missing.
1
2
3
4
5
(1 + 2)
((3 / 4) ** 5)
In the next cell, write the missing expression.
...
Question 5. List all the sub-expressions of the following expression:
(((2 ** 3) / 4) / 5) - 6
Put each sub-expression on its own line in the next cell.
Hint - there are eight of them.
...
...
...
...
...
...
...
...
Question 6.. Where are the variables now?
Consider the following cell. Execute it, to show the value of y.
x = 10
y = x * 3
x = 100
y
Why is y
equal to 30, and not 300? Explain in the text cell below.
(Remember, double-click the cell to edit it, and then press shift-enter when
you're done.)
Write your answer here, replacing this text.
Question 1. When you run the following cell, Python will produce a slightly cryptic error message. Explain in the text cell below, in your own words, what's wrong with the code.
4 = 2 + 2
Write your answer here, replacing this text.
Question 2. When you run the following cell, Python will produce another slightly cryptic error message. Fix the error, and then explain below in your own words what was wrong with the code.
two = 2
four = two plus two
Write your answer here, replacing this text.