Python Flashcards
Functions Have Scope
Functions define their own universes, or contexts.
They are their own pieces of scratch paper.
They have values handed in via arguments. Inside the function, any variable names can be used, regardless of whether there is a variable outside with
some value assigned to it.
When the function is done, it can return a value; the
scratch paper is always thrown away.
Iteration Allows Repeated Computation
whatever can be done to an element of a sequence (such as a list) can be done to every element, using the ‘for’ statement.
Be able to take any function that works on one element and apply it to every element in a list. You really must be
able to generalize computation in this way or it won’t be possible to progress much further. Finally, be able to accumulate a result, in the way that double() does by
building a list, in the way positive() does by counting, and in the way pluses() does by accumulating a total.
Types Distinguish Data
- There are different types of data. You can determine the type of a particular value held in a variable.
- different computations apply to different types—some only work on sequences such as lists, some only work on numbers
- some functions and arithmetic operators are polymorphic and apply to different types. Have a good sense of the essential differences between integers, floating point numbers, strings, Boolean values, and
sequences.
Equality Can Be Tested
- Just as Python will evaluate arithmetic expressions such as 2 + 2 for you, it will also evaluate equations such as 2 == 2, as this one would be written in Python.
- Understand that the result will be one of the two Boolean values, and that you can check to see if a variable has a particular value using the equality operator, ==.
The Conditional Selects Between Options
- The “if” in Python allows for one computation to be done in one case and another computation (or no computation at all) to be done otherwise.
- There are several formulations of the conditional.
- Understand the essential ability of “if” to determine whether or not a condition holds and to apply computation if it does.
abstraction
capturing the essence (of something while not getting bogged down in details)
(When writing a program, you may find that in different places and in different contexts, you are doing essentially the same computation ten times. For instance, you might need to take a string that contains a first name, a last name (perhaps a compound one), and possibly one or more middle names, and break it into its component parts.
The first relevant type of abstraction allows the
programmer to create one function that BUNDLES CODE TOGETHER together and expresses this essential computation; that function can then be used ten times)
More than just calculation - they imbue/eable significant capabilities enable in working with computation: fundamental abstractions discussed in chapter 6 are iterations, polymorphism, types, conditional
iteration
- the program should do the same thing “for” each element
a programmer can write a program that goes through a list of this data (rather than a single piece of data)
- Iteration can be used to generalize a computation over a sequence.
function
abstracts a bunch of code
has to follow a strict syntax
- using word def, choosing a name for the function, parentheses assigning an argument, colon
- calling a function in code means sending it some arguments so that a result can be returned
return
The keyword used to provide a value at the higher level, where a function is called, when the function’s execution is complete.
refactoring.
The process of improving the clarity and/or efficiency of code without changing the essential computational task that it carries out. By refactoring, the programmer is not trying to make the code do something else, but to make it do whatever it does in a way that is more understandable and extensible.
polymorphism
The ability of code, and particularly operators, functions, and methods, to work properly on different data types.
In Python, + works to add numbers together (including both integers and floating-point numbers) while it also works to concatenate, or join, strings.
nested
Refers to placing code within a control structure such as a loop.
To visit every pixel in a two-dimensional image, a program will typically use nested loops that, for instance, go through
every possible y value and for each such value go through every possible x value. For a three-dimensional data structure, another level of nesting can be added.
list
A data type that is a mutable sequence, very commonly used in Python but also central to several other languages
The shortest list, [], is the empty list. If a list has elements, and more than one, they are separated by commas between the brackets, as in [34, 28, 23, 14, 8].
Elements of lists can be removed and new elements
appended; lists can be concatenated together; and lists can be easily sorted.
iterate.
To progress through a loop, repeatedly executing the code in the loop. “iter” means “same,” but while the same code is executed, the values of parameters very often differ.
Iteration can be used to count the vowels in a string, for instance.
Nested iteration can allow a program to proceed through all the pixels in a two-dimensional image, for instance, figuring out the average brightness of the image.
pattern
for ____ in _____:
____
intentional
A working program that does what it is supposed to do at the current stage of its development.
Just because a program is valid, and does something, does not mean that it is intentional. This term, unlike valid, is idiosyncratic, but the concept is quite important.
integer
A number, positive, negative, or zero, that does not have a decimal point followed by digits. 17, -69105, and 0 are examples, while 3.333333 is not.
Numbers with decimal points can be represented as floating-point numbers.
function
A bundle of computation that accepts zero or more values as inputs and returns zero or one values as outputs. In mathematics, a function is a mapping from some domain (for instance, real numbers representing Fahrenheit temperatures) to a codomain (for instance, real
numbers representing Celsius temperatures) in which each input has a unique output (which is true in the case of this conversion, because no two Fahrenheit temperatures convert to same Celsius temperature).
In computing, however, there is no restriction of this sort for functions. A function capital() can return True for every capital letter and False otherwise; the inputs do
not need to each map to a unique output. In fact, functions in computing do not need to return a value at all. Functions are usefully thought of as encapsulations of code that could have occurred elsewhere in the program. They are very similar to methods, which are part of classes or
objects and thus can be encapsulted with data.
free software.
A concept distinct from “no-cost” software, free software includes several fundamental freedoms, including the freedom to use the software for any purpose and the
freedom to inspect, study, and modify it.
floating point
A type of number representation that can have a decimal point and is of variable precision.
Things that can be counted are probably better represented as integers, but the mean of several numbers, for instance, is almost certainly better represented as a floating point number.
conditional.
The “if” statement, which allows computation to be done only in the case where some condition is true.
command line
The text-based environment for computer use, allowing the same sorts of things that are done with the GUI (graphical user interface) to be done by typing commands.
On GNU/Linux and Mac OS, the command line is usually accessed via a terminal; on Windows, the Command Prompt is used. The Anaconda Prompt on Windows is also a command line.
casting
Changing the type of some data, for instance by making an integer variable num into a floating point number with float(num) or by making that integer variable into a string with str(num).
Boolean
A type that can have either a True or False value.
Such a value results from checking to see if an equality (such as x == y) or inequality (such as x > y) holds.
Unlike other types, this one is capitalized, because it is named after a person, George Boole.
IDE
(integrated development environment).
A special application for writing computer programs, allowing a programmer to easily run and debug them.