Skip to main content

Section 1.1 Using Python

Preview Activity 1.1.1.

Throughout this book, we will use Code cells to run code within a web browser. This activity will help you start to learn how to translate several mathematical operations into simple Python code and execute this code in using a cell.

(a)

Run the below Code cell to compute \(95+87\text{.}\)

(b)

Attempting to run the below Code cell to compute \(11(12)\) will result in something called a TypeError. Try modifying the line result = 11(12) to use Python's * multiplication operator and re-run the cell.

(c)

Compare the results from evaluating both 7/3 and 7//3. Then try using / and // with other numbers to explore.

Write a sentence or two explaining your guess as to why and how these results are different.

(d)

Copy code from previous cells to evaluate and print the result of computing the expressions 6**2 and 6^2. Experiment using ** and ^ with other numbers as well.

Then write a sentence or two explaining how these expressions might have been interpreted by the computer. (Tip: the ^ behaves pretty unexpectedly - don't worry if you don't figure out quite what it's doing on your own!)

(e)

Use a code cell to print out the value of how Python computes 1/10 + 1/5.

Write a sentence explaining why this result might be surprising.

Activity 1.1.2.

In the Python 3 programming language, we may use the addition +, subtraction -, multiplication *, division /, and exponentiation ** operators to evaluate the following mathematical expressions, save their values to a result variable by using the assignment operator = (not “equals”!), and then print(result) to output their values.

Do this for each, and then check each result by computing each expression's value by hand or using a traditional calculator.

(a)

\begin{equation*} 4-3(7) \end{equation*}

(b)

\begin{equation*} 3^2+4^2 \end{equation*}

(c)

Use parentheses, ( and ), to group expressions within more complicated expressions as necessary. Use a code cell to verify the value of the following expression is roughly \(0.733\text{.}\)

\begin{equation*} \frac{2^{7-5}+7}{3\times(1+4)} \end{equation*}

Activity 1.1.3.

Computers generally store numerical data using binary or base-2: sequences of \(0\)s and \(1\)s.

Just like the base-10 expressions we usually write describe values in terms of powers of ten, binary numbers describe values using powers of two.

\begin{align*} 125 &= (1\times 100) + (2\times 10) + (5\times 1)\\ &= (1\times 10^2) + (2\times 10^1)+ (5\times 10^0)\\ &= (1\times 64) + (1\times 32) + (1\times 16) + (1\times 8) + (1\times 4) + (0\times 2) + (1\times 1)\\ &= (1\times 2^6) + (1\times 2^5) + (1\times 2^4) + (1\times 2^3) + (1\times 2^2) +( 0\times 2^1) + (1\times 2^0)\\ &= 1111101_2 \end{align*}

The Python tool format(125,"b") displays the binary form of \(125\) automatically:

Modify this code to find the binary form for each of the following numbers. Then check that the result is valid by adding up appropriate powers of two as shown above.

(a)

\(28\)

(b)

\(63\)

(c)

\(493\)

(d)

\(1000\)

(e)

Python can also automatically convert a binary string to its decimal form using the tool int("1111101",2). Use this tool to programmatically check that each of the binary expressions you found above is correct.

(f)

Running format(6.75,"b") will result in an error. But considering that \(2^{-1}=\frac{1}{2}=0.5\) and \(2^{-2}=\frac{1}{4}=0.25\text{,}\) how might \(6.75\) be expressed as a binary number?

Activity 1.1.4.

While 10^3 in writing might seem to refer to the number \(10^3=1000\text{,}\) in many programming languages such as Python 3, the caret ^ instead refers to the bitwise XOR operation, which can be evaluated by hand using a variation of long addition on the binary form of each number.

\begin{equation*} 10 \verb|^| 3 = 1010_2 \verb|^| 0011_2 \end{equation*}
\begin{equation*} \begin{array}{r} 1010_2\\\verb|^|\hspace{1em} 0011_2\\\hline 1001_2 \end{array} \end{equation*}

Since \(1001_2=9\text{,}\) we may now understand the result of the following code cell.

(a)

Based on the above example, write a sentence guessing how the ^ operation works.

(b)

Test your rule by using it with a few combinations of small numbers (less than 16), using format(n,'b') and x^y in the below Code cell to help you as needed. If necessary, correct your guess above based upon this experiementation.

(c)

Notice what happens to 42 if you apply ^11 to it twice:

Is there anything special about the numbers \(42\) or \(11\) that made this happen? Try different combinations of numbers.

(d)

Alice and Bob both have a common favorite number \(5421483\) that only they know. Alice wants to be pass Bob notes containing other numbers, but doesn't want their nosy friend Eve to find out what those numbers are if she intercepts the note.

Write a short paragraph on how the bitwise XOR operator ^ might be used along with their favorite number to encrypt Alice's notes to Bob.

Activity 1.1.5.

Let's investigate why 1/10+1/5 isn't equal to 3/10 when evaluated in Python.

(a)

Explain why \(0.33\) is the best approximation of \(\frac{1}{3}\) using two decimal places, and why \(0.67\) is the best approximation of \(\frac{2}{3}\) using two decimal places.

(b)

Without using technology, compare the result of adding \(\frac{1}{3}+\frac{1}{3}\) directly and approximating the resulting fraction with a decimal, with replacing each \(\frac{1}{3}\) with \(0.33\) before adding instead.

(c)

Without using technology, find the exact values of each of the following sums.

\begin{gather*} 1+\frac{1}{3}\\ \frac{2}{3}+\frac{2}{3}\\ 1+0.33\\ 0.67+0.67 \end{gather*}

What do you notice?

(d)

Just like \(\frac{1}{3}\) cannot be expressed exactly using a finite number of base-10 digits, fractions like \(\frac{1}{10}\) cannot be described exactly using a finite number of base-2 digits. Replace each \(\square\) below with a \(0\) or \(1\) to create the best binary approximation you can.

\begin{gather*} \frac{1}{10} \approx \frac{\square}{2}+\frac{\square}{4}+\frac{\square}{8}+\frac{\square}{16}+\frac{\square}{32} = 0.\square\square\square\square\square_2\\ \frac{1}{5} \approx \frac{\square}{2}+\frac{\square}{4}+\frac{\square}{8}+\frac{\square}{16}+\frac{\square}{32} = 0.\square\square\square\square\square_2\\ \frac{3}{10} \approx \frac{\square}{2}+\frac{\square}{4}+\frac{\square}{8}+\frac{\square}{16}+\frac{\square}{32} = 0.\square\square\square\square\square_2 \end{gather*}

(e)

Now confirm that the sum of your best binary approximations of \(\frac{1}{10}\) and \(\frac{1}{5}\) do not exactly add up to your best binary approximation of \(\frac{3}{10}\text{.}\)

Exercises Exercises

1.

Find the value of the following expression by hand.

\begin{equation*} 3^{\frac{1+7}{6-2}}\times 4 \end{equation*}

2.

Check your work in the previous exercise using a Code cell.

3.

Use powers of two to verify that \(217=11011001_2\) by hand.

4.

Show how to use both the int and format Python tools to verify that \(217=11011001_2\) programmatically.

5.

Find an approximation of \(\frac{1}{7}\) to three decimal places.

6.

What is the value of your approximation when multiplied by \(7{,}000{,}000\text{?}\) Why isn't the result exactly \(1{,}000{,}000\text{?}\)

7.

Write a paragraph explaining why, in your own words, 0.1+0.2 doesn't result in 0.3 in many programming languages.