Chapter 2 Using Python as a Calculator

2.6 Identifiers, Variables and Assignment

Arithmetic expressions allows simple calculations using IDLE. For example, suppose that the annual interest rate of a savings account is 4%. To calculate the amount of money in the account after three years, with an initial sum of 3,000 dollars is put into the account, the following expression can be used.

One side note is that parentheses can be used to explicitly mark the intended operator precedence, even if they are redundant. In the case above, 3000 ∗ 1.04 ∗∗ 3 can be written as 3000 ∗ (1.04 ∗∗ 3) to make the operator precedence more obvious. In general, being more explicit can often make the code easier to understand and less likely to contain errors, especially when there are potential ambiguities (e.g. non-intuitive or infrequently used operator precedence).

For a second example, suppose that the area of a square is \(10m^2\). The length of each edge can be calculated by:

The result can be rounded up to the second decimal place.

For notational convenience and to make programs easier to maintain, Python allows names to be given to mathematical values. An equivalent way of calculating the edge length is:

In the example above, a denotes the area of the square, and w denotes its width. The use of a and w makes it easier to understand the underlying physical meanings of the values. a and w are called identifiers in Python. Each Python identifiers is bound to a specific value. In the example, a is bound to 10 and w is bound to \(\sqrt{10}\). Identifiers can be bound to new value:

In the example, the value of x is first 1, and then 2. Because identifiers can change their values, they are also called variables.

The = sign in the above example is not an operator, and hence the commands a = 10 and w = round(a ∗∗ 0.5) are not expressions. They bare no values. Instead, the = sign denotes an assignment statement, which binds an identifier to a value. Here a statement is a command to be executed by Python, and statements are the basic execution units in Python. There are different types of statements, as will be introduced in this book. In an assignment statement, the identifier to which a value is assigned must be on the left hand side of =, and the value to assign to the identifier, which can be any expression, should be on the right hand side of =. Python gives a name to a value by binding the value to an identifier.

Table 2.1 List of Keywords in Python

and

as

assert

break

class

continue

def

del

elif

else

except

exec

finally

for

from

global

if

import

in

is

lambda

not

or

pass

print

raise

return

try

while

with

yield

An intuitive difference between identifiers and literals is that the former are names while the latter are values. Formally, an identifier must start with a letter or underscore (_), and contain a sequence of letters, numbers and underscores. For example, area, a, a0, area_of_square and _a are all valid identifiers, but 0a, area of square or a1! are not valid identifiers. An additional rule is that identifiers must not be keywords in Python, which are a list of reserved words. There are 31 keywords in total, which are listed in Table 2.1. Each keyword can be associated with one or more statements, which will be introduced later.

For another example problem, suppose that a ball is tossed up on the edge of a cliff with an initial velocity v0, and that the initial altitude of the ball is 0m. The question is to find the vertical position of the ball at a certain number of seconds t after the toss. If the initial velocity of 5 m/s and the time is 0.1 s, the altitude can be calculated by:

To further obtain the vertical location of the ball at 1s, only t and h need to be modified.

Note that the value of h must be calculated again after the value of t changes. This is because an assignment statement binds an identifier to a value, rather than establishing a mathematical correlation between a set of variables. When h = v0 ∗ t−0.5 ∗ g ∗ g ∗ t ∗∗ 2 is executed, the right hand side of = is first evaluated according to the current values of v0, g and t, and then the resulting value is bound to the identifier h. This is different from a mathematical equation, which establishes factual relations between values. When the value of t changes, the value of h must be recalculated using h = v0 ∗ t − 0.5 ∗ g ∗ g ∗ t ∗∗ 2. For another example,

There are three lines of code in this example. The first is an assignment statement, binding the value 1 to the identifier x. The second is another assignment statement, which binds the value of x + 1 to the identifier x. When this line is executed, the right hand side of = is first evaluated, according to the current value of x. The result is 2. This value is in turn bound to the identifier x, resulting in the new value 2 for this identifier. The third line is a single expression, of which the value is displayed by IDLE. Think how absurd it would be if the second line of code is treated as a mathematical equation rather than an assignment statement!

An equivalent but perhaps less ‘counter-intuitive’ way of doing x = x + 1 is x+= 1.

The same applies to x = x − 3, x = x ∗ 6, and other arithmetic operators.

In general, x<op>=y is equivalent to x=x<op>y, where <op> can be +, −, ∗, /, % etc. The special assignment statements +=, −=, ∗=, /= and %= can be used as a concise alternative to a = assignment statement when it incrementally changes the value of one variable.

Given the fact above, it is not difficult to understand the outputs, if the following commands are entered into IDLE to find the position of the ball after 3s in the previous problem.

The commands above are executed sequentially and individually. When t is bound to the new value 3, h is not affected, and remains 0.09. After the new h assignment is executed, its value changes to −29.15 according to the new t. Cascaded assignments. Several assignment statements to the same value can be cascaded into a single line. For example, a = 1 and b = 1 can be cascaded into a = b = 1.

Check your understanding

Construct the code that will result in the value 134 being printed.

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.

© Copyright 2024 GS Ng.

Next Section - 2.7 The Underlying Mechanism