Gotchas
- Input must be gotten in the specified order or results will be different.
- Never hardcode values as an answer; you will get the problem wrong if he tests different values.
- Function
int()
does not work on a list or a string with multiple values.
- Python indexing begins at 0.
- The Python remainder operator is
%
.
- The operator
=
performs assignment — the value of the left operand becomes the value of the right operand (variable).
- The operator
==
tests whether its operands are equal to each other.
- The operator
[]
supports subscripting into a sequence (e.g., list or string).
- An accumulator must be initialized before the loop that does the accumulating.
- The operator
+
is overloaded. When its operands are numeric, it performs addition. When its operands are strings, it produces a new concatenated string. You cannot used +
where one operand is a string and the other is a number.
- Lists can grow and shrink.
- Strings are immutable; i.e., they cannot be changed.
- Function invocation
len( s )
returns the number of elements in sequence s
(think string or list).
- For string
s
, s.lower()
returns a new string where all alphabetic characters are lowercase.
- For string
s
, s.capitalize()
returns a new string where the first character is in s
is capitalized and the remaining are lowercase.
- Style rule: the values in a
print()
statement should be variables or literals.
url.get_text( link )
hands back the contents of webpage named by link
.
url.get_lines( link )
hands back the contents of webpage named by link
as a list of lines (each line is a string).
url.get_dataset( link )
hands back the contents of webpage named by link
as a dataset; that is, a table with individual rows of data cells.
- When you want to specify a range you must use the keyword
range
.
- The
range( a, b )
is the sequence of values a
, a + 1
, ..., b - 1
.
- The first index into a list is 0.
- Accumulators for summing should be initialized to
0
.
- Accumulators for list building should be initialized to
[]
.
- String functions work on strings not lists and vice-versa. For example,
x.split()
does not work if x
is a list.
- When you are using a function, you must have parentheses.
- Built-in function
sorted( a )
: returns a new list that is a sorted version of a
. List a
is unaltered.
s.split()
handbacks a list of words that make up string s
.
- Never write
x = x.append( v )
. It destroys x
.