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.
An alias is a name of a value that has a second, distinct name.
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.
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.
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.
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.
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>
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
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-time 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.
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.
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.
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.
A comparison operators is an operator that takes two comparable objects and yields a Boolean. The comparison operators are:
Equality: ==
Inequality: !=
Greater Than: >
Greater Than or Equal: >=
Less Than: <
Less Than or Equal: <=
Two strings or two lists are concatenated when they are joined together into a single string or list. String concatenation is achieved with the + operator. For instances:
>>> 'spam' + ' and ' + 'eggs'
'spam and eggs'
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
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 >>>.
A variable whose value will remain unchanged through the whole of a program's execution. Such variables are often written in all caps.
See iteration.
Really a bottom just a text editor. You write your programs there and then when you choose you run them.
Collection data types are collections of their elements; that is, an element is a member of the collection. For instance, 1 is an element of the list [1, 2, 3].
We say that an object of a collection data type (like a list, string or dictionary) is empty when it contains no elements. Alternatively, we say that it is empty when its length is 0.
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".
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.
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
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.
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.
The concept of function is complex.
A function is a named block of code.
The first line of a function is its header. Its form is def func_name(parameter1, parameter2, ...). def is the required keyword. func_name is a variable of your choice. The parameters that follow are variables that will be assigned the arguments sent in a function call.
The execution of a function results from a function call. A function is called by an occurrence of the function name followed by parentheses that (typically) enclose a value or values. Example: a_func(1, 2, 3).
When a function is called, the values contained in parentheses after the function name are sent to the function's parameters. These values are the arguments of the function call.
A function has a body. This is the indented block of code that computes the function's output.
A function's output is the value of the expression that follows the keyword return.
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.
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.
A function that either takes a function as an argument or returns a function as its value.
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.)
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.
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.
See iteration.
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.
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.
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 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.
A word that has a predefined use in Python; as such, keywords, though they might meet the criteria for valid variable names, cannot be used as variables. Examples: and, or, not, if, elif, else, for, in, while, def, class.
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']]
Python's native representation of a value. As such, literals do not require evaluation to take on a value. Examples: 2, 2.0, and "two". Any expression that requires evaluation is a non-literal. Example: PI * (r**2), for to reach a value, we must substitute in values for PI and r and then perform the indicated operations.
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 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.
In definite iteration, it is the variable between the keywords for and in. Its values will step through the sequence that comes after in.
The usual name for a function contained in a class definition. Methods are typically called with the dot operator - object name followed by the dot operator followed by the method name. Example: aString.capitalize() (which capitalizes the first character in aString).
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.
Some data types are mutable. This includes list and dictionaries. To mutate an object of a mutable data type means to change one or more of its attributes. For a list, this means to either add an element, remove an element or change the element at a particular position. For a dictionary, this means to either add a key-value pair, remove a key-value pair, or change the value associated with a given key.
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.
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.
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.
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.
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.
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:
Reads the line typed in.
Evaluates it.
Prints that value to the console (if indeed that's possible).
Loops back to the console prompt.
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.
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 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.
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".
Lists may also be sliced. The syntax is just the same as for strings.
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.
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.
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.
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!
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.
Statements either control the flow of execution or bring about a change in the program's state. We say that statements are executed.
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.
A shortened form. Done to appease programmers who begrudge every unnecessary key click. For instance, c += 1 is syntactic sugar for c = c + 1.
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.
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.
The tuple data type is a collection data type; that is, it collects together multiple items into a single object.
The tuple formation operator is a comma. Thus 1, 2 is the tuple that is a 1 followed by a 2.
Tuples may contain elements of multiple types. Unlike lists, tuples are immutable.
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.
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.