Please send questions to
st10@humboldt.edu .
* a FEW miscellaneous notes from Lecture 2:
* [from HtDP, Section 1] "A computer's language of instruction and
information is called a PROGRAMMING LANGUAGE."
* syntax: the grammar/RULES of a programming language;
semantics: the MEANING of what one writes;
* a statement or a program is SYNTACTICALLY CORRECT if it follows
a programming language's syntax rules;
* a statement or a program is SEMANTICALLY CORRECT if it does what
we intend it to do -- if its meaning is what we intended.
* [from HtDP, Section 1] "Information expressed in a programming
language is called DATA."
* many kinds of data -- rangining from
ATOMIC data, logically-"indivisible" chunks, and
COMPOUND data, "made up of other pieces of smaller pieces of data",
like a series of numbers, a family tree, etc.
* Data "represents information, but the CONCRETE INTERPRETATION is
up to us"
* And the computer decides what KIND of data something is by HOW it is
written -- there is SYNTAX for that;
* 13 <- to Python, that is the integer number thirteen
* 13.0 <- to Python, that is the floating point number thirteen
with a fractional part of zero
* 1.3e1 <- to Python, 1.3 * 10 raised to the first power -- the
floating point number thirteen with a fractional part
of zero, also.
* [HtDP, Section 2] "Each class of data comes with a set of PRIMITIVE
OPERATIONS."
* it turns out integers and floating point numbers are stored
differently in a computer;
* in Python, we get +, -, *, and / for integers, and
+, -, *, and / for floating point numbers;
* [HtDP, Section 2] "Programmers COMPOSE primitive operations into
PROGRAMS."
* And: to get good programs, careful DESIGN is necessary;
* EXPRESSIONS - have a value, can be simple or not so simple (compound)
* a numeric literal, such as 13 or 1.3, is an expression, an
arithmetic expression;
* putting an arithmetic operator between two arithmetic expressions
results in another arithmetic expression;
1 <- an arithmetic expression
3 <- an arithmetic expression
1 + 3 <- an arithmetic expression
* putting parentheses around an arithmetic expression results
in another arithmetic expression:
(1 + 3) <- an arithmetic expression
* and, calls to arithmetic functions are arithmetic expressions,
too. Python has a round function, for example, that expects
one value and returns that value rounded appropriately;
and Python's math module, as another example, contains a square root
function named sqrt, that expects one value and then returns the
square root of that value; and so, if you IMPORT that module in
your python session:
>>> from math import *
... then a function name, an opening parenthesis, an arithmetic
expression, and a closing parenthesis is also an arithmetic
expression:
sqrt(2) <- an arithmetic expression
round( 2.3 / 7.0 ) <- an arithmetic expression
* and from these rules, one can build a vast array of arithmetic
expressions!
3 + (sqrt( 4*6 ) / 7)
2 * sqrt( sqrt( sqrt(3) ) )
etc.!
* AND we are not limited to the functions that Python provides --
programmers may also write their own, as many as they want,
and use them within other programs, too.
* NOTE: an important thing to know about computer languages:
>>> COMPUTERS HATE AMBIGUITY! <<<
* We said that +, *, /, - are different for integers for floating
point numbers -- how does Python know WHICH to use?
* ...it looks at the types of the operands!
* if both are INTEGERS --- it assumes an integer operation
* if both are FLOATING POINT --- it assume a floating point
operation
* if it is MIXED (one integer, one floating point) --- it adds
special code to convert the integer value to floating point,
and assumes a floating point operation.
* What happens if you have multiple operators, such as 3 + 4 * 2?
* like in algebra, there is OPERATOR PRECEDENCE;
* (* gets done before +, for example)
* usually goes from left to right for operators at the "same"
level;
* for a complete operator precedence chart for Python, see:
http://www.python.org/doc/2.4/ref/summary.html
(also linked from public course web page, near these notes)
* BUT: also like algebra, can use ( )'s to "force" a different
precedence (stuff in ( )'s gets done FIRST);
SO: when in DOUBT --- use ( )'s!
* remember that floating point numbers MAY NOT BE EXACT -- computers
can only store so many significant digits;
* what if you'd LIKE to write your OWN functions, then?
* you CAN type them directly within the python interpreter --
and then you can use them for the rest of that python session.
(you'd have to retype it to reuse it in another python session)
* you can create your OWN Python module, a plain text file whose
name ends in .py (and begins with a letter) in which you type
Python code;
IF you type function definitions within a file with such a name,
say that_file.py, then you can type:
>>> from that_file import *
...and now you can use all of those functions during the rest of
that python session.
(whenever you want to use your functions within a python session,
type the line above.)
* rules for Python IDENTIFIERS (names the programmer gets to choose --
Python module names, function names, function parameter names, and
more!):
* from http://www.python.org/doc/2.3.2/ref/identifiers.html,
part of the on-line Python manual:
* must start with a letter or underscore
* can be followed by zero or more letters, digits, or
underscore
* (and remember that Python is CASE SENSITIVE...)
* CIS 130 STYLE RULE: pick DESCRIPTIVE, meaningful identifiers...
* Python basic function syntax for a function that returns a value:
def funct_name ( param1, param2, ..., paramN):
indented_statement
indented_statement
...
return ret_expr # also indented
* see posted examples
* and, if you write the above, then you can call it with the expression:
funct_name( expr1, expr2, ... exprN )
... where you have as many expressions as parameters, and
the value of this arithmetic expression will be the value
of ret_expr.
(and note: ( expr1, expr2, ... exprN ) above are called ARGUMENTS. )