In-class notes for 02/21/2014
CS 121B (CS1), Spring 2014
Correction from last time: In Python3, assignment (with
=
is a statement, not an operator.Recall that an operator is a function with specialized syntax for its calls. In contrast, a statement is a unit of programming language syntax indicating a step in a program. For example, the arithmetic operation
+
is an operator, with the following spec:+
(operator)- 2 arguments: Numbers
- Return: A number, the sum of arg1 and arg2
4+5 --> 9
But assignment is a Python3 statement, likefor
orreturn
-- a basic component of a program. Thus (unlike some other programming languages), there is no spec for=
, because it's part of the syntax of an assignment statement instead of being an operator.Quizzes will be returned on Monday
Submitted questions on assignments and technology
Upcoming
Read online text: More About Iteration
submit at least one reading question by 9am before the next class meeting
Submitted questions on readings
if and guards
Syntax for
if
A guard is a boolean-valued expression used in an
if
statement (or another statement that conditionally executes code). Example:x = int(input("enter an integer")) if x < 0: print("negative") elif x == 0: print("zero") else: print("positive")
There are two guards:x < 0
, which returnsTrue
ifx
is negative andFalse
otherwise; andx == 0
, which returnsTrue
ifx
equals zero and false otherwise.
nested; elif
The second guard uses the equality operator
==
. Since an operator is just a function with specialized syntax for its calls, we can give a spec for==
.==
(operator)- 2 arguments: Two Python3 values
-
Return: Boolean,
True
if arg1 and arg2 are the same value, andFalse
otherwise
We could also give a spec for the
<
operator.<
(operator)- 2 arguments: Two numbers, or two strings
-
Return: A boolean value,
True
if arg1 is less than arg2, andFalse
otherwise. Note: if arg1 and arg2 are strings, lexicographic (dictionary) order is used.
Predicate functions
A predicate is a function that returns a boolean value (i.e., either
True
orFalse
).The operators
<
and==
are predicates, as areand
,or
,not
,>
, etc.and
(operator)- 2 arguments: Boolean values
-
Return: Boolean,
True
if both arg1 and arg2 areTrue
, andFalse
otherwise
x = 7 y = 3 x == 4 and y < 10 --> False x == 7 or y < 10 --> True
Predicates such as
and
that have boolean arguments as well as boolean return values are called boolean operators.Besides
and
, the other basic boolean operators areor
andnot
.or
(operator)- 2 arguments: Any two boolean values
-
Return: A boolean value,
True
if either arg1 or arg2 isTrue
, or if both areTrue
;False
if both arg1 and arg2 areFalse
x = 7 y = 3 x == 4 or y < 10 --> True x == 7 or y < 10 --> True x == 4 or y > 10 --> False
Here is a spec for the
not
operator:not
(operator)- One argument: Any boolean value
-
Return: A boolean value,
False
if arg1 isTrue, and
True
if arg1 isFalse
x = 7 y = 3 x == 4 --> False not(x == 4) --> True x == 4 or y < 10 --> True not(x == 4 or y < 10) --> True
______ ______
Exercises
-
Write a function that satisfied the following spec
countNegs
- One argument: A list of numbers
- Return: A non-negative integer, the number of elements in the list arg1 that are negative.
countNegs([1, -2, -3, 4, 0, 6]) --> 2
Hint:Use an accumulator, but only increment it (i.e., add 1) if an element is negative.
Write a function that satisfied the following spec
countSigns
- One argument: A list of numbers
- Return: A list of three numbers, whose first element is the count of negative numbers in arg1, second element is the count of zeroes in arg1, and third element is the count of postive numbers in arg1
countSigns([1, -2, -3, 4, 0, 6]) --> [2, 1, 3]
Local vs. global variables
Recall that a variable is a named location in main memory
Accumulator example:
def invite(namelist): count = 0 # invar: count holds the number of invitations printed so far for name in namelist: print("Hi,", name, "-- please come to my party!") count = count + 1 return count
Example callinvite(['Bob', 'Carol', 'Ted', 'Alice']) --> 4 Hi, Bob -- please come to my party! Hi, Carol -- please come to my party! Hi, Ted -- please come to my party! Hi, Alice -- please come to my party!
Invariant...
The scope of a variable is the range of code in which it is in effect.
< >