Today: int vs float, function call / parameters, PyCharm, command line, bluescreen algorithm

These are all on HW2, due next Wed. To start: look at survey results.

Two Math Systems - int and float

Talk about math briefly for today.

You would think computer code has a lot to do with numbers, and here we are.

See Python Guide: Python Math chapter.

Surprisingly, computer code generally uses two different types of numbers - "int" and "float".

int numbers

Math Operators: + - * / **

>>> 2 + 3       # ints
5
>>> 2 + 3 * 4   # Precedence
14
>>> 2 * (1 + 2) # Use parenthesis
6
>>> 10 ** 2     # 10 Squared
100
>>> 2 ** 100    # Grains of sand in universe
1267650600228229401496703205376
>>>
>>>
>>> 1 + 2 * 3    # int in, int out (except /)
7

float numbers

>>> 2.0 * 3.5        # float in, float out
7.0
>>> 2.0 * 3.5 + 1.0
8.0
>>> 
>>> 2 * 3.5 + 1      # one float -> float result
8.0
>>> 
>>> 6.02e23        # "e" float - 1 mole
6.02e+23 
>>> 6.02e23 * 2    # 2 moles
1.204e+24
>>>

Conversions int() float()

The formal names of the types are "int" and "float". As a handy Python convention, there are functions with those names that try to convert an input to that type. The int() function just discards the fractional part of the number.

>>> int(3.6)
3
>>> float(3)
3.0

Division / → float

>>> 7 / 2
3.5
>>> 8 / 2
4.0

What Does Left-to-Right Mean?

With a series of operators of the same precedence, the operators are evaluated from left to right. Start with the leftmost number, then apply each operation to it, going from left to right. Example (click to reveal):

>>> 4 - 2 + 1
3

Python starts with 4, and does the operations one at a time, left to right - so subtracting 2, then adding 1, resulting in 3.

It's the same with multiplication and division. What is the result of this expression:

>>> 8 / 4 * 2
4.0

Start with 8, then divide by 4, yielding 2.0. Then multiply by 2, resulting in 4.0.

In this case, the answer is float 4.0 because the division operation / produces a float result, even with int inputs.

Why Do We Care - int vs float?

Indexing into a structure is very common, e.g. get_pixel(x, y), and in this context, the x and y will always be int.

Indexing - get_pixel(x, y) - int x, y


Function Call - How Parameters Work

bit.paint('blue')

The parentheses part of the function call contains the parameter values for the function, also knows as the arguments.

We hand-waved how that worked before, but today we go through it in detail.

Function: darker_left

Say we have a darker_left function that takes in the name of an image file, and a number. It computes a new image, darkening the number of pixels on the left of the image corresponding to the number, like this:

alt: darker_left takes in image and number

The structure here is we want to call a function and pass in some extra information like 'poppy.jpg' and 50 to go into the computation. This is role of parameters in Python functions.

Example: Darker Left

> Darker Left

We have the darker_left() function on the experimental server to see how parameters work.

1. Function Has Two Forms - Def and Call

We think of a function as a single thing, but in fact each function appears in the code in two different forms — the def form, and the call form. We'll look at them in turn...

2. Def Form - Name + Parameters

The def has the function name and its parameters listed within parenthesis. There is only a single def for each function, but there can be many calls to that function.

def darker_left(filename, left):
        ...

This function has two parameters, filename and left.

What is the meaning of each parameter?

Each parameter holds a value for the code in the function to use. In this case, the filename parameter is the name of the image file to work on. The left parameter specifies the number of pixels on the left to darken.

We can say that the parameters are "coming in" to the function when it runs.

3. How To Write Code?
Parameters Are Ready To Use

To write code within the def, assume that each parameter is ready to use. The function code simply uses each parameter, relying on the right value being in there.

Each parameter is like a variable, and for a def like this, the variables are already set to point to appropriate values. So in this case filename and left are already pointing to the right values, and the code in the def simply uses them.

The attitude here is one of trust - the code is given filename and left and just uses them.

alt:each param has a value

Write Code for darker_left()

> Darker Left

Here is the code with the key line missing:

def darker_left(filename, left):
    image = SimpleImage(filename)
    for y in range(image.height):
        for x in range(  ?????  ):
            pixel = image.get_pixel(x, y)
            pixel.red *= 0.5
            pixel.green *= 0.5
            pixel.blue *= 0.5
    return image

Q: How many pixels on the left to darken?

A: Whatever value is in the left parameter.

Key idea: the parameters filename and left are ready, so just use them by name in the code.

Darker Left Solution

def darker_left(filename, left):
    image = SimpleImage(filename)
    for y in range(image.height):
        for x in range(left):  # Key line
            pixel = image.get_pixel(x, y)
            pixel.red *= 0.5
            pixel.green *= 0.5
            pixel.blue *= 0.5
    return image

4. Where Do the Parameters Come From?

Where do the parameter values come from? The parameter values come from the function call and its parenthesis. The only way a function runs is when a lines calls that function, and the call will specify the values for the parameters.

Function Call Parameters

A call of a function is one line, calling the function by name to run it. The call specifies the values for the parameters within its parenthesis.

How does a function run? The only way a function runs is if there is a line somewhere that calls the function, and that line will have the parameter values.

alt:call specified the parameter values

darker_left() Example Calls

When darker_left() function is called, the parameter values to use are written within the parenthesis. In this case the filename parameter gives the name of the file with the image to edit, e.g. 'poppy.jpg'. The left parameter specifies the number of pixels at the left side of the image which should be darkened.

Here is the Python syntax for three calls to the darker_left() function. You can see the values for the filename and left parameters within the parenthesis.

alt:darker_left called with param values

For each call to the function, we say the parameters are "passed in" to the function. So above the first calls passes in 50 for the left parameter and the last call passes in 2.

Look in the Run Menu

Look at the Run Menu on the experimental server for darker_left() carefully. For each case, you can see the syntax of the function call in the menu — the name of the function and a pair of parenthesis. Look inside the parenthesis, and you will see the parameters passed in for each run. Click the Run button with different menu selections to see it in action, essentially calling the function that way.

Interpreter >>> foo(a, b) Example

What do you see here?

def foo(a, b):
   ...
   ...

It's a function named "foo" (traditional made-up name in CS). It has two parameters, named "a" and "b".

Each parameter is for extra information the caller "passes in" to the function when calling it. What does that look like? Which value goes with "a" and which with "b"?

A call of the foo() looks like this:

   ....
   foo(13, 100)
   ...

In the function call above, values match up by position within the parenthesis.

The first value in the call is 13, so it goes to the first parameter in the def (a). The second value in the call is 100, so it goes to the second parameter in the def (b).

Call foo() in Interpreter

We can do a little live exercise in the interpreter to see function call in action.

The hack mode "interpreter", has a ">>>" prompt. You type a line of python code here, hit return. What you typed is sent to python to run, the results are printed after the ">>>".

> Experimental Interpreter >>>

Here we'll enter a little two-line def of foo(a, b) in the interpreter, then try calling it with various values. It's very unusual to define a function within the interpreter like this, but here's one time we'll do it. Normally we define functions inside a .py file.

The most important thing is that at the function call line, the parameter values are copied from within the parenthesis to the function parameters. The parameters match by position. So the first value goes to "a", the second value goes to "b".

Since foo() has 2 parameters, the number of values in the function call must be exactly 2 or there's an error, The word "argument" is another word for a parameter, which appears in the Python error messages.

Each parameter value can be an expression - Python computes the value if needed, then sends that value as the parameter.

Each function has its own variables and parameters, separate from the variables and parameters in other functions, even if the variables have the same name. So an "a" inside a function is different from an "a" variable in some other function. This shows up in the last example below.

>>> # Define foo
>>> def foo(a, b):
      print('a:', a, 'b:', b)
>>>
>>> # Call it with 2 param values
>>> foo(1, 2)
a: 1 b: 2
>>> foo(13, 42)
a: 13 b: 42
>>>
>>> # Call with var/expr - passes computed value, e.g. 61
>>> x = 10
>>> foo(1 + x * 6, 1 + 1)
a: 61 b: 2
>>>
>>> # Show that number of parameters must be 2
>>> foo(12)
Error:foo() missing 1 required positional argument: 'b'
>>> foo(12, 13, 14)
Error:foo() takes 2 positional arguments but 3 were given
>>>
>>>
>>> # The "b" here is separate from the "b" in foo()
>>> # Parameters do not match by name
>>> # Parameters match by position - first to "a", second to "b"
>>> b = 10
>>> b
10
>>> foo(b, b + 1)
a: 10 b: 11
>>>

The Command Line

The command line is how your computer works "under the hood", and we'll use it in CS106A. Not fancy looking, but very powerful.

When you type on the command line, you are talking to the operating system of your computer. We'll use it with a free program called PyCharm to edit and run your Python programs - see the Install-Python-PyCharm instructions on the course page.

For details about the command line, see Python Guide: Command Line

The above guide has instructions to download this example folder: hello.zip

Unzip that folder. Go to PyCharm and open the "hello" folder (not the hello.py, the folder). (Windows: confusingly, windows will typically create two "hello" folders, one inside the other. We want the inner "hello" folder.) In PyCharm after opening the folder, select "Terminal" at lower left - that's the command-line area.

First we'll type the command "date" into the terminal and hit the return key to run it. The computer runs the "date" program, and shows its output in the terminal, and then gives us another prompt to type more commands. (In our examples "$" represents the prompt printed by the computer, waiting for you to type something.)

$ date
Fri Oct  6 11:14:23 PDT 2023
$

The basic structure is: the computer prints a prompt (e.g. "$"), you type a command and hit the return key, the computer prints out a response and another prompt.

Then try the "ls" command ("dir" on old windows). The command line runs in the context of some folder on the computer - "ls" lists the files in the folder. Try the "cal" command (sorry, no equivalent on Windows).

Run Python Program hello.py

The file hello.py contains a Python program.

Try running the hello.py program ("python3" on the Mac, "py" or "python" on Windows) with the command shown below. The hello.py program takes in a name on the command line, and prints a greeting to that name. It's a simple program, but it shows how programs are run and how to adjust their inputs.

$ python3 hello.py Alice
Hello Alice
$ python3 hello.py Bob
Hello Bob
$ python3 hello.py -n 100 Alice
Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice 

Use the up arrow in the command line to retrieve and then edit a previous line. We never type in the command from scratch - use the up-arrow instead. Huge time saver!

When typing at the command line, you are typing commands to your computer operating system - Mac or Windows or Linux. This is different from the ">>>" prompt where you are typing command to Python, but the two have a similar type-a-command computer-prints-result structure.

Key Command Line Skills

Image Grid Demo

Then we demo command lines from the image-grid homework. They look like this

$ python3 image-grid.py -channels poppy.jpg
$
$ python3 image-grid.py -grid poppy.jpg 2
$
$ python3 image-grid.py -random yosemite.jpg 3

Those are described in detail in the image-grid homework handout, so you'll see explanations for those when you begin the homework.


Color If Logic

> Logic Stop1

alt: red stop sign

Red Detect red >= 100

Red Detect - Average - Stop2

> Logic Stop2

First, compute the average of red + green + blue for each pixel, like this (parenthesis are needed):

# compute average number for a pixel
average = (pixel.red + pixel.green + pixel.blue) / 3

alt: redish is red value above average per pixel


Bluescreen Algorithm

Bluescreen Algorithm Outline

Diagram:

alt: replace red pixel with pixel at same x,y from back image

Bluescreen Stop Sign Example

This code is complete, look at the code then run it to see.

> Bluescreen Stop Sign

Solution code

def stop_leaves(front_filename, back_filename):
    """Implement stop_leaves as above."""
    image = SimpleImage(front_filename)
    back = SimpleImage(back_filename)
    for y in range(image.height):
        for x in range(image.width):
            pixel = image.get_pixel(x, y)
            average = (pixel.red + pixel.green + pixel.blue) / 3
            if pixel.red >= average * 1.4:
                # the key line:
                back_pixel = back.get_pixel(x, y)
                pixel.red = back_pixel.red
                pixel.green = back_pixel.green
                pixel.blue = back_pixel.blue
    return image

Before - the red stop sign before the bluescreen algorithm:

alt: red stop sign

After:

alt: red part of stop sign replaced with leaves

Bluescreen Monkey

> Bluescreen Monkey

A favorite old example of Nick's.

Have monkey.jpg with blue background

alt: monkey with blue background

The famous Apollo 8 moon image. At one time the most famous image in the world. Taken as the capsule came around the moon, facing again the earth. Use this as the background.

alt: moon image

The bluescreen code is the same as before basically. Adjust the hurdle factor to look good. Observe: the bluescreen algorithm depends on the colors in the main image. BUT it does not depend on the colors of the back image - the back image can have any colors it in. Try the stanford.jpg etc. background-cases for the monkey.

The code is complete but has a 1.5 factor to start. Adjust it, so more blue gets replaced, figuring out the right hurdle value.

This is the "front" strategy - replacing blue pixels in the front image, then the front image is the final output. There is also a "back" strategy on the HW2c handout which you have as an option.

For HW2c you will make your own bluescreen, but we provide most of the code. We will have an art-show/contest with the results.

Bluescreen Contest Categories:

Best artistic

Best humor

Best use of background

Best picture of you with someone super famous