CSCI Fall 2018 Midterm Flashcards

1
Q

conditional execution

A

instruction check for certain conditions, and run their accompanying code

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

repetition

A

instruction to perform some action repeatedly, usually with some variation

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

Python interpreter

A

program that reads and executes python code

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

> > >

A

prompt. telling you that interpreter is ready for you to enter code

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

*

A

multiply

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

**

A

62 = 36
3
3=27
exponent

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

ctrl + z

A

zombies the program. stops it in its track

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

kill %n

A

kills the nth job–use carefully

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

value

A

one of the basic things a program works with, like a letter or a number. Some
values we have seen so far are 2, 42.0, and ‘Hello, World!’.

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

type

A

integers, floating-point numbers, strings

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

type()

A

gives you what type of value is inside the parentheses

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

prompt

A

Characters displayed by the interpreter to indicate that it is ready to take input
from the user.

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

interpreter

A

A program that reads another program and executes it

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

program

A

A set of instructions that specifies a computation

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

Operator

A

A special symbol that represents a simple computation like addition, multiplication,
or string concatenation.

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

int

A

type
integer
whole number

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

float

A

type

numbers with fractional parts

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

string

A

sequence of charachters

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

syntax

A

rules that govern structure of program

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

assignment statement

A

creates a new variable and gives it a value:
»> message = ‘And now for something completely different’
»> n = 17
»> pi = 3.141592653589793

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

Python key words

A
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

string concatenation

A
The + operator performs string concatenation, which means it joins the strings by linking
them end-to-end. For example:
>>> first = 'throat'
>>> second = 'warbler'
>>> first + second
throatwarbler
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

syntax error

A

“Syntax” refers to the structure of a program and the rules about that structure.
For example, parentheses have to come in matching pairs, so (1 + 2) is legal,
but 8) is a syntax error.

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

semantic error

A

The third type of error is “semantic”, which means related to meaning.
If there is a semantic error in your program, it will run without generating error
messages, but it will not do the right thing. It will do something else. Specifically, it
will do what you told it to do.

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

runtime error

A

The second type of error is a runtime error, so called because the error does
not appear until after the program has started running. These errors are also called
exceptions because they usually indicate that something exceptional (and bad) has
happened.

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

variable

A

name that refers to a value

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

assignment

A

statement that assigns to a variable

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

keyword

A

A reserved word that is used to parse a program; you cannot use keywords like
if, def, and while as variable names.

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

statement

A

A section of code that represents a command or action. So far, the statements
we have seen are assignments and print statements.

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

concatenate

A

: To join two operands end-to-end.

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

argument

A
expression in parenthesis of a function call
function(argument)
print('hello world')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

function

A

It is common to say that a function “takes” an argument and “returns” a result. The result
is also called the return value.

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

module

A

a file that contains a collection of related functions.

import some module

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

to call on a function in amodule

A

To access
one of the functions, you have to specify the name of the module and the name of the
function, separated by a dot (also known as a period). This format is called dot notation.
»> ratio = signal_power / noise_power
»> decibels = 10 * math.log10(ratio)
»> radians = 0.7
»> height = math.sin(radians)
The first example uses math.log10 to compute a signal-to-noise ratio in decibels (assuming
that signal_power and noise_power are defined). The math module also provides log,
which computes logarithms base e.

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

function definition

A
specifies the name of a new function and the
sequence of statements that run when the function is called.
Here is an example:
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print("I sleep all night and I work all day.")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

paramaters

A

parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method’s parameters. Parameter is variable in the declaration of function.
equivalent to argument

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

local variables

A

When you create a variable inside a function, it is local, which means that it only exists
inside the function.

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

function call

A

statement that runs the functions

39
Q

void function

A

always returns ‘none’

40
Q

dot notation

A
The syntax for calling a function in another module by specifying the module
name followed by a dot (period) and the function name.
41
Q

docstring

A

string at the beginning of a function that explains the interface (“doc” is
short for “documentation”)

””” “””

42
Q

‘for’ statement

A

The syntax of a for statement is similar to a function definition. It has a header that ends
with a colon and an indented body. The body can contain any number of statements.
A for statement is also called a loop because the flow of execution runs through the body
and then loops back to the top. In this case, it runs the body four times.

43
Q

encapsulation ** need help

A

An objects variables should not always be directly accessible.

To prevent accidental change, an objects variables can sometimes only be changed with an objects methods. Those type of variables are private variables.

The methods can ensure the correct values are set. If an incorrect value is set, the method can return an error.

44
Q

interface

A

what user deals with.

An interface is “clean” if it allows the
caller to do what they want without dealing with unnecessary details.

45
Q

refactoring

A

rearranging a program to improve interfaces and facilitate code re-use

46
Q

generalization

A

The process of replacing something unnecessarily specific (like a number)
with something appropriately general (like a variable or parameter).

47
Q

precondition

A

A requirement that should be satisfied by the caller before a function starts.

48
Q

floor division operator

A
//
divides two numbers and rounds down to an integer
49
Q

modulus operator

A

%

divides two numbers and returns remainder… often used with //

50
Q

1) x % 10

2) x % 100

A

1) finds right most digit of x

2) finds the two right most digits of x

51
Q

boolean expressions

A

expression that is either true or false

true and false are bool type objects

52
Q

relational operators

A
x == y #x is equal in value to y
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
53
Q

logical operators

A

and, or, not

54
Q

conditional statements

A

to say to do something only if a condition meets certain criteria

if ____:
do something

55
Q

if else statements

A

if x % 2 == 0:
print(‘x is even’)
else:
print(‘x is odd’)

56
Q

chained conditionals

A

if elif else
if x < y:
print(‘x is less than y’)
elif x > y:
print(‘x is greater than y’)
else:
print(‘x and y are equal’)
elif is an abbreviation of “else if”. Again, exactly one branch will run. There is no limit on
the number of elif statements. If there is an else clause, it has to be at the end, but there
doesn’t have to be one.
if choice == ‘a’:
draw_a()
elif choice == ‘b’:
draw_b()
elif choice == ‘c’:
draw_c()
Each condition is checked in order. If the first is false, the next is checked, and so on. If one
of them is true, the corresponding branch runs and the statement ends. Even if more than
one condition is true, only the first true branch runs.

57
Q

nested conditionals

A
One conditional can also be nested within another. We could have written the example in
the previous section like this:
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
58
Q

recursive function

A

one function calls another, or even calls itself
def countdown(n):
if n <= 0:
print(‘Blastoff!’)
else:
print(n)
countdown(n-1)
If n is 0 or negative, it outputs the word, “Blastoff!” Otherwise, it outputs n and then calls
a function named countdown—itself—passing n-1 as an argument.

59
Q

fibonacci

A

After factorial, the most common example of a recursively defined mathematical function
is fibonacci:
fib(0)=0
fib(1) =1
fibonacci(n) = fibonacci(n − 1) + fibonacci(n − 2)

60
Q

temporary variable

A

A variable used to store an intermediate value in a complex calculation.

61
Q

dead code

A

Part of a program that can never run, often because it appears after a return
statement

62
Q

reassignment

A
A new assignment makes an existing variable refer to a new value (and stop
referring to the old value).
>>> x = 5
>>> x
5
>>> x = 7
>>> x
7
63
Q

iteration

A

repetition of an identical or similar task

for loops/ while statements

64
Q

while statement

A

More formally, here is the flow of execution for a while statement:

  1. Determine whether the condition is true or false.
  2. If false, exit the while statement and continue execution at the next statement.
  3. If the condition is true, run the body and then go back to step 1.
def countdown(n):
while n > 0:
print(n)
n = n - 1
print('Blastoff!')
65
Q

index

A
expression in brackets. indicates which character in the sequence you want.
>>> fruit = 'banana'
>>> letter = fruit[1]
>>> letter
'a'
66
Q

len

A

> > > fruit = ‘banana’
len(fruit)
6

67
Q

slice

A
A segment of a string is called a slice. Selecting a slice is similar to selecting a character:
>>> s = 'Monty Python'
>>> s[0:5]
'Monty'
>>> s[6:12]
'Python'
The slice operator also works on lists:
92 Chapter 10. Lists
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']
68
Q

immutable

A

cannot be changed after its created

69
Q

mutable

A

a mutable object can be changed after it is created

70
Q

immutable strings

A

The reason for the error is that strings are immutable, which means you can’t change an
existing string. The best you can do is create a new string that is a variation on the original:
»> greeting = ‘Hello, world!’
»> new_greeting = ‘J’ + greeting[1:]
»> new_greeting
‘Jello, world!’
This example concatenates a new first letter onto a slice of greeting. It has no effect on the
original string

71
Q

counting

A

word = ‘banana’
count = 0
for letter in word:
if letter == ‘a’:
count = count + 1
print(count)
This program demonstrates another pattern of computation called a counter. The variable
count is initialized to 0 and then incremented each time an a is found. When the loop exits,
count contains the result—the total number of a’s.

72
Q

‘in’ operator

A
The word in is a boolean operator that takes two strings and returns True if the first appears
as a substring in the second:
>>> 'a' in 'banana'
True
>>> 'seed' in 'banana'
False
73
Q

list

A

a list is a sequence of values. In a string, the values are characters; in a list,
they can be any type. The values in a list are called elements or sometimes items.
The elements
of a list don’t have to be the same type. The following list contains a string, a float, an
integer, and (lo!) another list:
[‘spam’, 2.0, 5, [10, 20]]

74
Q

nested list

A

list within another list

75
Q

mutable lists

A

Unlike strings, lists are mutable. When the bracket operator appears on the left side of an
assignment, it identifies the element of the list that will be assigned.
»> numbers = [42, 123]
»> numbers[1] = 5
»> numbers
[42, 5]
The one-eth element of numbers, which used to be 123, is now 5.

76
Q

list operations

A
The + operator concatenates lists:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> c
[1, 2, 3, 4, 5, 6]
The * operator repeats a list a given number of times:
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
The first example repeats [0] four times. The second example repeats the list [1, 2, 3]
three times.
77
Q

list.append

A
append adds a new element
to the end of a list:
>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> t
['a', 'b', 'c', 'd']
78
Q

list.extend

A
extend takes a list as an argument and appends all of the elements:
>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
['a', 'b', 'c', 'd', 'e']
This example leaves t2 unmodified.
79
Q

list.sort

A

sort arranges the elements of the list from low to high:
»> t = [‘d’, ‘c’, ‘e’, ‘b’, ‘a’]
»> t.sort()
»> t
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Most list methods are void; they modify the list and return None.

80
Q

add up everything in a list

A
To add up all the numbers in a list, you can use a loop like this:
def add_all(t):
total = 0
for x in t:
total += x
return total
81
Q

deleting elements from lists

A
you can use pop:
>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> t
['a', 'c']
>>> x
'b'
pop modifies the list and returns the element that was removed. If you don’t provide an
index, it deletes and returns the last element.
If you don’t need the removed value, you can use the del operator:
>>> t = ['a', 'b', 'c']
>>> del t[1]
>>> t
['a', 'c']
If you know the element you want to remove (but not the index), you can use remove:
>>> t = ['a', 'b', 'c']
>>> t.remove('b')
>>> t
['a', 'c']
The return value from remove is None.
To remove more than one element, you can use del with a slice index:
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> t
['a', 'f']
As usual, the slice selects all the elements up to but not including the second index.
82
Q

convert string to list of characters

A

> > > s = ‘spam’
t = list(s)
t
[’s’, ‘p’, ‘a’, ‘m’]

83
Q

Aliasing

A
If a refers to an object and you assign b = a, then both variables refer to the same object:
>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True

An object with more than one reference has more than one name, so we say that the object is aliased.
if the aliased object is mutable, changes named to one will affect the other

84
Q

editing strings vs lists

A

Most list methods modify the argument and return None. This is the opposite of the
string methods, which return a new string and leave the original alone.

85
Q

equivalent vs identical

A

equivalent: Having the same value.
identical: Being the same object (which implies equivalence).

86
Q

reference

A

association between a variable and its value

87
Q

dictionary

A

A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type. A dictionary contains a collection of indices, which are called keys, and a collection of values. Each key is associated with a single value. The association of a key and a value is called a key-value pair or sometimes an item. in mathematical language, a dictionary represents a mapping from keys to values, so you can also say that each key “maps to” a value. As an example, we’ll build a dictionary that
maps from English to Spanish words, so the keys and the values are all strings

88
Q

{ }

A

> > > eng2sp = dict()
eng2sp
{}
eng2sp = {‘one’: ‘uno’, ‘two’: ‘dos’, ‘three’: ‘tres’}
The squiggly-brackets, {}, represent an empty dictionary. To add items to the dictionary, you can use square brackets:
eng2sp[‘one’] = ‘uno’
This line creates an item that maps from the key ‘one’ to the value ‘uno’. If we print the
dictionary again, we see a key-value pair with a colon between the key and value:
eng2sp
{‘one’: ‘uno’}

89
Q

dictionary mapping

A

The order of the key-value pairs might not be the same. If you type the same example
on your computer, you might get a different result. In general, the order of items in a
dictionary is unpredictable.
But that’s not a problem because the elements of a dictionary are never indexed with integer
indices. Instead, you use the keys to look up the corresponding values:
»> eng2sp[‘two’]
‘dos’
The key ‘two’ always maps to the value ‘dos’ so the order of the items doesn’t matter

90
Q

how to lookup a value in a dictionary

A

Given a dictionary d and a key k, it is easy to find the corresponding value v = d[k]

91
Q

tuple

A

A tuple is a sequence of values. The values can be any type, and they are indexed by
integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable.
Syntactically, a tuple is a comma-separated list of values:
»> t = ‘a’, ‘b’, ‘c’, ‘d’, ‘e’
To create a tuple with a single element, you have to include a final comma:
»> t1 = ‘a’,
»> type(t1)

92
Q

tuples=immutable

A

But if you try to modify one of the elements of the tuple, you get an error:
»> t[0] = ‘A’
TypeError: object doesn’t support item assignment
Because tuples are immutable, you can’t modify the elements. But you can replace one
tuple with another:
»> t = (‘A’,) + t[1:]
»> t
(‘A’, ‘b’, ‘c’, ‘d’, ‘e’)
This statement makes a new tuple and then makes t refer to it.

93
Q

list comprehension

A

def capitalize_all(t):
res = []
for s in t:
res.append(s.capitalize())
return res
We can write this more concisely using a list comprehension:
def capitalize_all(t):
return [s.capitalize() for s in t]
The bracket operators indicate that we are constructing a new list. The expression inside
the brackets specifies the elements of the list, and the for clause indicates what sequence
we are traversing.

94
Q

Generator expression

A

Generator expressions are similar to list comprehensions, but with parentheses instead of
square brackets:
»> g = (x**2 for x in range(5))
»> g
at 0x7f4c45a786c0>
The result is a generator object that knows how to iterate through a sequence of values. But
unlike a list comprehension, it does not compute the values all at once; it waits to be asked.
The built-in function next gets the next value from the generator:
»> next(g)
0
»> next(g)
1