In-class notes for 02/21/2014 (CS 121B (CS1), Spring 2014)
>>     < >




In-class notes for 02/21/2014

CS 121B (CS1), Spring 2014

  • Homework assignment

  • 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
    Example call:
      4+5 --> 9
    
    But assignment is a Python3 statement, like for or return -- 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

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 returns True if x is negative and False otherwise; and

    • x == 0, which returns True if x 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, and False 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, and False 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 or False).

  • The operators < and == are predicates, as are and, or, not, >, etc.

    and   (operator)

    2 arguments: Boolean values
    Return: Boolean, True if both arg1 and arg2 are True, and False otherwise
    Example calls:
        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 are or and not.

    or   (operator)

    2 arguments: Any two boolean values
    Return: A boolean value, True if either arg1 or arg2 is True, or if both are True; False if both arg1 and arg2 are False
    Example calls:
        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 is True, and True if arg1 is False
    Example calls:
        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

  1. 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.
    Example calls:
       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.

  2. 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
    Example call:
        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 call
        invite(['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.




< >