Python - How to think like a computer scientist Flashcards
What is a “state snapshot”?
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
Compare the error types between JavaScript and Python
What does the line “import turtle” do?
load turtle module and associated types/objects (e.g. Turtle(), screen()
define: instances. how do we create instances
copy of an object, independent of each other and has their own properties and methods
alex = turtle.Turtle()
tess = turtle.Turtle()
give an example of a for loop and identify the different parts
example:
for i in [item1, item 2, item3]: print(i)
LOOP VARIABLE: i
LIST: [item1, item 2, item3]
LOOP BODY: print(i)
define control flow
python keeping track of the code statement it’s executing, like a pointer finger
what is a sequential control flow?
execution of code from top to bottom
explain in flow chart how the for loop is a sequential control flow
what’s another name for control flow
flow of execution
how is the for
loop a compound statement?
indentation
what’s the general structure to print a range of numbers in an iteration?
range(start, stop, step)
why do we need to wrap range in a list?
list(range()) - apparently range sometimes doesn’t print all the values
what values would these statements give?
print(list(range(4)))
print(list(range(1, 5)))
print(list(range(1, 5, 2)))
[0, 1, 2, 3]
[1, 2, 3, 4]
[1, 3]
identify method that allows us to turn a turtle negative angles or distances
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)
identify method that allows turtle to pickup or put down pen
Turtle.up() / Turtle.penup()
Turtle.down() / Turtle.pendown()
identify method that influences turtle shape, some shapes the turtle can take on
Turtle.shape(“”)
shapes = classic (pointer - think this is also the default), turtle, square, triangle, etc
identify method that influences turtle’s animation speed
Turtle.speed()
identify method to leave an imprint of the turtle shape on canvas
Turtle.stamp()
what is programming?
“breaking up large task into tinier tasks that are generally made up of basic instructions”
programs vs algorithms?
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
compare interpreters and compilers
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
what does the compiler convert source code into (2 different words)
object code
executable
what are a few common instructions (5) that appears in almost every programming language?
input
output
math & logic operations
conditions
repetition/iterations
what are the 3 kinds of errors that can occur from human writing a program and give examples
(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
natural vs formal language
NATURAL FORMAL
evolved over time designed for purpose
strict syntax
redundant precise
not literal (idioms etc) literal
ambiguous unambiguous
what 2 types of syntax rules are there for languages? elaborate
tokens - language elements (or building blocks)
structure - how the tokens are arranged
what are processes involved in understanding a language?
parsing - comprehending language structure
semantics - getting meaning from structure
in python, everything is an _____
object, even a value that is a number or word is an object
what other names are values known as?
classes/data types
name 3 types of values
strings
floats - fractional numbers
integers - whole numbers
how do we denote strings?
marked by single, double or triple quotes
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”
which function helps us identify what type an object is?
type([value here])
function
how to convert types into another?
int()
float()
str()
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?
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
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]
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
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)
)
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
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
list order of precedence ** - + () * /
()
**
* /
- +
which operators have the same precedence? what’s the formal name for the same precedence? what does this mean?
left-associative
- /
- +
how is the exponentiation operator evaluated in this case 2 ** 3 ** 2
?
right most exponent is evaluated first
What is the value of the following expression: 16 - 2 * 5 // 3 + 1
?
14
16 - (2 * 5 // 3) + 1
remember // is integer division
what is the function to get user input?
input( “[default prompt string here]” )
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
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
What are the 2 most important clues you need to help you debug?
(1) error messages
(2) print statements - similar to “console.log”?
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?
Compare the error types between JavaScript and Python