6.00.1x & 6.00.2x Flashcards
imperative vs. declarative knowledge
declarative - statement of facts
imperative - recipe or “how-to” computation
What is the difference between a program and a algorithm?
An algorithm is a conceptual idea, a program is a concrete instantiation (representation?) of an algorithm.
A computational mode of thinking means that everything can be viewed as a math problem involving numbers and formulas.
True or False ?
True
Computer Science is the study how to build efficient machines that run programs.
True or False ?
False
What does it mean when we say that “the computer walks through the sequence executing some computation”?
The computer executes the instructions mostly in a linear sequence, except sometimes it jumps to a different place in the sequence.
What are primitive operations and what are some examples of primitive operations ?
Where are they in a computional machine?
A low-level object or operation from which higher-level, more complex objects and operations can be constructed.
Examples: addition, subtraction, examine true or false statements
In the ALU - arithmetic logic unit
What “Determines whether a string is legal”?
syntax
What “determines whether a string as meaning”?
semantic syntax
What “assigns a meaning to a legal sentence”?
semantics
What is the output after each expression ?
Remember this is one session - each expression is calculated directly after the previous problem.

- 5.0 and float
- 4.0 and float
- none type and error
What sort of expression is this ?
and what value is returned ?

Bool
True
print(ss[-4:-1])
How do you read slicing strings with negative index numbers ?
Same as positive
Read from left to right
remember starting value is inclusive
ending value is exclusive
What is the significance of the stride in slicing ?
it allows you to choose the interval at which python takes parts when slicing
How do you print in reverse order using slicing ?
Double colon to signify that this applies to the whole string
and then have -1 as the stride value.
What is the return value of this piece of code ?
and why?

Hot
because the computer reads the code in a linar fashion and the value meets the if statement to ‘print’ HOT.
What does += mean?
add whatever number to the varaible that is attached to it
mysum += i
What is a recipe?
- Sequence of simple steps
- Flow of control process that specifies when each step is executed
- A means of determining when to stop
How many primitives do you need to do any calculation
6
What are expressions ?
complex but legal combinations of primiives in a programming language
What could go wrong when writing a program ?
Syntactic errors
Static syntactic errors
No semantic error but different meaning to what the programmer intended
The difference between static semantics and semantics?
A statement in python may have meaning (it is statically semantically correct), but it may not do what you intended (it is not semantically correct).
List all the operations on ints and floats

How do you bind a value to a variable ?
Using the equals sign
e.g. pi = 3.14159
What are the comparison operators for Ints and Floats ?

Why is an indentation important?
Each indented set of expressions denotes a block of instructions.
Indentations also provide a visual structure that reflects the semantic structure of the program.
What is a Branching program ?
The simplest branching statement is a conditional
A test ( an expression that evaluates to true or false )
A block of code to execute if the test is true
An optional block of code to execute if the test is false

What is a nested conditional?
That inside a block, you have another conditional

What does ‘control flow - branching’ do?
evaluates expressions in blocks if <condition> is <strong>True</strong></condition>

For logical operator OR, what do you need to have a True output?
You need one True part of the statement
True or False
Output ?
True
BINDINGS: What would be the final answer ?

First as you go through the code you see that y is being assigned the value of ‘x’, which is 1.
So the new value of y is 1.
Now assigning the value of y to x, we know that y is 1 so the final value for x is 1
STRINGS: How do you concatenate two strings with a space?
you have to add (with plus signs) to double quotation marks
STRINGS: How do you find the length of string ?
What about spaces?
len( )
includes spaces
STRINGS: If you index ‘eric’ with [1:3]
what is the output ?
The first element which is r
and the second element i
it doesn’t include the third element as you might think
In ranges like these when indexing you include the starting index element but the finishing index value is not included
Final output: ‘ri’
STRINGS: what does the -1 mean for this string slicing?

a negative number means you count from the end of the array instead of the beginning.
STRINGS: What result would this slicing have?

Every element except for the last one.
STRINGS: What about this entail?

it would reverse the order of the string
STRINGS: What would this return?

Nothing because it can’t step back any places when it is confined between 0 and 9.
You cannot start at 0 and count backwards to 9…
STRINGS: How do you convert an integer into a string?
str(x)
where x is a variable holding the value
INPUT/OUTPUT: Why were the quotation marks also printed?
How do end up with an int for the output value?

Because input simply expects everything to be a string.
Whatever I type it is going to read it in as a string.
Even if you inputted a number it is going to interpret it as a string of the characters of the number.
So you need to turn it into an int before you actually use it.

INPUT/OUPUT: What is the ouput? And Why ?

The output is Hot
Code is ran linearly, from top to bottom, and it sees the “Hot” if statement first, and will never get to the “really Hot” elif statement because it already exited the loop at the first if.
IDE: What is an IDE ?
Integrated development environment
What do for and while loops do?
they can create an infinitely long looping of an expression(s) until it is stopped.
Difference between for and while ?
for has a function integrated into it that allows you to define a range
if you find this expression ..
mysum += i
i being a variable
what should think of ?
that some value is being added to mysum - mysum accumulates thes sums…
Summarize for and while loops main features.

When would you use a for loop?
And similarly, when would you use a for loop?
Use a for loop when you know what you are going to do as a computation
use a while loop when there is a condition that you can’t predict that would allow you to break out of the loop.
EX vara varb: How could you have made the code shorter ?

instead of using ‘if’ clauses throughout you could have used ‘elif’ clauses after the first ‘if’ statement and that will have stopped you from having to test for ‘integers’ - the elif statement is only read if the first condition isn’t met. So if the condition is true it will have stopped there.
Inside if statements that are inside ‘for’ and ‘while’ loops, if there is a break, what happens?
it stops the whole loop ( for or while )
What do you need to remember about division in python?!

It always returns a float!

What’s the answer to this question?
Why is it the way it is?

This is the answer as the code clearly states that after the count increase by 1 the loop should be broken…That means that no more iterations of the loop are going to take place, so the only printed value will be for the first letter ‘S’,

Are space taken into account for a string in a for loop function!
yes !
spaces are also considered characters in python
GUESS & CHECK: What is the output of an input function?
a string
If you can remember, how did this work?

It worked because index was reset every time, due to the index = 0 variable. You can do this when the variable definition is inside the loop.
What sort of input does the input function expect?
It expects a string.
So if you write something with double quotes attached, it will print the double quotes.
How do you convert the input function from string to int?
write int() around the input function.
What does the range option entail in a for loop?
Specifies what value you want the for loop to go to.
how do you define a stop and step when using range
range(start, stop, step)
what is important to remember about the stop value when using range?
the stop is actually equal to stop - 1
What does this output ?

0.0, 1.0, 2.0, 3.0, 4.0
When you divide there the output is a float…
What did you learn about the alphabet in python ?
that python recognizes alphabetical order, so you can simply test individual letters to see if one is higher up on the alphabet.
What does an immutable type mean ?
You can’t change them.
They can’t be modified.
How could you change a string?
You can’t change the string individually, you have to redefine the variable it is in.
What does int(4.7) become in python?
4
Python simply truncates down.
When slicing, is the final number inclusive or not?
Not inclusive, so if the range is 1:4, 4 is not included.
How can you skip characters while slicing?
you add a third element to the slicing,
example
1:6:2
What type does ‘input’ always use?
string
you need to enclose by int() if you want an integer.
What is the order of growth of this function?
What did you learn?

The order of growth is O(log(b)).
Why? Well if you put in some testing values for b, like 16, you’ll notice that it takes 4 steps to get to b = 1, which can be represented by log2 (16). The last step from 1 to 0 can be ignored because as b gets larger so does that step become more negligible. Similarly, if b is odd, there is one extra step that becomes negligible for large values of b.
The best way to approach these problems is to write down the general step equation for the whole function and then identify the dominant factor from there.
Why is the big O of this function log(b)?

By converting the number into a string it is iterating over the characters of the string and not through the length of the number. For example, 110 is broken up as ‘1’, ‘1’ and ‘0’ as a string while as a number the for loop would go through every value until 110. By converting it into a string the function essentially divided by 10 on each iteration.
So the number of iterations through the loop can be seen as log(b).
You should remember this next time you see an integer being converted into a string, even though it probably isn’t the most effective method.
If a recursive function calls the function twice in the return, what sort of complexity is it?
exponential