Python - How to think like a computer scientist Flashcards

You may prefer our related Brainscape-certified 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
Q

natural vs formal language

A

NATURAL FORMAL
evolved over time designed for purpose
strict syntax
redundant precise
not literal (idioms etc) literal
ambiguous unambiguous

26
Q

what 2 types of syntax rules are there for languages? elaborate

A

tokens - language elements (or building blocks)
structure - how the tokens are arranged

27
Q

what are processes involved in understanding a language?

A

parsing - comprehending language structure
semantics - getting meaning from structure

28
Q

in python, everything is an _____

A

object, even a value that is a number or word is an object

29
Q

what other names are values known as?

A

classes/data types

30
Q

name 3 types of values

A

strings
floats - fractional numbers
integers - whole numbers

31
Q

how do we denote strings?

A

marked by single, double or triple quotes

32
Q

why can’t you put a comma to write 42000 like 42,000?

A

, is used to separate values so we would get the result “42 0”

33
Q

which function helps us identify what type an object is?

A

type([value here]) function

34
Q

how to convert types into another?

A

int()
float()
str()

35
Q

with int(), what’s something to note about this function? what happens if we’re trying to convert a non-numeric value?

A

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
Q

how to remind ourselves that assignment tokens does not mean equality?

A

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
Q

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

A

[attach diagram here]

38
Q

what are 5 naming conventions to keep in mind when making legal variable names in Python?

A

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
Q

what is the primary difference between a statement and expression and give examples

A

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
Q

what’s a way to tell if we’ve written a statement in the shell?

A

submitting a code line is followed by another prompt symbol rather than a value

note to self - insert pic later

41
Q

what is the operator for integer division? how is it different from division operator?

A

//

this returns only the integer after the calculation, kinda the opposite of what % does which is only return the remainder after division

42
Q

list order of precedence ** - + () * /

A

()
**
* /
- +

43
Q

which operators have the same precedence? what’s the formal name for the same precedence? what does this mean?

A

left-associative

  • /
  • +
44
Q

how is the exponentiation operator evaluated in this case 2 ** 3 ** 2?

A

right most exponent is evaluated first

45
Q

What is the value of the following expression: 16 - 2 * 5 // 3 + 1?

A

14

16 - (2 * 5 // 3) + 1
remember // is integer division

46
Q

what is the function to get user input?

A

input( “[default prompt string here]” )

47
Q

What is the key to being a successful programmer?

A

the ability to debug
keep up a momentum where successfully solving one problem motivates you to keep going

48
Q

What are the three key things to remember to avoid having to debug in the first place?

A

(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
Q

What are the 2 most important clues you need to help you debug?

A

(1) error messages
(2) print statements - similar to “console.log”?

50
Q

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

A

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
Q

Compare the error types between JavaScript and Python

A