Today - interpreter, math int/float, style, black box examples, boolean logic, string introduction, string +=, string for/i/range loop

Big Picture Strategy - Divide and Conquer

Data Flow Picture

Here is a way of looking at a program.

You have some files with data in them. You write code to lode the data into Python in one form. Then compute a transform into another form. Then a further computation produces an "answer" form. You publish the answer and get tweets of praise - the coding lifecycle! You can think of this as a sort of "data flow", from the original data to the final output. You can think of each step in this picture as one function - takes in sort of data, transforms it to another form. alt: data flows through program

Today: Black Box Function

"Black Box" Function Model

function is black box with parameters in and result out

Black-box model of a function: function runs with parameters as input, returns a value. All the complexity of its code is sealed inside.

The black box model makes it easy to call the function - provide data you have as the input, call the function, get back an answer.

Strategy - One Function at a Time, Sealed Off

For Divide and Conquer, want to be able to work on each function separately, one at a time. We need to keep the functions sealed off from each other as much as possible. The black box model works well for this, narrowing the interactions to just the input and output data for each function.


Write Black Box Code - Cases - Systematic

Here we'll work through the code for first black-box examples. There are in the logic 1 section on the experimental server.

We'll think about the various cases for the input, since ultimately the code needs to handle all possible input cases. Thinking about the various cases is an easy way to get started with the code for a function. We might use the word systematic to describe are approach here - think through all the cases to make sure each is handled.

1. winnings1() Example

> winnings1()

score -> winnings
0        0
1        10
2        20
3        30
4        40
5        60  # 12x kicks in here
6        72
7        84
8        96
9        108
10       120

1. Function Input: Parameter

def winnings1(score):
    ...

2. Function Output: return result

def winnings1(score):
    return score * 10

Experiment 1 - winnings1()

You can type these in and hit the Run button as we go or just watch as we go. The results are pretty easy to follow.

def winnings1(score):
    return score * 10

Experimental Server Output Format

alt: experimental server output table

if-return Pick-Off Strategy

Experiment 2

def winnings1(score):
    if score < 5:
        return score * 10

Add if-logic to pick off the < 5 case. Run it. We can see that it's right for half the cases.

None Result

Experiment 3 - Very Close

def winnings1(score):
    if score < 5:
        return score * 10

    if score >= 5:
        return score * 12

Add pick-off code for the score >= 5 case (>= means greater-or-equal). This code returns the right result in all cases. There is just at tiny logical flaw.

Experiment 4 - Perfect

def winnings1(score):
    if score < 5:
        return score * 10
    
    # Run gets here: we know score >= 5
    return score * 12

What About This Code?

What about this solution...

def winnings1(score):
    if score < 5:
        return score * 10
        return score * 12

The above does not work. The second return is in the control of the if because of the indentation. It never runs because the line above exits the function. Obviously indentation is very significant for what code means in Python.

Think Like A Programmer - Systematic Cases


2. Winnings2 Example

Extra practice example.

> winnings2()

Say there are 3 cases. Use a series of if-return to pick them off. Need to think about the order. Need to put the == 10 case early.

Winnings2: The int score number is in the range 0..10 inclusive. Bonus! if score is 10 exactly, winnings is score * 15. If score is between 4 and 9 inclusive, winnings is score * 12. if score is 3 or less, winnings is score * 10. Given int score, compute and return the winnings.

Solution

def winnings2(score):
    if score == 10:
        return score * 15
  
    if score >= 4:  # score <= 9 is implicit
        return score * 12
    
    # All score >= 4 cases have been picked off.
    # so score < 4 here.
    return score * 10

    # Here the cases are handled from 10 to 0.
    # The opposite order would be fine too.

Boolean Values

For more detail, see guide Python Boolean

Ways to Get a Boolean

Try this in the interpreter (Hack Interpreter)

>>> n = 5   # assign to n to start
>>> n == 6
False
>>> n == 5
True
>>> n != 6
True
>>> 
>>> n < 10
True
>>> n < 5  # < is strict
False
>>> 
>>> n <= 5  # less-or-equal
True
>>> 
>>> n > 0
True

Boolean Operators: and or not

Weather Examples - and or not

Say we have temp variable is a temperature, and is_raining is a Boolean indicating if it's raining or not. Here are some examples to demonstrate and or not:

# OR: either or both are true
# Say we don't want to go outside if
# if it's cold or raining.
if temp < 50 or is_raining:
    print('not going outside!')


# AND
# We want to go outside if it's snowing.
if temp < 32 and is_raining:
    print('yay snow!')

Boolean Exercise

What is the code to detect if the temp is over 70 and it's not raining?

if -what goes here?-:
    print('Sunny and nice!')

Style Note: no == True

In an if or while test, do not add == True or == False to the test - though the English phrasing of it does tend to include those words.

Do it like this:

if is_raining:           # YES like this
    print('raining')

Not like this:

if is_raining == True:   # NO not like this
    print('raining')

The if/while checks if the test is True or False on its own.

Numeric and Example

Suppose we have this code, and n holds an int value.

if n > 0 and n < 10:
   # get here if test is True

What must n be inside the if-statement? Try values like 0 1 2 3 . 8 9 10

You can work out that n must be a an int value in the range 1..9 inclusive.

3. is_teen(n) Example

> is_teen()

is_teen(n): Given an int n which is 0 or more. Return True if n is a "teenager", in the range 13..19 inclusive. Otherwise return False. Write a boolean test to pick off the teenager case.

Solution

def is_teen(n):
    # Use and to check
    if n >= 13 and n <= 19:
        return True

    return False

    # Future topic: possible to write this
    # as one-liner: return n >= 13 and n <= 19

4. Lottery Scratcher Example

> scratcher()

An extra example for practice.

Lottery scratcher game: We have three icons called a, b, and c. Each is an int in the range 0..10 inclusive. If all three are the same, winnings is 100. Otherwise if 2 are the same, winnings is 50. Otherwise winnings is 0. Given a, b and c inputs, compute and return the winnings. Use and/or/== to make the tests.

Solution:

def scratcher(a, b, c):
    # 1. All 3 the same
    if a == b and b == c:
        return 100
    
    # 2. Pair the same (3 same picked off above)
    if a == b or b == c or a == c:
        return 50
    
    # 3. Run gets to here, nothing matched
    return 0

Strings

For more detail, see guide Python Strings

len() function

>>> len('Hello')
5
>>> s = 'Hello'   # Equivalently
>>> len(s)
5
>>> len('x')
1
>>> len('')
0

Empty String ''

Zero Based Indexing - Super Common

alt: the string 'Python' with 6 index numbers 0..5

String Square Bracket Index

>>> s = 'Python'
>>> s[0]
'P'
>>> s[1]
'y'
>>> s[4]
'o'
>>> s[5]
'n'
>>> s[6]
IndexError: string index out of range

String Immutable

>>> s = 'Python'
>>> s[0]        # read ok
'P'
>>> s[0] = 'X'  # write not ok
TypeError: 'str' object does not support item assignment

String +

>>> a = 'Hello'
>>> b = 'there'
>>> a + b
'Hellothere'
>>> a
'Hello'

1. Set Var With =

We've already used = to set a variable to point to a value. The code below sets s to point to the string 'hello'.

s = 'hello'

alt: set s to point to 'hello'

2. Change Var With =

What if we use = to set an existing variable to point to a new value? This just changes the variable to point to the latest value, forgetting the previous value. The assignment = could be translated to English as "now points to".

s = 'hello'
# change s to point to 'bye'
s = 'bye'

alt: change s to point to new string

Useful Pattern: s = s + something

>>> s = 'hello'
>>> s = s + '!'
>>> # Q: What is s now?

alt: change s to point to new string

Answer: s is 'hello!' after the two lines. So s = s + xxx is a way of adding something to the right side of a string. The following form does the exactly the same thing using += as a shorthand:

>>> s = 'hello'
>>> s += '!'

String += Strategy

Use a series of += to add on the end.

>>> s = 'hello'
>>> s += '!'
>>> s += '!'
>>> s
'hello!!'

alt: change s to point to new string


Recall: String Index Numbers

Here is our string, using zero-based index numbers to refer to the individual chars..

>>> s = 'Python'
>>> s[0]
'P'
>>> s[1]
'y'
>>>

alt: string 'Python' with index numbers 0..5

How To Loop Over Those Index Numbers?

The length of the string is 6. The index numbers are 0, 1, 2, 3, 4, 5. How to write a loop that generates those numbers? Use the same range(n) we used to generate x values for an image. Length is 6 and want numbers 0, 1, 2, 3, 4, 5 *mdash; range(6) generates those numbers. Or more generally, use: range(len(s)) generates the index numbers for a string.

Standard loop: for i in range(len(s)):

This is the standard, idiomatic loop to go through all the index numbers. It's traditional to use a loop variable with the simple name i with this loop. Inside the loop, use s[i] to access each char of the string.

# have string s, loop over its chars
for i in range(len(s)):
    # access s[i] in here

1. double_char() Example

double_char(s): Given string s. Return a new string that has 2 chars for every char in s. So 'Hello' returns 'HHeelllloo'. Use a for/i/range loop.

> double_char

Solution code

def double_char(s):
    result = ''
    for i in range(len(s)):
        result = result + s[i] + s[i]
    return result

Also, see the experimental server section string2 for many problems like double_char()

print() In The Loop

The experimental server has a feature to give a little insight about what's going on inside the loop.

Add inside the loop: print(i, s[i])

def double_char(s):
    result = ''
    for i in range(len(s)):
        print(i, s[i])
        result = result + s[i] + s[i]
    return result

This will print one line for each iteration of the loop, showing what i and s[i] are as the loop runs.

Recall that the print() output is separate from the formal return output of the function. In the experimental server, the print output is shown below the function result, looking like this for the 'xyz' input:

alt: double char output with print

print(i, s[i]) - Think About Output

Say the input is 'xyz'

Q: How many iterations will the loop run?

A: 3 - it runs through the index numbers 0, 1, 2

Q: What lines will print(i, s[i]) produce?

A:

0 x
1 y
2 z

Practice: add_exclaim(s)

> add_exclaim