Python Flashcards

1
Q

Functions Have Scope

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Iteration Allows Repeated Computation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Types Distinguish Data

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Equality Can Be Tested

A
  • 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, ==.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The Conditional Selects Between Options

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

abstraction

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

iteration

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

function

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

return

A

The keyword used to provide a value at the higher level, where a function is called, when the function’s execution is complete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

refactoring.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

polymorphism

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

nested

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

list

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

iterate.

A

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 _____:
____

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

intentional

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

integer

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

function

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

free software.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

floating point

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

conditional.

A

The “if” statement, which allows computation to be done only in the case where some condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

command line

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

casting

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Boolean

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

IDE

A

(integrated development environment).

A special application for writing computer programs, allowing a programmer to easily run and debug them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

programming

A

The writing of programs, from scratch or by modifying existing code.
Programming can be exploratory or done to implement a pre-determined idea.

In all cases but the very simplest, programming is an iterative process in which code is tried out, revised,
refactored, and expanded. The programmer uses modularity, reuse of existing code, libraries, and
other kinds of abstraction to accomplish goals, whether these goals are directly instrumental or
are ones of art or inquiry.

26
Q

sequence

A

An ordered collection of elements, each of which can be accessed by index or by slicing. In Python, strings, lists, and tuples are sequences

27
Q

slice

A

A portion of a sequence accessed by placing [a:b:c] after the sequence, where a is the (optional) starting index, b is the (optional) end index, and c is the (optional) step.

Slicing is forgiving, so if there are no characters or elements in the specified slice, a null sequence will be returned and no error will be thrown.

28
Q

string.

A

A sequence of characters,
one that can be accessed by index and can be sliced.

This is a data type well-suited to representing the sort of texts that interest humanists and literary artists.

In Python, the string is one of several sequences, along with the list and the tuple.

29
Q

type.

A

The overall category or kind of data that is being represented.

For instance, ‘hello’ is of the type string, while 17 is of the type integer.

Types are used to check to see if certain operations
or computations are sensible. For instance, attempting to divide 17 by ‘hello’ is almost certainly a mistake on the programmer’s part.

A type system can help to catch such errors soon
after they are made, making program development easier.

30
Q

value.

A

A specific piece of data that can be output or used in a computation.

For instance, if a program should always stop after processing ten items, it might contain if (count < 10). In
this line, count is a variable while 10 is a value. When this code is run, the variable count will (most likely) take on different values each time execution reaches this line.

31
Q

variable

A

A variable can “vary” in that it can represent many different values.

For instance, the variable count can first have the value 1, then 2, then 3, and so on. Variables can be used in expressions. Variables can have any names the programmer chooses; the functioning of the program is not affected, although the ability of people to
understand it may be

32
Q

null string.

A

The string with no characters in it, and thus a length of 0.

The list that corresponds to the null string is the emtpy list.

Usually one reads of “the” null string because there is only one: The number of ways that zero characters can be included in a string is one.

33
Q

index

A

The number corresponding to a particular character in a string or a particular element in a list or array.

Index 0 corresponds to the first character or element, the one that is offset 0 from the beginning.

34
Q

printing

A

simple type of iteration
displaying every element of a list on a screen

l = [7, 2, 6, 5]

for num in l:
print (num)

not return a value that can be used in further computation. It just displays data for a person to read

35
Q

iteration pattern/template

A

for ____ in _____:
____
In the first blank is the variable that will be used to hold each element. It can be called almost anything—num is used in the example above, but it might be i or
element.

In the second blank is the sequence to iterate through

:, a colon. This is necessary to indicate that everything that is indented underneath is part of this iteration,

In the third blank—and possibly occupying more than one line of code, all within this same level of indentation—is whatever we want done during each iteration

for num in l:
print(num * 2)

36
Q

mean

A

is the number that is the sum of all data points of a list divided by the number of data points

For two points, it’s simply the sum of those two numbers divided by two.

37
Q

problematise

def mean(sequence):
   for element in sequence:
      total = total + element
   return total / len(sequence)
A

The problem is that this code instructs the computer to use the value total (on the right hand side of total = total + element) before any value has been assigned
to it. It’s like asking the computer for the answer to temperature - 80 before you have given any value to temperature.

def mean(sequence):
  total = 0
  for element in sequence:
      total = total + element
  return total / len(sequence)

The first thing done in the new version of the function is seen in the line total = 0.
That line is initializing the variable total. It assigns the value 0 to total, which is
indeed the value that should be in there before anything has been accumulated.

38
Q

to check a type of data

A

x = 5

we can check to see what type of value the variable x holds by using the built-in
type() function:
type(x)

OR

x = ‘hello world’
type(x)

39
Q

concatenation

A

The operation of putting two strings together is called concatenation and is done in Python as addition, using +

‘hello’ + ‘world’

You can also assign the strings you are using to variables and then concatenate the
variables:
a = 'hello'
b = 'world'
a + b
40
Q

len()

A

function, which provides the length of its
argument.

len(‘hello world’)
len([1, 2, 3])

returns numbers of characters in a string or elements in a list.

works on both strings and lists, so is polymorphic function

41
Q

to concatenate / cast integers as string

A

len(str(5))

or

42
Q

DRY

A

(don’t repeat yourself).

43
Q

double function

A
def double(sequence):
   result = []
   for element in sequence:
        result = result + [element * 2]
    return result

was written for sequences, polymorphism, can deal with ‘strings’ and ,,lists

44
Q

the conditional function e.g.

A
def secret(word):
  if word == 'please':
      return 'Yes!'
45
Q

shell

A

basic interface on the command line

as the top window of your file viewer shows a particular folder’s contents, your shell is also context-dependent.
Your commands apply based on your current location within the hierarchy of your computer’s file system. It’s possible to refer to any file at any point, but if you’re interested in working with files in a particular
directory, it’s easiest to change directory so that you are “there”

46
Q

pwd

A

print working directory

command in terminal that doesn’t’ move’ you but shows which directory you are currently in

47
Q

cd

A

change you to your home directory

or any directory e.g.. cd Desktop

48
Q

ls

A

list directory contents

49
Q

python hi.py

A

This is the same python command that can be used by itself to start the standard Python interpreter, although we are using the more powerful Jupyter Notebook.

Here, we add something to this command—an argument. This is an extra piece of data for the python command to work with.

Although the shell and Python are not the
same environment, this command-line argument is really quite similar to the argument that “Double, Double” accepts within the interpreter, within Python.

50
Q

terminal / command line

A

command line. The Terminal or
Command Prompt is an entirely different way to access your computer—more powerful in some ways than the GUI and not based on all of the same principles or metaphors

51
Q

raise error

A
def to_f(c):
  if c < -273.15:
      raise ValueError("Temperature value is below absolute       zero.")
return ((9/5) * c) + 32
52
Q

elif

A

form of conditional
The elif combines an else and an if, indicating “otherwise, check to see if…”

def sign(num):

answer = ‘?’

if num > 0:

answer = ‘+’

elif num < 0:

answer = ‘-‘

else:

answer = ‘’

return answer

In this case, the conditional
tests to see if the number is positive, and only if it isn’t proceeds to test whether it is
negative, and then, without doing any additional tests (“otherwise…”), proceeds to
the case that applies when the number is zero.

53
Q

regular expression

A
  • re a formal language in themselves
  • regular expressions allow you to define pattern so as to search for not just one string but a category of strings
  • useful for text processing, finding particular strings within a text
  • search eg.email inbox using text editors, python, command line
54
Q

Regular expression define a character class

A
  • to define character class use brackets [ ]

- think about whether it includes lower and upper case characters and include both if needed AEIOU aeiou

55
Q

Regular Expressions

special characters

A

. (dot) means “any character except a new line”
* asterisk means any number of (whatever) / quantifier / zero or more occurrences
- square brackets can be used to ‘specify a set of characters’ [0-9] will match any numeral, any decimal
digits.
\ backslash special characters used to indicate things like newline or word boundaries
\s indicates whitespace \w indicates ‘word characters’ that is alpha numeric characters (!)

If you want all non-space characters, use \S. Similarly,
\D matches non-digits and
\W matches non-word characters
\b word boundary

56
Q

literals

A

I literally want to search for this literal thing e.g.. dance
you don’t need Regular expressions to search for literals, they become useful when combination with other elements

57
Q

If you want to match particular letters such as ‘a’ or ‘p’

A

place them directly in the brackets: [ap]

58
Q

everything but 0

A

[^0]

59
Q

[^A-F]

A

means everything but the capital letters A through F.

60
Q

. dot

A

everything but a newline

61
Q

quantifiers

*
+
?
{3,7}

A

asterisk “zero or more of these”
“one or more of these.”
“zero or one of these”
in other words, the part of the pattern to which this
quantifier applies is optional. Thus travell?er will match either spelling of this word, “traveler” or “traveller.”

62
Q

Kha{3,7}n!

A

specifies that the pattern must occur at least m times but no more than n times

which will match “Khaaan!” as well as
“Khaaaaaaan!

For instance, the pattern Khaaaa?a?a?a?n! works exactly the same way as that previous pattern