Python: string indexing

The tutor gives a Python example of string indexing.

In computer science, a string is a word which can include non-alphabetical characters. An example of a string is “desk17”.

Let’s imagine creating the variable thestring with the value “desk17”, as follows:

thestring=”desk17″

The characters in “desk17” can be referred to by number. Going forward, the letter d is in position 0. However, the string can be counted backward: 7 is in position -1. Therefore, the program


thestring=”desk17″
print(thestring[0])
print(thestring[-1])

produces the output

d
7

I’ll be talking more about Python strings in future posts:)

Source:

Donaldson, Toby. Python, 3rd Edition. Peachpit Press, 2014.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Python: the power function

The tutor focuses on Python’s pow(x,y) command.

Back in my post on May 3, 2014 I pointed out that

    \[\sqrt[c]{a}^b=a^{\frac{b}{c}}\]

In the left expression above, c is the root while b is the exponent. a you might call the argument.

On the right, a is once again the argument, while \frac{b}{c} is the rational exponent.

Note that it follows that

    \[\sqrt{49}=49^{\frac{1}{2}}=7\]

since the exponent on 49 in the left expression, not being written, is understood to be 1, while the root, not being stated, is assumed to be 2.

Another example:

    \[\sqrt[4]{625}^{3}=625^{\frac{3}{4}}=125\]

Python’s pow function seems to favour the a^{\frac{b}{c}} interpretation. Before using pow, you need the line

import math

to make pow available. Then, pow can be used as follows:

math.pow(argument, rational exponent)

Therefore, the little program

import math
print(str(math.pow(3,2)))
print(str(math.pow(8,1/3)))
print(str(math.pow(32,2/5)))

should yield the output


9.0
2.0
4.0

I’ll be talking more about Python math functions in future posts:)

Source:

Donaldson, Toby. Python, 3rd Ed. Peachpit Press, 2014

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chess: kingscrusher

The tutor mentions an inevitable Web discovery for a chess enthusiast.

I heard about kingscrusher from Sean Godley at Killegar Chess.

Kingscrusher’s site is likely appealing to chess enthusiasts who have short durations to spend watching. The reason is that he posts blitz games – his own. Each game includes candid commentary about his own moves as well as his suspicions about his opponent’s. His love of the game is evident, win or lose.

Kingscrusher also commentates games between masters, of which I’ve not seen many so far. However, I’ll likely be watching more soon. As I do, I’ll keep you posted:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Python: (lack of) punctuation

The tutor comments on a possibly surprising characteristic of Python.

Unlike Perl or Java, Python doesn’t use semicolons at the end of statements. Furthermore, it doesn’t use braces around loops. A loop body is indicated by indentation:

for i in range(3):
   print(“Hello, “)
   print(“how are you?”)
   print(“\n”)
print(“Cheers:)”)

yields the output


Hello,
how are you?
Hello,
how are you?
Hello,
how are you?
Cheers:)

Notice that the indented print statements are executed each pass through the loop, while the non-indented print statement is only executed once.

Python’s sparse use of punctuation might surprise programmers from other languages:)

Source:

Donaldson, Toby. Python, 3rd Edition. Peachpit Press, 2014.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Plant identification from field guides: oceanspray

The tutor, continuing with his identification of familiar plants, shares another find.

One guide compares the creamy clouds of flowers on oceanspray (Holodiscus discolor) to lilacs, but they’re less structured than some lilac flowers; I’d say their stems are softer and reach further from the main branch. From my observations, lilac limbs are heavier than oceanspray ones as well.

Oceanspray is very common around here; it’s often an ornamental, but just as often an accidental on the fringe of a woodsy patch. As the guide suggests, the flowers turn brown but stay on the bush. The leaves are serrated.

Oceanspray might be up to 10 feet tall. Its thin limbs lean outward. My guide says it’s common all over Vancouver Island, as well as on the south BC and Washington coast.

Source:

Pojar, Jim and Andy MacKinnon. Plants of Coastal British Columbia. Vancouver:
   BC Ministry of Forests and Lone Pine Publishing, 1994.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Calculator usage: linear regression on the Staples BD-6120G

The tutor focuses on the Staples BD-6120G, which has some convenient features.

Overlooking the Staples BD-6120G might be easy: I can’t recall any of my students using it. Yet, I picked one up from Staples’ abundant shelves; someone must use it.

As a scientific calculator, the BD-6120G is all my students would need; moreover, it has graphing capabilities. While perhaps not so powerful as the TI-83 family of graphers, the BD-6120G could probably get the user out of most high school jams.

Today I’m looking at how to do linear regression with the Staples BD-6120G. Specifically, suppose you want to fit a model equation of the form y=a+bx to the following data:

x y
4.1 2.3
10.8 6.1
21.4 15.2

With the Staples BD-6120G, here’s how:

  1. Turn the calculator on. Press the mode button, arrow over to reg, then press =, at which time lin will appear on the screen. Press = a second time to choose linear regression. Afterwards, reg will appear in the top-left corner.
  2. From the table, enter the data pair (4.1, 2.3) as follows:
    4.1 shift hyp 2.3 M+
    (An unexpected graph might appear as you do this; just ignore it and continue:))
  3. Repeat Step 2 for the next two data pairs in the table.
  4. To get the equation of the form y=a+bx, you need the values of the a and b parameters. To find a, press shift 7 = (which yields, in this example, -1.279495773). To find b, press shift 8 = (which yields, in this example, 0.755881193).
  5. It’s best not to forget to actually write the equation with the values for a and b substituted in. For this example the regression equation is as follows:

    y=-1.27950 + 0.75588x

Best wishes with this technique!

Source:

Staples BD-6120G Owner’s Manual.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Plant identification from field guides: the common sow thistle

The tutor continues a survey of the wild plants in the yard.

I’ve been looking for other plants to identify. There is one in some gravel; I’d say I’ve pulled it up many times. Yet, it persists. This time I’ve decided to write a post about it.

The spines at points on the leaves suggest it’s a thistle. Thistles, like so many other “wild” plants around here, belong to the Aster family. Its yellow ray flowers suggest it’s a sow thistle – but which one?

The guide reports that the common sow thistle has leaves with sharp-pointed, clasping flanges, which means that, at the stem, the leaf reaches around as it it’s holding on with pointed arms. This plants has those. Furthermore, its achenes (the achene is the stem below the flower that remains after the flower passes) seem to be cross-wrinkled, as if they’re wrapped up like Halloween candies. Based on the flanges and the cross-wrinkling, the plant is apparently a common sow-thistle.

I’ve never thought to look at flanges of leaves before; it’s interesting that the features of a plant that help identify it are ones a person might not notice most of the time.

HTH:)

Source:

Pojar, Jim and Andy MacKinnon. Plants of Coastal British Columbia. Vancouver:
    BC Ministry of Forests and Lone Pine Publishing, 1994.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Plant identification from a field guide: fireweed

The tutor shares a discovery from his yard.

We have a lawn, but also a bark-chip area in front of our house. During a season, I let parts of it grow more or less freely: some very colourful blooms populate it.

Yesterday my wife asked about the “fluffiness” of some flowers in the bark chip area. I told her it didn’t surprise me; though I’d never paid particular attention, it seemed ordinary. Most of the flowers are already gone, but a few pink ones remain. The plant grows straight up, over a metre tall.

Today, I decided to identify that plant, then decide if, indeed, the fluffiness is normal. I picked up the field guide and outside I went, first examining the plant, then sitting back in a lawn chair scanning the guide’s pages for it.

I wouldn’t have guessed it to be of the evening primrose family, yet, voilà:  it’s unmistakably fireweed (Epilobium angustifolium, aka Rosebay Willowherb), an evening primrose member.  The guide reports it common in disturbed areas (which our bark chip area is, since I rake it a few times annually), and up to 3m tall – once again, correct.

The flowers, it reports, are found in clusters atop the the stem.  They have groups of 4 petals and 4 sepals.  The remaining fruit is just the “stalk” that supported the flower; it might be a little more purple and a little enlarged.  According to the guide, it opens to release “hundreds of fluffy, white seeds” – hence the fluffiness.

Hikers can picture this erect herb, up to 3m tall, with green, lance-shaped leaves below, and pink inflorescence above.  The guide points out its commonness on railroads – not that we have many of those locally.  It’s also common on old burn sites, leading to its name.

HTH:)

Source:

Pojar, Jim and Andy MacKinnon. Plants of Coastal British Columbia. Vancouver:
   BC Ministry of Forests and Lone Pine Publishing, 1994.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Python: a few comments about the print command

The tutor seeks to clarify some ideas about Python’s print command.

I refreshed Python on one of my computers yesterday; I uninstalled Python 2.7, then installed Python 3.4 instead. To do so, I went to python.org. I was using a Windows 7 computer at the time; the site suggested a download for me, so I just followed its lead. Later I tried a test script. The whole process took inside of 10 minutes. Everything works flawlessly, so far as I know.

Of course, there are differences between Python 2.x and 3.x. A key one that might affect people still running Python 2.x concerns the print command.

In Python 2.x, print doesn’t require brackets. Therefore,

print “Hello!”   #2.x

works perfectly. However, in Python 3.x, brackets are required:

print(“Hello!”)   #3.x

In 2.x, print always attaches a newline. Therefore,

print “Hello!”
print “Ciao!”

will give the output

Hello!
Ciao!

In 3.x, you can stop the newline’s being added with end=” “:

print(“Hello!”, end=” “)
print(“Ciao!”)

gives the output

Hello! Ciao!

To my knowledge, the end=” “ trick doesn’t work in Python 2.x.

HTH:)

Source:

Donaldson, Toby. Python, 3rd Ed. Peachpit Press, 2014.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chess: advancing the king’s bishop pawn

The tutor continues his exploration of chess.

Since I always play Black, the king’s bishop pawn’s home square is f7. Advancing that pawn, especially in the opening, potentially weakens Black’s king side, especially if White still has the white bishop.

On the other hand, advancing the pawn from f7 hinders White’s use of the king side; the pawn can even be used in an attack on White’s castled king. Furthermore, it develops Black’s rook, if it’s still on f8. The rook and the pawn can coordinate good attacking chances, if the risk to Black’s king is properly managed.

I had a good win against GNU chess (easy setting, although, as I comment here, it’s not necessarily easy) after advancing the f7 pawn during the opening. However, it’s a new strategy for me; I’m not sure it’s sound.

I’ll keep you posted about my findings with this new strategy:)

Source:

Pachman, Ludek. Modern Chess Strategy. New York: Dover Publications, Inc., 1963.

Oracle Tutoring by Jack and Diane, Campbell River, BC.