Class 17, Friday, February 22
Knowing what you know
Though the past is past – It is still an augury – Of future tidings
Look both ways
Agenda
- Chronicle what we know or should know
Peer mentoring
- 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
Examples
- TBD.py – to be determined during class. The artifacts link has the corresponding discussion from last term.
Test logistics
- 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.
Chrestomathics
- 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
for
repeats 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
type()
andid()
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
int
,float
, andbool
. Primitive type values have no behaviors.
- The values (objects) of types such as
str
andlist
do have behaviors. Behaviors are implemented as methods (message functions). A method function invocation tells its object to carry out a behavior. For example, ifs
is a string, thens.lower()
returns a new copy ofs
where are letters ins
are in lower case form in the copy.
- Literal values are composed of explicit strings, numbers, and logical values like
3
,0.1415
,True
,[
2,
'abc'
,5.85
]
.
- Operators like
+
,/
,//
,%
, and**
evaluate their operands and return the value of the corresponding mathematical operation save Python limitations on decimal representations.
str
- Once created
str
values 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.
- Operators
[ i ]
and[ i : j ]
retrieve substrings and+
to concatenate.
- Built-in function
len()
to find how many characters there are
- Methods like
find()
andupper()
return values.
range
range( i , j )
is the sequence of integer values fromi
toj-1
.
list
- Literal values like
[]
and[ 1, 'two', [ 3, 3.0 ] ]
.
- Operators
[ i ]
and[ i : j ]
to retrieve or modify elements or sublists;+
to concatenate; andin
to check if a value is within the list can be a range or a list.
- Functions like
len()
,max()
, andsum()
to analyze the contents of a list.
- Method functions like
index()
return values.
- Method functions like
append()
andsort()
that modify the list in place (and returnNone
).
for
loops
- 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 :
statement1
statement2
...
statementn
In the above, iterator variable
item
takes on each of the values ofsequence
in turn. For each of those values,statement1
,statement2
,...
, andstatementn
are repeated.
- A
for
loop inside anotherfor
loop is called a nested loop.
for
loops 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
0
and 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 length0
and 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 usingappend()
.
- If the result is to be a product, then the accumulator is initialized before the loop with
1
and 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
True
and updated within the loop using Boolean operatorand
.
- 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
False
and updated within the loop using Boolean operatoror
.
Producing values
- 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 valuesa
andb
.
- A method is named piece of code that manipulates the object to which it is targeted (dotted); e.g., in invocation
s.capitalize()
methodcapitalize()
examiness
to produce and return a copy of strings
with 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 forprint()
anda.append()
).
Library
- A library is a modue of related functions and values that must be imported to gain access to them.
- Standard library
math
provided access to a wide variety of mathematical computations and values; e.g.,math.sqrt()
,math.sin()
, andmath.pi
.
- Standard library
random
provides ability to generate random values. The most important functions for us are
random.randrange( a, b )
returns a random integer froma
tob-1
.
random.choice( s )
returns a random value from sequences
.
random.shuffle( s )
returns a new sequence whose values are a scrambling of the values of sequences
.
random.seed( s )
usess
to set up the initial configuration of the Python random number generator.
- Local library
url
provides web access
url.get_contents( link )
returns the contents of the web page at urllink
.
url.get_dataset( link )
returns the contents of the web page at urllink
as as a dataset sheet whose cell values are all strings.
url.get_and_parse_dataset( link )
returns the contents of the web page at urllink
as as a dataset sheet whose cell values are either string,int
,float
, orbool
as appropriate.
Web processing
- Suggest import local module url.
- Use
url.get_text()
to get the contents of a web file.
- Use
url.get_dataset()
to get the contents of a web dataset.
Errors
- 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.
Gotchas
- 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
int()
orfloat()
or to a Boolean value usingbool()
.
- Output expectations
- Improper accumulation
- Combining a string and a number using the
+
operator
- 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
To do
- 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.