Python - How to think like a computer scientist Flashcards

1
Q

What is a “state snapshot”?

A

A more popular word (“most often referred to”) for reference diagram that maps variable names and values they refer to at a particular point in time

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

Compare the error types between JavaScript and Python

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

What does the line “import turtle” do?

A

load turtle module and associated types/objects (e.g. Turtle(), screen()

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

define: instances. how do we create instances

A

copy of an object, independent of each other and has their own properties and methods

alex = turtle.Turtle()
tess = turtle.Turtle()

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

give an example of a for loop and identify the different parts

A

example:

 for i in [item1, item 2, item3]: 
      print(i)

LOOP VARIABLE: i
LIST: [item1, item 2, item3]
LOOP BODY: print(i)

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

define control flow

A

python keeping track of the code statement it’s executing, like a pointer finger

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

what is a sequential control flow?

A

execution of code from top to bottom

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

explain in flow chart how the for loop is a sequential control flow

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

what’s another name for control flow

A

flow of execution

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

how is the for loop a compound statement?

A

indentation

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

what’s the general structure to print a range of numbers in an iteration?

A

range(start, stop, step)

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

why do we need to wrap range in a list?

A

list(range()) - apparently range sometimes doesn’t print all the values

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

what values would these statements give?

print(list(range(4)))
print(list(range(1, 5)))
print(list(range(1, 5, 2)))

A

[0, 1, 2, 3]
[1, 2, 3, 4]
[1, 3]

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

identify method that allows us to turn a turtle negative angles or distances

A

turning a number negative produces the opposite of a specified direction

e.g. Turtle.forward(-100) moves it backward by 100
but you can also write this as Turtle.backward(100)

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

identify method that allows turtle to pickup or put down pen

A

Turtle.up() / Turtle.penup()
Turtle.down() / Turtle.pendown()

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

identify method that influences turtle shape, some shapes the turtle can take on

A

Turtle.shape(“”)

shapes = classic (pointer - think this is also the default), turtle, square, triangle, etc

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

identify method that influences turtle’s animation speed

A

Turtle.speed()

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

identify method to leave an imprint of the turtle shape on canvas

A

Turtle.stamp()

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

what is programming?

A

“breaking up large task into tinier tasks that are generally made up of basic instructions”

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

programs vs algorithms?

A

algorithm is a set of instructions but doesn’t necessarily need to be written in a language for the computer to interpret

a program is a set of instructions that needs to be more precise so the computer can follow

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

compare interpreters and compilers

A

both converts high-level language (written to be readable by humans) to low-level language (machine/assembly language)

interpreter: alternates between converting and executing

compiler: converts entire source code before executing

22
Q

what does the compiler convert source code into (2 different words)

A

object code
executable

23
Q

what are a few common instructions (5) that appears in almost every programming language?

A

input
output
math & logic operations
conditions
repetition/iterations

24
Q

what are the 3 kinds of errors that can occur from human writing a program and give examples

A

(1) SYNTACTICAL ERROR
grammatical errors - caught by the machine before executing e.g. mispelling keywords, typos, forgetting a :

(2) RUNTIME ERROR
can only be caught after executing
e.g. trying to complete a computation

(3) SEMANTIC ERROR
program runs but. does something unexpected
trickier to solve, would need to retrace steps one by one

25
natural vs formal language
NATURAL FORMAL evolved over time designed for purpose strict syntax redundant precise not literal (idioms etc) literal ambiguous unambiguous
26
what 2 types of syntax rules are there for languages? elaborate
tokens - language elements (or building blocks) structure - how the tokens are arranged
27
what are processes involved in understanding a language?
parsing - comprehending language structure semantics - getting meaning from structure
28
in python, everything is an _____
object, even a value that is a number or word is an object
29
what other names are values known as?
classes/data types
30
name 3 types of values
strings floats - fractional numbers integers - whole numbers
31
how do we denote strings?
marked by single, double or triple quotes
32
why can't you put a comma to write `42000` like `42,000`?
, is used to separate values so we would get the result "42 0"
33
which function helps us identify what type an object is?
`type([value here])` function
34
how to convert types into another?
int() float() str()
35
with int(), what's something to note about this function? what happens if we're trying to convert a non-numeric value?
finds the closest whole number - this is not the same as rounding up e.g. `int(53.785)` will produce `53` not `54` .. so essentially, it's rounding down?
36
how to remind ourselves that assignment tokens does not mean equality?
whenever we write it, we don't say to ourselves "a equals 2" but: - a "refers to object" 2 - "reference to object" - a "is assigned to" 2 - a "gets the value of" 2
37
how can we track and diagram variables to their values would like to revise this card when I see this tracked on more involved/complex problems
[attach diagram here]
38
what are 5 naming conventions to keep in mind when making legal variable names in Python?
good_variable_name variable$ (1) must begin with letter - conventional to use lowercase - or underscore, case sensitive (2) no spaces (3) the only special character that is safe is an underscore (i think). he says $ and + are not allowed but he doesn't explicitly say all other characters other than underscore are illegal (4) is not a reserved list of keywords Python already uses (5) meaningful for other human readers of the program
39
what is the primary difference between a statement and expression and give examples
STATEMENTS must be EXECUTED, they don't return a value FOR EXAMPLE: assignment with = while for if import ... e.g `y = 3.14` EXPRESSIONS must be EVALUATED, they can contain values, variables, operators, function calls (e.g. `print(y)`)
40
what's a way to tell if we've written a statement in the shell?
submitting a code line is followed by another prompt symbol rather than a value note to self - insert pic later
41
what is the operator for integer division? how is it different from division operator?
// this returns only the integer after the calculation, kinda the opposite of what % does which is only return the remainder after division
42
list order of precedence `** - + () * / `
() ** * / - +
43
which operators have the same precedence? what's the formal name for the same precedence? what does this mean?
left-associative * / - +
44
how is the exponentiation operator evaluated in this case `2 ** 3 ** 2`?
right most exponent is evaluated first
45
What is the value of the following expression: `16 - 2 * 5 // 3 + 1`?
14 16 - (2 * 5 // 3) + 1 remember // is integer division
46
what is the function to get user input?
input( "[default prompt string here]" )
47
What is the key to being a successful programmer?
the ability to debug keep up a momentum where successfully solving one problem motivates you to keep going
48
What are the three key things to remember to avoid having to debug in the first place?
(1) understand the problem (and plan) (2) get something small working first (3) keep adding small working bits until you get a complete program
49
What are the 2 most important clues you need to help you debug?
(1) error messages (2) print statements - similar to "console.log"?
50
Explain what these 4 common errors in Python you might get are and what debugging techniques might you use to solve them? ParseError NameError TypeError ValueError
ParseError - syntax errors NameError - not initialising a variable before using it TypeError - trying to combine different data types ValueError - passing unexpected data through function?
51
Compare the error types between JavaScript and Python