24 August 2021

Last Time We did the conversion quiz (people did quite well) and we worked on the Scientific Conversion assignment. That is due next Wed, but if you can turn it in sooner, that is great.

Small Quiz This is on numbers.pdf.

So Far in Python

We have met the three number types. They respond to a full complement of arithmetic operations. The order of operations is Wormwoodean.

We met two other types

We have met variables and know the rules for creating them.

We met the six relational operators, <, >, <=, >=, !=, and ==.

Easy Spelunking Exercise

How do the relational operators act on strings? Let's figure this out.

Ordering of letters:  (ASCII)
    ord("a")  97   0b01100001
    ord("b")  98   0b01100010
    ord("A")  65   0b01000001
    ord("\n") 10   0b00001010

order a string
    dictioary ordering
    compare first if one lesser, report that as less
    repeat for succeeding charcters

lexicographical 

lexicon: dictionary

asc
    ord("0")  48   0b00110000

Reading Read p0.pdf, §§ 0-2.

Some New String Things (§ 3)

single or double You can use single or double quotes to bound a string. You must use the same type on both sides.


>>> cow = 'elsie'
>>> horse = "secretariat"

Any magic character (such as ') becomes non-magic when you whack it by preceding it with a whack (\).


>>> print('horse\'s petoot')
horse's petoot

This arabesque also works.


>>> print("horse's petoot")
horse's petoot

Here are some magic characters; whacking turns magic on for ordinary characters if it exists.


>>> x = "a\tb\nc\n\n"
>>> x
'a\tb\nc\n\n'
>>> print(x)
a	b
c


Here we print a whack.


>>> print("\\")
\

Let's get raw Place an r in front of the string. No escaped characters are expanded.


>>> path_to_perdition = r"C:\Program Files\Bill Gates\hot_mess.py"
>>> print(path_to_perdition)
C:\Program Files\Bill_Gates\hot_mess.py
>>> print(r"\\n")
\\n
>>> #raw string: whack's magic gets whacked.
>>> print("c:\cow\bill gates\nuts")
c:\coill gates
uts
>>> print(r"c:\cow\bill gates\nuts")
c:\cow\bill gates\nuts

Triple Goodness Use triple-quoted strings for multiline strings.


>>> print("""This goes on
... for many lines
... until we are all asleep""")
This goes on
for many lines
until we are all asleep
>>> print('''bleep
... blap
... bomba''')
bleep
blap
bomba

Format strings Place an f in front of the string. Things inside of {} are evaluated and turned into strings, then they are embedded in the string.


>>> x = 5
>>> y = 3
>>> print(str(x) + " + " + str(y) + " = " + str(x + y))
5 + 3 = 8
>>> print(f"{x} + {y} = {x + y}")
5 + 3 = 8

Here is another example.


>>> x
5
>>> y
2
>>> print(f"{x} < {y} is {x < y}")
5 < 2 is False

Ooh, look at this.


>>> for k in range(10):
...     print(f"<tr><td>{k}</td><td>{k*k}</td></tr>")
... 
<tr><td>0</td><td>0</td></tr>
<tr><td>1</td><td>1</td></tr>
<tr><td>2</td><td>4</td></tr>
<tr><td>3</td><td>9</td></tr>
<tr><td>4</td><td>16</td></tr>
<tr><td>5</td><td>25</td></tr>
<tr><td>6</td><td>36</td></tr>
<tr><td>7</td><td>49</td></tr>
<tr><td>8</td><td>64</td></tr>
<tr><td>9</td><td>81</td></tr>

OPC Alert!!!! You will see this in other people's code. These methods are obviated by format strings.


>>> "{} + {} = {}".format(x, y, x + y)
'5 + 2 = 7'
>>> "%s + %s = %s" % (x, y, x + y)
'5 + 2 = 7'

Lists

Here is a list.


>>> x = [1,2,3,4,5]
>>> type(x)
<class 'list'>

Utterly unsurprising.


>>> print(x[0])
1

Casting a string to a list "explodes" it. Note that any object in Python can be cast to a string.


>>> list("cows")
['c', 'o', 'w', 's']
>>> str(x)
'[1, 2, 3, 4, 5]'

It slices, it dices......


>>> x[1:]
[2, 3, 4, 5]
>>> x[1:3]
[2, 3]
>>> x[::2]
[1, 3, 5]
>>> x[1::2]
[2, 4]

Lists are heterogeneous and can hold objects of any type.

Let's make a function. This is an object, too.


>>> def f(x): return x*x
... 
>>> type(f)
<class 'function'>
>>> def f(x): return x*x
... 
>>> type(f)
<class 'function'>

Even a function can be cast to a string. I didn't say it was gonna be pretty.


>>> str(f)
'<function f at 0x7fdf42ad5310>'

Let us go to great lengths.


>>> len("dsfewwee")
8
>>> len(x)
5

The length of a string is the number of characters in it. The length of a list is the number of items in it.

Steel yourself.


>>> menagerie = [2, True, "cow", [1,2,3], 5.6, f]
>>> print(menagerie)
[2, True, 'cow', [1, 2, 3], 5.6, <function f at 0x7fdf42ad5310>]

If it quacks like a duck....


>>> menagerie[5](10)
100
>>> menagerie[0]
2
>>> menagerie[3][1]
2
>>> menagerie[3][2]
3

List entries are lvalues.


>>> x
[1, 2, 3, 4, 5]
>>> x[0] = 6
>>> x
[6, 2, 3, 4, 5]
>>>