Definitions

Abstraction

We abstract when we capture the similarity of multiple blocks of code in a single function. The similarity becomes the body of the function. The differences become parameters.

Alias

An alias is a name of a value that has a second, distinct name.

Algorithm

A sequence of instructions which if followed will achieve some goal. If of course the algorithm is properly designed, and if it is properly followed.

Argument

The values sent to a function when it's called. These are the values of the expressions contained in parentheses in the expression that calls the function.

For instance, if we call func with:

func(1, 2, 3)

the arguments of that functions call are 1, 2 and 3. The arguments are then assigned to the function's parameters.

Assignment

To give value to a variable (see definition of "variable" below); alternatively, to assign a value to a variable. Console example:

>>> a_var = 12

Here the object 12 was assigned to the variable a_var.

Assignment Operator

The = symbol as used in an assignment statement. Note that it is not the = of mathematics. For instances, the statement "x = x + 1" makes perfect sense in Python. It means to increase the value of x by 1. However in mathematics it says that the value of x is the same as the value of x + 1, which is of course impossible.

Attribute


Boolean Expressions

An expression whose value is either True or False. Boolean expressions are found within statements that control the flow of execution of a program.  Like this:

if <boolean_expr>:

    <code block>

elif <another_boolean_expr>:

    <code block>

else:

    <code block>

Boolean Operator

An operator that takes two boolean-valued expressions and yields a boolean. The three boolean operators are and, or and not. Examples:

>>> (1 > 2) and (3 > 2)

False

>>> (1 > 2) or (3 > 2)

True

>>> not(1 > 2)

True

Bug

A flaw in a program that either results in a crash or in output that is not correct. Bugs are of three types: semantic, syntactic and run-time. (Each of these has its own entry.) Syntactic and run-rime bugs are examples of what I called in Chapter 1 "stop bugs"; when Python encounters one of these, it ceases execution and outputs an error message to the console. Semantic bugs are bugs in logic. A program with only a semantic bug will execute through to the end. But it will not in all cases produce the desired output.

Class

Chapter 1 definition:

Every value is a member of a certain class, and (as will we find) the class of which a value is a member determines what may be done with it.

Chapter 10 definition:

When we create a class, we create a new type of object; and as with built-in types, we may then create specific instances of that type. Let us call these objects. Objects have attributes. The set of of an object's attributes at a moment in time is its state.  Objects also have methods.  These are functions that modify the state of an object.

Command

A line of code that directs the program to do this or that. Some commands result in a request for input, some result in output, some change the flow of control, some change the state of the program.

Comment

Anything on a line of code that follows the hashtag symbol. Comments are not themselves code. Instead they are code commentary. Their purpose is to explain the code to a human reader.

Comparison Operator

A comparison operators is an operator that takes two comparable objects and yields a Boolean. The comparison operators are: 

Concatenate

Two strings are concatenated when they are joined together into a single string. String concatenation is achieved with the + operator. For instances:

>>> 'spam' + ' and ' + 'eggs'

'spam and eggs'

Console

The console is where you speak directly to Python. You type in a line and hit return and then Python responds.

The console operates in REPL mode - Read the line typed in, Evaluate that line, Print the value of the line to the screen, Loop back to the console prompt.

The standard Python console prompt is >>>.

Constant

A variable whose value will remain unchanged through the whole of a program's execution. Such variables are often written in all caps.

Definite Iteration

See iteration.

Code Editor

Really a bottom just a text editor. You write your programs there and then when you choose you run them.

Error Message

Python's attempt to explain why it couldn't execute a line of code. Error messages are Python's last act after it encounters what I called in Chapter 1 a "stop bug".

Execute

Programs implement algorithms, and when we execute a program, it carries through the instructions that make up the algorithm. An analogy: when you execute a recipe, you follow its instructions and make a dish.

Expression

An expression is a sequence of symbols that has a value. When we evaluate an expression, we substitute values for subexpressions until what results is a literal. Example:

(2 + 3) * 4 becomes 5 * 4 becomes 20

Float

One of Python's built-in numeric types. If Python prints a number with a decimal point, that number to Python is a float.

Always assume that if a value is a float, it's at best an approximation.

Flow of Control

The order in which the statements that comprise a program are executed. This of course does not always correspond to the order of the statements from top to bottom in the program. Indeed most often the flow-of-control order and the top-to-bottom order are different. How so? Three examples. (1) When a block of code iterates, flow of control jumps back up in the top-to-bottom order. (2) When a conditional statement is encountered and its condition is not met, the associated block of code below will not execute. (3) When a function is called, flow of execution jumps to the function block.

Function, Function Header, Function Call, Parameter, Argument

The concept of function is complex. 

Example:

def pow(b, e):

    # Parameters: base b and exponent e

    # Return b raised to the e.

    result = b ** e

    return result


pow(12, 2)  # A function call. 12 and 2 will be bound to b and e respectively.

Function Composition

Let's say we have two functions, f and g; and let's say that we pass an argument x to g, and then take g's output and pass it immediately to x. If we do, we have composed the two functions: g's output becomes f's input.

In math, this is symbolized as f(g(x)); and that syntax works just fine for Python too, if f and g are defined. Note that we could compose with a different order, i.e. g(f(x)). Need I say that you should no expectation and f(g(x)) and g(f(x)) always give the same final output?

Another example in Python (assume that the sqrt and abs functions are fined):

>>> sqrt(abs(-1))

1.0

We work from the inside out. The inner function (abs) takes the input-1 and outputs 1; and then that 1 is sent to the sqrt function, which gives the final result of 1.0.

Higher Order Function

A function that either takes a function as an argument or returns a function as its value.

IDE

Short for 'Integrated Development Environment'. It's where you write, run and debug. (Want to become a programmer? That's what'll you do. Write. Run. Debug. Mostly debug.)

Immutable

We say that an object is immutable if, once it has been created, it cannot be changed. An example is a string. Once a string is created, it cannot be changed; its length cannot be changed, and no character at any index can be changed.

Increment, Decrement

To increment a variable is to increase its value by a certain amount. To decrement a variable is to decrease its value by a certain amount. One common use of variables that are incremented is to record the number of times a certain event has taken place.

Indefinite Iteration

See iteration.

Index

Every element of a sequence-type object has an index, and that index gives its position. (Two sequence-type objects are strings and lists.) In Python, the initial element of a sequence has index 0, the element after index 1, etc. Thus in Python, the index of the last element of a sequence that has length n is n – 1.

We can extract a object at a position by use of square brackets. Thus a_str[0] returns the initial character of a_str, a_str[1] returns the character after, etc.

Int

One of Python's built-int numeric types. The equivalent of "integer" from mathematics. When Python prints an int, it will not use the decimal point.

Ints are, unlike floats, exact values.

Integer Division

Python's symbol for integer division is //; a // b is the number whole times b goes into a. Examples: 12 // 4 is 3, 13 // 5 is 3, 24 // 5 is 4 and 25 // 13 is 1.

Remember divisor, dividend and quotient and remainder? The number which we divide is called the dividend. The number by which we divide is called the divisor. The number of whole times the divisor goes into the dividend is the quotient.  What remains of the dividend is the remainder. For instance, when we divide 13 by 5, 13 is the dividend and 5 is the divisor. The division results in a quotient of 2 (5 goes into 13 2 whole times) with a remainder of 3. Integer division is the quotient.

Iteration

Iteration is the repeated execution of a block of code. Typically iteration terminates. If it does not, we have an infinite loop. How does iteration terminate? In Python, have two possibilities. (1) When the iteration begins, the number of times it is to occur is set. This is definite iteration. In Python, definite iteration begins with the keyword for. (2) Before execution of the (potentially) iterated block of code, a boolean expression is evaluated. If the value is True, the block is executed; if it is not, the block is not executed. If the block is executed, then when it is done, we return again to the boolean expression and evaluate again. This is indefinite iteration. In Python, indefinite iteration begins with the keyword while.

List

One of Python's built-in collection data types. (A collection data type collects; that is, it gather multiples items in a single object.) Lists can be composed of objects of multiple types, and lists are mutable.

Lists may be formed by means of square brackets.

>>> a_list = [1, 1.0, '1', [1, 1.0, '1']]

a_list

[1, 1.0, '1', [1, 1.0, '1']]

Literal

An expression that is incapable of further simplification. 2 is a literal. 2 + 2 is not.

Local Variable, Global Variable

The value of a global variable can be accessed or changed anywhere within a program. The value of a local variable can be accessed or changed only within a limited portion of a program. In Scratch, you're given a choice to make a variable either global or local when it's created. If you make it global, all sprites can access or change its value; if you make it local, only the sprite for which it was created can access or change its value. In Python, variable created within a function are local, and variables created outside a function are global.

Logical Operators

Logical operators take Booleans and yield Booleans. Python's three logical operators are not, and and or.

Let bool1 and bool1 be two Boolean-type variables. bool1 and bool2 is True just when both bool1 and bool2 are True. bool1 or bool2 is True just when either bool1 or bool2 (or both) are True. not bool1 is True just when bool1 is False.

Loop Variable

In definite iteration, it is the variable between the keywords for and in. Its values will step through the sequence that comes after in.

Method


Modulus

Often abbreviated as "mod". Modulus is the remainder operation. x modulus y is the remainder when x is divided by y. Examples: 12 mod 4 = 0, 12 mod 5 = 2, 24 mod 5 = 4 and 24 mod 13 = 11.

Python's symbol for the mod operator is %. Thus the value of 24 % 11 is 13.

Also see integer division.

Name

In Python, names refer to values; and when Python encounters a name in an expression, it replaces it with its value. We use names to store values so that they might later be retrieved and used.

Output

Information supplied by a program to a user or device outside that program. Output flows out of a computer. Typically, output is formatted in such a way that it can be understood by the user or another computer. Examples of output devices: display, speaker, printer.

Parameters

The variables in a function header contained within parentheses. When the function is called, the values sent to the function are assigned to a function's parameters.

Consider the function:

def func(p1, p2, p3):

    pass

The parameters are the variables p1, p2 and p3.

Please distinguish parameters from arguments. Parameters are variables. Arguments are the values assigned to parameters.

Program

Programs are not algorithms. Rather programs express or implement algorithms. What does this mean? Consider this analogy. We can express an idea - the same idea - in both English and French. Thus we must distinguish the sentence from the idea it expresses. The sentences are different; the idea expressed is the same.  Likewise, we must distinguish a program from the algorithm it implements. A program in one language is never identical to a program in another - as a sentence of English is never identical to a sentence of French - but programs in different languages can implement the same algorithm. What does this word 'implement' mean? If a program implements an algorithm, then that program, if executed (and bug free), will produce the output desired of the algorithm.

Recursion

A function is recursive when it calls itself. This no doubt seems strange. If a function calls itself, then when it is called from outside the function (and all functions must have their initial call from outside the function), it will then call itself. But then since it is recursive, it will call itself again. And again. And again. And so on to infinity. Or so it seems. How then do we avoid an infinite loop when we call a recursive function? Answer: a recursive function won't always call itself. Rather, it will call itself only if a certain condition is met. When that condition is not met, the function does not call itself but instead returns a value. Consider the example below. (Assume that n is a positive integer.)

def sumtorial(n):

    if n == 1:

        return 1

    else:

        return n + sumtorial(n - 1)

The condition that triggers recursion: n greater than 1. When n does equal 1, the value 1 is returned.

The Base Case (or Base Cases) in recursion are those that do not call the function again but instead return a value. The Recursive Case (or Recursive Cases) are those that do call the function again. All recursive functions have a both.

REPL

An acronym for "Read, Evaluation, Print, Loop". This describes how Python's console works. You type in some expression after the console prompt, and then when you hit enter Python:

Run-Time Error

A bug in code that Python finds only as the program executes. In particular, it is a bug in a line that, though syntactically correct, cannot be executed for one reason or other. One example is division by 0. If for instance, a has the value 1 and by the value 0, the line of code:

a / 1

is perfectly syntactically correct. But it cannot be executed, for it has no value.

Variable Scope, Local Scope and Global Scope

In a program, the scope of a variable is that part of the program that can access (and potentially change) the value of that variable.

The scope of a variable is local to a part of a program if it can be accessed and changed only there. Note that, if a variable is assigned a value inside a function, that variable is local; its value is inaccessible once that function call has ended.

The scope of a variable is global if it can be accessed anywhere in the program.

Selection

Selection control the flow of execution. How? A condition is tested. If that condition is met, the block of code that follows is executed. If it is not met, the block of code that follows is not executed. Conditionals are often paired with elif or else type conditionals. These take over control if the original condition is not met.

The conditions mentioned above are Booleans.

Slice

A slice is a subsequence of a sequence. For instance, "ana" is a subsequence from "banana", but "baa" is not.

The slice operator in Python has the form [<start index>:<stop index>]. For instance,  "banana"[0:3] returns "ban".

String

One of Python's built-int data types. A string is a sequence of characters. One way in which to create a string is to begin and end an expression with quotation marks - "this is a string". The quotations marks thus act as the string formation operator.

The empty string is the string of length 0.

A string function is a function that either takes a string as input, returns a string as output, or both.

State

All the stored information, at a given instant in time, to which a program has access. A program stores data in variables. Thus the values of these variables, at any given time in the program's execution, is the program's state.

Syntax

The syntax of a language is the set of rules that distinguish properly structured from improperly structured sequences of symbols in that language. Syntax rules distinguish sense from nonsense.

Syntactic Sugar

A shortened form of a bit of Python syntax. For instance, a += 1 is short for a = a + 1.

We call its sugar because it's sweet not to have to type out the long form!

Semantic Bug

A program with a semantic bug (but otherwise bug-free) successfully executes but does not produce the desired output.

Code with a semantic bug is perfectly sensical code. It means something. But the something meant is not the something intended.

As is sometimes (sarcastically but hilariously) said, a program with a semantic bug doesn't do what you want it to do; instead it does exactly what you've told it to do.

Statement

Statements either control the flow of execution or bring about a change in the program's state. We say that statements are executed.

Syntactic Bug, aka  Syntax Error

A bug that results from a violation of the syntax rules of a particular language; either a undefined symbol is used, or defined symbols are combined in some illegal way (or both). A syntactic bug results in gibberish. Nothing is meant by a line of code that has a syntactic bug.

A program with a syntactic bug will not successfully execute. When the syntactic bug is encountered, the program will cease execution and output an error message.

Syntactic Sugar

A shortened form. Done to appease programmers who begrudge every unnecessary key click. For instance, c += 1 is syntactic sugar for c = c + 1.

Truncate

To cut off. When a number is truncated, we simply cut it off after a certain point and do not rounded. For instance, 3.467 truncated to two digits after the decimal point is 3.46.

Types

The various kinds of information that a language can represent. In Python, the built-in types include integer, float, Boolean, string, list, tuple, set and dictionary.

Types are general. Values are particular. Many values can be of the same type.

Value

An instance of an object type. Integer is a type, 2 is an instance of that type and thus is a value. String is a type, 'spam' is an instance of that type and so is a value.

Types are general. Values are particular. Many values can be of the same type.

Variable

Another name for "name" (see definition of "name" above). We often say that the value to which a variable refers has been assigned to that variable. Multiple variable can be assigned the same value, and a variable can be assigned a new value.