Today: bluescreen, float, comprehensions

Bluescreen Logic

Bluescreen Art Contest (optional!)

> bluescreen-monkey

Also a PyCharm project with monkey example: bluescreen.zip. This example shows both "front" (above) and back strategy.


Aside: HP 35 Calculator

Two Math Systems, "int" and "float"

# int
3  100  -2

# float, has a "."
3.14  -26.2  6.022e23

Math Works

1. int, int + int = int

>>> 1 + 2
3
>>> type(3)

>>> 10001 - 24
9977
>>> 10001 // 10
1000
>>> 10001 % 10
1
>>> 10 ** 3
1000

One exception: / yields a float - crosses the two worlds

>>> 5 // 2  # // int division, no fraction
2
>>> 5 / 2   # / division, yields float
2.5

float

>>> 7 / 2     # get float
3.5
>>> 7 // 2    # get int
3

float Arithmetic: float + float = float

>>> 3.14
3.14
>>> type(3.14)

>>> 
>>> 3.14 * 2.0 
6.28
>>> 3.14 * 2.0 + 1.0
7.28
>>> 10.0 ** 6.0
1000000.0

float Scientific Notation "3.14e26"

Mixed Case: int + float = float "promotion"

>>> 1 + 1 + 1.0
3.0
>>> 3.14 * 2
6.28
>>> 3.14 * 2 + 1
7.28

float() int() Conversions

-Use float() to convert str to float value, similar to int()
>>> float('3.14')   # str -> float
3.14
>>> int(3.14)       # float -> int, truncation
3
>>> int('16')
16
>>> int('3.14')
ValueError: invalid literal for int() with base 10: '3.14'

int for Indexing, Not float

>>> lst = ['a', 'b', 'c', 'd']
>>> mid = len(lst) // 2     # int division
>>> mid
2
>>> lst[mid]                # fine
'c'
>>> mid = len(lst) / 2      # float
>>> mid
2.0
>>> lst[mid]
TypeError: list indices must be integers or slices, not float

Float Applications

Float - One Crazy Flaw - 1/10

Crazy Flaw Demo - Adding 1/10th

>>> 0.1
0.1
>>> 0.1 + 0.1
0.2
>>> 0.1 + 0.1 + 0.1    # everyone contemplate this
0.30000000000000004
>>> 
>>> 0.1 + 0.1 + 0.1 + 0.1
0.4
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.5
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.6
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.7
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1
0.7999999999999999     # here the garbage is negative

Another example with 3.14

>>> 3.14 * 3
9.42
>>> 3.14 * 4
12.56
>>> 3.14 * 5
15.700000000000001   # this is why we can't have nice things

Summary: float math is slightly wrong

Why? Why Must There Be This Garbage?

Crazy, But Not Actually A Problem

Must Avoid One Thing: no ==

>>> a = 3.14 * 5
>>> b = 3.14 * 6 - 3.14
>>> a == b   # Observe == not working right
False
>>> b
15.7
>>> a
15.700000000000001
>>> abs(a-b) < 0.00001
True

Float Conclusions


List Comprehensions - Magic

Comprehension Syntax 1

>>> nums = [1, 2, 3, 4, 5, 6]
>>> [n * n for n in nums]
[1, 4, 9, 16, 25, 36]
>>> [n * -1 for n in nums]
[-1, -2, -3, -4, -5, -6]

Comprehension If 2

>>> [n for n in nums if n > 3]
[4, 5, 6]
>>> [n * n for n in nums if n > 3]
[16, 25, 36]

Comprehension Type Change 3

>>> [str(n) + '!' for n in nums]
['1!', '2!', '3!', '4!', '5!', '6!']
>>> [(n, n)  for n in nums]
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]

Comprehension Style Notes

You Try It - Comprehensions

These are all 1-liner solutions with comprehensions. We'll just do 1 or 2 of these. Syntax - e.g. make a list of squares:

[n * n for n in nums]
> Comprehensions