Putting it together again

Oorspronkelijk geschreven op 2015-02-08 14:48

Now that we've seen some of the components let's look at how they're combined into build meaningful sentences.

As mentioned, we have three categories: keywords (and symbols), names, and values. Some examples of putting these together:

max_value = 500
if testing == True:
print('My house is bigger than yours')

In the first example, max_value is a name, 500 is a value and = is a symbol that binds the name to the value.

In the second, if is a keyword that indicates a condition to be tested, testing and True are names and == is a symbol that compares the values associated with the names on either side. : is another symbol that - in this case - designates that the code following is to be executed when the condition is satisfied (Note that this is actually a bad code example: firstly, when comparing to the name True we generally don't use the value comparison operator == but the identity comparison operator is; and secondly, in Python each value is regarded as either true or false so we don't have to explicitely compare against these).

In the third example, print is a name for a standard function, ( and ) are symbols as used in other languages that indicate that the function is being called with the argument(s) in between them, and the text between quotes is a data value that is used as the function argument, and in this case is being sent to the standard output stream.

You'll notice that I introduced a few new terms; this is because when they are combined into sentences, the components acquire some extra value which may be called semantics: not only is there the qualification as to what they are, but also to how they are used. This is mainly true for names, which get to be called function, class, method, argument, parameter etc; and for combinations of components that are used in larger sentences, that get to be called things like expression or condition or comprehension.

DATA AND CODE

In Python, there is no fundamental difference between an name for a piece of data or a name for a piece of processing code.

Data

In a lot of computer languages, the name of a variable is more or less equivalent with the variable itself but in Python, the name is just a label and can easily replaced by another label. For example, identifying a value by a name goes like this:

name_1 = "Just another piece of text"

Now you can equate this name to another name:

name_2 = name_1

But what you're actually doing is creating a second name that identifies the same value as the first name. In other computer languages you would be copying the value and have the second name refer to the copy, but not in Python. The consequence of this is that in some cases (known as mutable values), when you do a modification on the first name, the modification is also visible when you use the second name.

Code

Similarly, in a lot of languages the name of a piece of code (e.g. a function) is more or less equivalent to the code itself and you can't use the name without calling the code (even if you have to use parentheses following the name). In Python, this is not so - which creates the possibility of "using" the piece of code without calling it. It also becomes possible to refer to the same piece of code using a different name.

Before I illustrate this, I first need to talk about something else: Building "paragraphs".

In a story written in a programming language, parts are written in the context of another part. for instance when a series of statements are executed provided a certain condition is fulfilled. Another example would be a function: a series of statements that are identified by a name so that they can be called at various moments in the story, just by sing the name. Python requires you to indent your text when you want to indicate that something is done in the context of something else. For instance:

conditional execution of code:

if True:
    test = 5
    print(test)

function definition (def indicates that a function is being defined, adder being - in this case - the name of the function)

def adder(value, increment):
    value += increment
    return value

Other constructs are of course possible (think loops or classes etc). Paragraphs can also be built inside paragraphs (nested ifs, functions inside classes etc).

next