Class 17, Monday, February 22
Knowing what you know
Though the past is past – It is still an augury – Of future tidings
Look both ways
- Chronicle what we know or should know
- Some course personnel have volunteered to pilot an effort to provide peer mentoring support for CS 1112. If you are interested in participating, please fill out this application
- TBD.py – to be determined during class. The artifacts link has the corresponding discussion from last term.
- Is next class.
- In the normal classroom for the normal time (2–3:15 pm).
- You will have access to module info, Python info sheet, and some gotchas, but nothing else from our website or other websites. Access to those other elements of the web site will not be available until after everyone completes the test.
- The test will include some written and some coding parts, so bring both a pencil (or pen) and a laptop.
- You are not allowed to access past code (yours or ours) during the test.
- You are not be allowed to use PyCharm for the paper part of the test.
- Class personnel cannot help you debug your answers.
- An algorithm is doable structured communication without ambiguity.
- A program is an algorithm written in a language that can be carried out on a computer.
Development and translation
- Python is an interpretive programming language designed for automating tasks.
- There are compilers that can be downloaded for most computers to execute Python programs.
- There are IDEs like PyCharm for computers to develop Python code.
How Python works
- Executes one line at a time, in order from top to bottom (except
forrepeats some lines).
- Evaluates literals, expressions, variables, function, and method invocations according to operator precedence.
Values and types
- All values have a type and an identity. Built-in functions
id()return respectively the type of a value and its identity. The identity of a variable is a reference to where its value is stored in computer memory.
- The simple value (primitive) types are
bool. Primitive type values have no behaviors.
- The values (objects) of types such as
listdo have behaviors. Behaviors are implemented as methods (message functions). A method function invocation tells its object to carry out a behavior. For example, if
sis a string, then
s.lower()returns a new copy of
swhere are letters in
sare in lower case form in the copy.
- Literal values are composed of explicit strings, numbers, and logical values like
- Operators like
**evaluate their operands and return the value of the corresponding mathematical operation save Python limitations on decimal representations.
- Once created
strvalues are immutable character sequences (there is no function, method, or operator that can modify an existing string).
- Escape sequences, like
'\n'represent the new-line character.
[ i ]and
[ i : j ]retrieve substrings and
- Built-in function
len()to find how many characters there are
- Methods like
range( i , j )is the sequence of integer values from
- Literal values like
[ 1, 'two', [ 3, 3.0 ] ].
[ i ]and
[ i : j ]to retrieve or modify elements or sublists;
+to concatenate; and
into check if a value is within the list can be a range or a list.
- Functions like
sum()to analyze the contents of a list.
- Method functions like
- Method functions like
sort()that modify the list in place (and return
- Repeat statements by iterating over a sequence of values. The sequence is a typically list or a
range, but can also be a string.
for item in sequence :
In the above, iterator variable
itemtakes on each of the values of
sequencein turn. For each of those values,
forloop inside another
forloop is called a nested loop.
forloops can be used to build up (accumulate) a result.
- If the result is to be a sum, then the accumulator is initialized before the loop to
0and increased within the loop using numeric operator
- If the result is to be a string, then the accumulator is initialized before the loop to
''and added to within the loop using string operator
+. Observe that
''is a string of length
0and different from the string
' 'which is a string of length 1 that consists of a single splace.
- If the result is a list, then the accumulator is initialized before the loop with
and added to within the loop using
- If the result is to be a product, then the accumulator is initialized before the loop with
1and increased within the loop using numeric operator
- If the result is to a check for sameness (i.e., all values have a property), then the accumulator is initialized before the loop with
Trueand updated within the loop using Boolean operator
- If the result is to a check for occurrence (i.e., a value(s) have the property), then the accumulator is initialized before the loop with
Falseand updated within the loop using Boolean operator
- Values are produced whenever Python evaluates an expression.
- Anytime an expression is encountered it is evaluated.
- Anything following an assignment operator is an expression.
- Any argument to a function or method is an expression.
- The operand(s) for an operator are expression(s).
- An expression can be composed from literals, variables, operators, and sub-expressions.
Functions and methods
- A built-in function invocation does not have an object to which its actions are targeted; e.g.,
max( a, b )returns the larger of values
- A method is named piece of code that manipulates the object to which it is targeted (dotted); e.g., in invocation
sto produce and return a copy of string
swith the first character capitalized and the other characters in lowercase.
- Whenever a function or method is invoked it, returns a value, even if that value is just
None(like it is for
- A library is a modue of related functions and values that must be imported to gain access to them.
- Standard library
mathprovided access to a wide variety of mathematical computations and values; e.g.,
- Standard library
randomprovides ability to generate random values. The most important functions for us are
random.randrange( a, b )returns a random integer from
random.choice( s )returns a random value from sequence
random.shuffle( s )returns a new sequence whose values are a scrambling of the values of sequence
random.seed( s )uses
sto set up the initial configuration of the Python random number generator.
- Local library
urlprovides web access
url.get_contents( link )returns the contents of the web page at url
url.get_dataset( link )returns the contents of the web page at url
linkas as a dataset sheet whose cell values are all strings.
url.get_and_parse_dataset( link )returns the contents of the web page at url
linkas as a dataset sheet whose cell values are either string,
- Suggest import local module url.
url.get_text()to get the contents of a web file.
url.get_dataset()to get the contents of a web dataset.
- Syntax errors are illegally formed Python instructions. This web page epistle might prove useful in dealing with syntax errors.
- Semantic errors are actions not doing what is intended.
- Ignoring the Bill Murray rule of life — baby steps to obtain your goals
- Forgetting the Law of Cole
- Not reading the complete description before starting to problem solve
- Ignoring the input order
- Not parsing (converting) the input into the needed values
- Not converting a string to a number using
float()or to a Boolean value using
- Output expectations
- Improper accumulation
- Combining a string and a number using the
- Using only the first or last value of a range or sequence to compute result
- Not checking whether your solutions match given test run expectations
- Not mindfully writing and reading your solution — you must understand what you are writing
- Make your computational journey clearer and easier to mindfully read
- Corollary: use print statements when developing code to document computational progress and comment out those statement before submitting
- Ignoring the Lamas rule of life — looking good makes a life complete
- Consistent use of whitespace around operators and blank lines make mindfulness easier to obtain.
- Functions use parentheses; lists use brackets. Remember this
- Functions must always have parentheses. Remember this
- Review class artifacts
- Review standard documentation for built-in functions.
- Review course documentation for module str.
- Review course documentation for module list.
- Review course documentation for local module url.
- Review standard documentation for module math.
- Review ccourse documentation for module random.
- Epistle on syntax errors and exceptions.
- Epistle on accomplishments.