






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Answer: CPU instructions represented in binary — sequences of 0's and 1's. The ONLY
language a CPU can execute directly.
Answer: CPU instructions written with mnemonics (e.g., add $r3, $r2, $r1). Easier to read,
but equivalent to machine language.
Answer: C, C++, Java, Python — much easier to use, but must be translated into machine
language to run.
Examples?
Answer: Translated ONCE, before running the code. Saves time. Examples: C, C++, Java
(partially).
Examples?
Answer: Translated WHILE the code executes, every run. Can adapt to the runtime
situation. Examples: Python, Basic, Java (partially).
Answer: Interpreted — a high-level (scripting) language that needs an interpreter to
execute.
Answer: A program that accepts Python commands and executes them.
Answer: Interactive: commands executed one at a time as they are typed into a shell.
Batch: many commands (a program) read from a file and executed in sequence.
Answer: The shell + text editor you get when installing Python from python.org (along
with the interpreter).
Answer: The Python shell is waiting for you to type a command (interactive mode).
Answer: 1) The expression is evaluated and its value is put into an object of the
appropriate type. 2) That object is assigned the name .
Answer: NameError: name '...' is not defined. A variable does not exist until it is assigned.
Answer: a-z, A-Z, the underscore _, and digits 0-9. BUT the name cannot START with a
digit.
Answer: Names can't start with a digit — SyntaxError: invalid syntax.
Answer: Underscore delimiter (new_temp) or camelCase capitalization (newTemp).
Answer: a, b = b, a
Answer: tmp = b
b = a
a = tmp
Answer: -
Answer: 3.
evaluated first, next, last?
Answer: 1) Algebraic operators first, 2) comparison operators next, 3) Boolean
operators (and, or, not) last.
Answer: ( ) > [ ] indexing > *
> unary +x, -x >
, /, //, % > +, - > in, not in > <, >, <=, >=, ==, != >
not > and > or
Answer: True — 6 == 6.0. An int and a float compare equal by VALUE.
Answer: x % n == 0 (remainder is zero)
Answer: False — 284 is even, but 284 % 3 is 2. (With 'or' instead, it's True.)
Answer: < > == != <= >= — they evaluate to True or False.
Answer: and, or, not — evaluated LAST in a mixed expression.
Answer: True (both sides True)
Answer: True — 'or' needs only one True side.
Answer: False
Answer: 1 and 2 — bools convert to ints (True = 1) in arithmetic.
Answer: A sequence of characters enclosed in quotes (single or double).
raise?
Answer: NO — strings are IMMUTABLE. Item assignment raises TypeError: 'str' object
does not support item assignment.
Answer: 'rock climbing' — + concatenates strings.
Answer: 'ababababab' — * repeats a string.
Answer: False — no letter o in climbing. On STRINGS, 'in' checks substrings.
Answer: True — 'bi' is a substring.
Answer: False — case matters! The string contains 'Ant', not 'ant'.
Answer: True — string comparison is alphabetical, and 'r' comes after 'c'.
Answer: 8
Answer: 0 — the second has index 1, the third has index 2, ...
Answer: Position from the end: the last item is -1, second to last is -2, etc.
Answer: 'A', 'e', 'e', 'A'
Answer: False
Answer: False — on a LIST, 'in' matches WHOLE items, not substrings. ('dog' in the list is
True.)
Answer: 3 — the nested list counts as ONE item.
lst[4][1]?
Answer: lst[4] is [4, 'five'] (the whole inner list); lst[4][1] is 'five'.
Answer: 1, 3, 6
Answer: Adds x to the END of lst. Modifies the list; RETURNS None.
Answer: RETURNS the number of times x occurs in lst. Does not modify the list.
Answer: RETURNS the index of the FIRST occurrence of x.
Answer: Removes AND RETURNS the last item — the only mutator that gives back a
value.
Answer: Removes the FIRST occurrence of the VALUE x (not index!). Returns None.
Raises ValueError if x isn't in the list.
Answer: Reverses the order of items IN PLACE. Returns None.
Answer: Sorts items in increasing order IN PLACE. Returns None.
Answer: None — sort() modifies the list but returns nothing. Same for append, remove,
reverse.
which RETURN a value?
Answer: count, index, and pop. The others (append, remove, reverse, sort) return None
but modify the list. pop BOTH modifies and returns.
Answer: A function is called on its own: len(lst), sum(lst). A method must be called ON
an object with a dot: lst.append(7) — append() can't be called independently.
lst.append(7)
lst.append(3)
What is lst and lst.count(3)?
Answer: [1, 2, 3, 7, 3] and count(3) is 2.
Answer: Executes a code block once for every item of a sequence:
for in :
Answer: A, p, p, l, e — one character per line (items of a string are its characters).
Answer: 0, 1, 2, 3, 4 — it stops BEFORE 5. range(n) gives n numbers starting at 0.
if age < 18: ...
What error and why?
Answer: TypeError — '17' < 18 compares a str with an int, which isn't supported. Fix: wrap
input() with int().
(name='Marie', age=17)
Answer: name + ', you will be ' + str(age + 1) + ' next year!' — str() is required; you can't
concatenate str + int.
Answer: if :
— the non-indented line ALWAYS runs.
Answer: if :
else:
Answer: Exactly one — the FIRST condition that is True (top-down); else runs if none are
True.
(explicitly or implicitly)?
Answer: Because only the first True branch runs. Test the most extreme condition FIRST
(t > 86 before t > 32), or make ranges explicit (86 >= t > 32).
Answer: if t > 32:
print('It is cool')
elif t > 86:
print('It is hot')
temperature(90) prints what? Why is it wrong? 'It is cool' — 90 > 32 is True so
the first branch fires; the elif is never checked. Fix: test t > 86 first.
Answer: Indentation determines which statements belong to the block. Indent
'Goodbye' and it only runs when the condition is True; unindent it and it always
runs.
Answer: SyntaxError
Answer: def (<0 or more variables>):
Answer: return sends a value back so it can be used in expressions. print just displays
text — the function then returns None.