In-class notes for 02/12/2014
CS 121B (CS1), Spring 2014
Runestone accounts... TBA. Let me know (e.g., via Piazza or daily questions form) if you find some things you can't do without creating an account.
Downloading Python3 for your own computer (optional): videos available (these links taken from text)
Sometimes we will need to use the Link computers, so be sure you can also use them.
Submitted questions on assignments and technology
Upcoming
Read online text: Debugging Interlude 1; Python Turtle Graphics; and Python Modules
submit at least one reading question by 9am before the next class meeting
Submitted questions on readings
Course themes
Algorithm: A step-by-step procedure for accomplishing a task that has an end.
Doesn't need to be for a computer, e.g., cooking recipe
Data: individual pieces of information (singular is datum)
Abstraction: selectively forgetting some details in order to focus on a "big picture"
Example: When writing computer instructions to process a data set of names and phone numbers, we selectively forget the many individual values (particular names and numbers) and focus on how to process a name in general, how to process a number in general.
Problem solving: coming up with strategies to accomplish a task or achieve an objective. Requires creativity, reasoning, knowledge -- can use all your skills.
Computer Science (CS) is primarily concerned with problem solving -- a very applicable skill.
Python3 is a programming language, i.e., a language for specifying instructions for a computer to carry out.
Some elements of the computer langauge Python3
Architecture diagram of a computer
Architecture of buildings is the structural design. Likewise, we have structural design for computers, with components such as a CPU (specialized circuitry for carrying out instructions), main memory, permanent storage like disk, input (e.g., keyboard or mouse), output (e.g., screen or printer)
Main memory vs disk: CPU can interact directly with main memory; programs or data on disk must be copied to main memory in order to be processed
Types of data: numbers such as Python3
int
(integer) orfloat
("floating point", allows decimal points); character strings (Python3str
); structured types such as alist
of multiple values (later)A variable is a named location in main memory. Has a name, can have a value.
n = input("What is your name? ")
Here,n
is a variable that holds your response (e.g.,Dick
) as it's value (typestr
).val = int(input("What is your favorite number? "))
Here,val
is a variable that holds response translated into an integer numerical value. Python3 treats the string"123"
differently than the numerical value 123, because they are different types of data.As with human languages, computer languages such as Python3 have expressions (language structures that can have a meaning), syntax (rules for forming correct language expressions), and semantics (assigning meaning to language expressions).
For example, a Python
input
expression must have the following syntax:input ( string )
A Python3 statement is a langauge expression that can be carried out by Python3 software (such as
idle3
). For example,n = input("What is your name? ")
is a statement that prompts for and obtains an input string, then stores that string into a variablen
.An assignment statement assigns a value to a variable (named location in main memory)
In any programming language, a statement is a type of language syntax. In Python, a statement is the basic unit of evaluation: a Python interpreter evaluates one statement at a time.
An expression in a programming language is another type of language syntax. Expressions are language syntax that represent a computation that returns a value.
(Note: CS 121 counts as an elective in the Linguistics Concentration)
Computer functions
Recall mathematical functions:
x f(x) 1 2 -1 2 3 10 0 1 1 2
A mathematical function associates "domain values" x with "range values" f(x), so that every domain value is associated with exactly one range value.Some math functions include square root, trig functions, etc.
A computer function is a procedure of computer actions, that can have 0 or more arguments and may return a value
For example, the
input
function has one argument (a string), and returns a value (also a string); the procedure of computer actions prints the argument string on output and retrieves a string from input, then returns that retrieved string.A function call is a language expression that specifies that a computer function's actions should be carried out. Examples of function calls:
input("What is your name? ") print("Hello", n) math.sqrt(7)
Syntax for a function call: function name, followed by
(
, followed by 0 or more argument values (separated by commas), followed by)
.Nested function calls, e.g.,
val = int(input("What is your favorite number? "))
An operator is a function with specialized syntax.
Examples: Addition operator
+
for numbers (e.g.,2 + 3
); multiplication operator*
for numbers (e.g.,2*3
), concatenation function+
for strings (e.g.,"CS" + "121")
)
< >