Unit 1: Programming Basics

Lesson 3: Type Casting

Notebook Authors

(fill in your two names here)

Team Roles

Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)

If there are only three people in your team, have one person serve as both spokesperson and process analyst for the rest of this activity.

At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.

Computational Focus: Python Scripts

Model 1: Explicit Type Cast

Every variable in Python has a data type. The example code below demonstrates how you can convert a variable to one of three data types: integers, floating point numbers, and strings.

Type (don't copy and paste) the following code, one line per Jupyter cell, to observe the output (if any):

pi = 3.1415
type(pi)
integer = int(pi)
word = str(pi)  
print(pi)  
print(integer)  
print(word)
number = float(integer)
print("the final value is", number)

Critical Thinking Questions

1. What is the data type (integer, floating-point number, or string) of each of the following variables?

1a. pi

1b. integer

1c. word

1d. number

2. List the function calls that convert a variable to a new data type.

3. Explain the syntax for printing multiple items in one line.

4. Predict what will happen if you multiply each of the four variables by 2. Then run the code and verify that it occured correctly. The first Jupyter code cell is provided for you.


In [ ]:
pi*2

5a. Explain the difference between the two number data types (integer and floating point).

5b. Explain the difference between a number data type (integer and floating point) and a string data type.

Model 2: Multiple Parameters, Multiple Return Values

A function can take more than one parameter each separated by a comma. It can also return multiple values if they are separated by a comma and enclosed by square brackets [ ].

Type the following function definition in a single Jupyter code cell (don't copy and paste):

def polar(x,y):
    """Convert Cartesian (x,y) coordinates to polar and return (r,theta), with theta in radians."""
    import math
    print("x = ", x)
    squared_sum = x*x + y*y
    r = math.sqrt(squared_sum)
    theta = math.atan(y/x)
    return r,theta

Note: The triple double-quotes (""") are used around a docstring that explains how to use a function. Notice that the example above has the "Do this", "Return that" structure. It is both good practice and required for class that you write a docstring for every function that you define. More technical details about docstrings.

Read the Critical Thinking Question BEFORE typing the following lines of Python code into separate code cells:

polar(15,2)
print(polar(15,2))
distance, angle = polar(15,2)
print(distance)
print(angle)
help(polar)

Critical Thinking Questions

6. After typing only the function definition, how many times does the polar function execute? Provide justification for your group’s answer.

7. Now type the first function call into its own cell cell. What is the value and type of the variable x (the parameter by the polar function)?

8. Now run the second function call (print(polar(15,2))) in its own code cell. Explain the effect of using the print function with the function call.

9. Now run the remaining lines of Python code in three more code cells.

9a. Write an assignment statement (in a markdown cell) showing how the variable distance gets its value.

9b. Write an assignment statement (in a markdown cell) showing how the variable angle gets its value.

10. What information is provided when you call help on a function you've defined?

Model 3: Local Variables

Recall that an assignment statement takes the form:

variable = value

(You do not need to type the above line in a Jupyter code cell).

Variables that are assigned for the first time within a function are known as local variables. Variables that are assigned outside a function are known as global variables. Because global variables can have unexpected consequences, you should avoid using global variables inside a function.

def polar(x,y):
    """Convert Cartesian (x,y) coordinates to polar and return (r,theta), with theta in radians."""
    import math
    print("x inside = ", x)
    print("x2 inside = ", x2)
    squared_sum = x*x + y*y
    r = math.sqrt(squared_sum)
    theta = math.atan(y/x)
    return [r,theta]

Type in the following statements in separate Jupyter code cells (some of these lines will not run):

polar(15,2)
x = 300
x2 = 600
[distance, angle] = polar(15,2)
print("x outside = ", x)
print("x2 outside = ", x2)
double = distance*2
print(distance, double)

Critical Thinking Questions

11. Examine the polar function.
11a. List all the local variables in the polar function (variables that are assigned values inside the polar function).

11b. List all the global variables used by the polar function.

12a. When the polar function runs correctly, what is the value of x inside the polar function? Explain how x got its value.

12b. What is the value of x outside the polar function? Explain how x got its value.

13a. When the polar function runs correctly, what is the value of x2 inside the polar function? Explain how x2 got its value.

13b. What is the value of x2 outside the polar function? Explain how x2 got its value.

13c. Explain how using global variables in the polar function caused an error sometimes. (These types of errors are the hardest to hunt down, because they don't happen consistently!)

14. Can you print the value of the squared_sum variable outside of the polar function? Offer an explanation as to why or why not.

15. Is it possible to have two variables with the same identifier (variable name)? Explain your answer.

16. It’s important for programmers to be aware of a variable’s scope: where that variable is assigned for the first time and where it is accessible.
16a. Where is the value of a local variable accessible (i.e. where can its value be used)? Provide justification for your group’s answer.

16b. Where is the value of a global variable accessible? Provide justification for your group’s answer.

17. Why is the problem of using global variables inside a function even worse with a system like Jupyter Notebooks (as opposed to writing a script or program, storing it in a file, and executing it)?

Temporal Analysis Report

How much time did it require for your team to complete each Model?

END OF CLASS