Archive for Multi-part Series

An Object-Oriented Primer in Python: Part 1

I’ve read several tutorials on Object-Oriented Programming (OOP), and none of them has been as clear as I thought they should be. So it took me a long time to figure out OOP, and now I feel like I should pass on the knowledge. Here it is:

Objects

One of the main difficulties in OOP is thinking about it. So start trying to wrap your mind around an object. Basically, an object is a specialized data structure than can hold information, and do things with it. In other words, it’s a thing represented by code. And you aren’t limited to just one thing, you can have as many as you want, just as you can have as many numbers, lists or dictionaries as you want.

Holding Information and Doing Things

You can give objects attributes. What’s even cooler is that you can access them really specifically:

fido = Dog("Golden Retriever")
fido.breed

Assuming you’ve made a “Dog” class, fido.breed will be a string saying “Golden Retriever.” You can also change the data (it’d be pretty pointless if you couldn’t…) :

>>> fido = Dog("Golden Retriever")
>>> fido.showBreed()
It's a Golden Retriever
>>> fido.breed = "Labrador"
>>> fido.showBreed()
It's a Labrador
>>> fido.changeBreed("Dalmatian")
>>> fido.showBreed()
It's a Dalmatian
>>> fido.breed
'Dalmatian'

Here we make a Golden Retriever named fido. We have it call a method (OOP-speak for function) to say what breed it is, and then change it’s breed variable to “Labrador.” After that, we use a different method (yes, objects can have multiple methods) to change the breed to “Dalmatian,” and have fido tell us that he is, in fact, a Dalmatian. If you feel like having to prefix everything with “fido.” is annoying, don’t worry: There’s a reason. We could just as easily have:

>>> fido = Dog("Golden Retriever")
>>> spot = Dog("Mutt")
>>> fido.showBreed()
It's a Golden Retriever
>>> spot.showBreed()
It's a Mutt

If we just used a generic ‘showBreed()’ method, Python wouldn’t know whether we were talking about fido or spot, or some other dog. The <name><dot> system is a pretty efficient way of telling Python who’s doing what, or who’s remembering what.

Look back over this section and make sure you understand it well: We made an instance of the Dog class, which held information (fido.breed) and did things (fido.showBreed()), we also made another instance of the same Dog class (spot), which held different, but similar information (Mutt instead of Golden Retriever), and did similar things (said what breed he, spot, was, as opposed to what fido was).

Obviously stuff can get a lot more complicated, but if you understand that, you’re doing fine!

~Read Part 2 to learn how to make classes~

Share/Save/Bookmark

Comments (1)

TI-BASIC (Round Three) (Control Flow: If-Then-End)

Consider the following code:

1->A
If A = 1
Disp "A=1"

Let’s study it a bit. The first line just stores 1 as A. Standard stuff. The second line is freaky, and the third line just displays “A=1″. Standard stuff. Let’s go back to the freaky part. It introduces one of the more powerful commands in TI-BASIC: If. What it says, is that if A equals 1, to do something. The syntax isn’t nearly as remarkable as the code, just If [value][thingy][value]. Thingy can be anything from the ‘Test’ menu. =, <, >, etc.

Note: Due to the constraints of this keyboard, the less-than-or-equal-to sign will be written as <=, and the greater-than-or-equal-to sign will be written as >=. Also, the not-equal-to sign will be written as !=.

If you’re looking for ‘if’, it’s under ‘Ctl’, not ‘I/O’. And this is the first time we’re using ‘Ctl’…Yay! OK, digression over :)

If’s power comes from the fact that it introduces uncertainty into the program. This means the program can rocket off in a lot more directions, which means: Sweeeeeeet.

Since you handled that chunk of code so beautifully, let’s consider this one too.

Input A
If A=1
Disp "A=1"
Disp "Where is this?"

I hope you’re wondering if the ‘Where is this?’ line is part of the if statement or not. It’s not. The only command that’s conditional is the one immediately after the if line. Which is really useful for a code snippet like this:

If A=1
Disp "A is 1"
If A=2
Disp "A is 2"
If A>2
Disp "A is pretty big"
If A<1
Disp "A is pretty small"

That's a lot cleaner than the following method, which is also legal.
If A=1
Then
Disp "A is 1"
End
If A=2
Then
Disp "A is 2"
End
If A>2
Then
Disp "A is pretty big"
End
If A<1
Then
Disp "A is pretty small"
End

You’re probably wondering why that’s legal. It’s stinking huge! Which brings us back to our problem–if only lasts for a line. If-Then-End fixes that problem. Then means that all of the following instructions are only to happen if the statement is true, and End means that an end has been put to the conditional instructions. There’s no arguments required for Then or End (an argument is something that has to be put with it to make the syntax legal.) So let’s look at our ‘Guess!’ program again. Last time we’d gotten this far:

Disp "Guess!","","(GPL) 2008", "T. Macdonald"
#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)

And now we do know how to say if a number is too big or too small!
So
#I'm not copying all the code, we'll pick it up here:
Input "Guess:", G
If G<R
Disp "Too small!"
If G>R
Disp "Too big!"
If G=R
Disp "You win!"

Unfortunately, that’s really all we can do with that, as of right now.
Next time we’ll talk about Else.

Assignment: Write a program that will ask the user for his age, and then will classify them as “Kid” (12 and under), “Teenager” (13-19) or “Adult” (20+).

Share/Save/Bookmark

Leave a Comment

TI-BASIC (Round Two-and-a-Half) (More with Input)

The somewhat astute among us have noticed that in Round *, there was the following line of code:

Input "Guess:",G

And that I haven’t explained how that works.

The pretty astute among us have also noticed that when they run a program such as the following:

Input G

All that’s shown is a question mark, and it’s not very visually appealing.

The quite astute among us have already realized that the way to get rid of the ugly question mark, and replace it with something more attractive is to put the attractive message in quotes, follow it with a comma, and then tack the variable name to the end of it.

Those who are not feeling very astute should be please that the astute have shared their thoughts, and we’re all back on a level playing field.

Finally, the EXTREMELY astute people are saying to themselves “It is such a drag when there’s like four variables that the user has to put in, and I have to make four Input statements. Four! Can you imagine? Surely there’s an easier way.”

Meet Input’s big brother ‘Prompt’. (TI-BASIC is very family-oriented. Output is Disp’s big brother, Input is Prompt’s little brother, Else is If’s big broth–oh, we haven’t gotten there yet. Round three is coming.)

Prompt is great for math. Take the Pythagorean Theorem:

Prompt A,B
A^2+B^2->D
sqrt(D)->C
Disp C

Which will display:

A=?
B=?
(The solution, which depends on the input)

So Prompt is just a quick and easy way of getting a bunch of variables inputted. Would I use it for games, or polished programs? No. Do I use it for quickly making a program that’ll do a mathematical formula? Always.

Share/Save/Bookmark

Leave a Comment

TI-BASIC (Round *) (Special Bonus Edition)

I’ve been thinking, and I realized it’d be nice to add another goal to the TI-BASIC series. So I’m adding a sample program.

About the program: It’s called ‘Guess!’ (I am really creative), and it’s been around since I was in the fifth grade, in one form or another. It consists of guessing a number from 1-100, and being told if it’s too big or too small, and then guessing again, and being told again, etcetera, until you guess the number.

Technical description: Before you make a program, it’s a really really really really really really really really good idea to know exactly what you want it to do. So here’s the ‘outline’ (remember that # and everything after it is a comment):

Guess! (GPL 2008 T. Macdonald) #Title and license (GPL is sort of like a Copyright)

Guess: #User guesses a number

Too big! #If the number’s too big

Too small! #If it’s too small

You win! #If the user guesses it

And that’s it. Believe it or not, the minute it took to write that saved a lot of wasted coding.

So let’s implement what we already know:

Disp "Guess!","","(GPL) 2008", "T. Macdonald"
#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)

Share/Save/Bookmark

Leave a Comment

TI-BASIC (Round Two) (Basic BASIC Input…and Math)

If you have a program that consists solely of displaying things, and it’s not some kind of thrilling narrative or story, then you have problems.
What you need is input.
So here’s how to do it:

Input X
Disp X

Wasn’t that tricky? When you run it, you’ll get a question mark (as in, “Yo! What do you want to input?”) and when you type something in (numbers only, for now), it’ll name the input ‘X’. Then you just ask it to display X.

(Input is in the same menu as Disp, if you’re having trouble finding it. Typing in I then N then P…etcetera, won’t work)

Now let’s do something (somewhat) useful:

Input L #L for length
Input W #W for width
Disp L*W

(NOTE: Everything after a # is a comment. Do not type it into the actual program, it’s just me explaining why I wrote a particular line the way it is. So what you should type in is:)

Input L
Input W
Disp L*W

There’ll be a ‘?’. You type in a number (length of rectangle). There’ll be another ‘?’. You type in another number (width of rectangle). It displays the product of the two numbers (area of rectangle).

In terms of function that ends our lesson on Input. However, I find the ‘?’s pretty ugly. Especially when you have several. So here’s the syntax for pretty-fying them.

Input "Length:",L
Input "Width:",W
Disp "Area is:",L*W

Here’s the output:

Length:3
Width:2
Area is:
                6

One more thing: the ‘Input’ function only accepts numbers. This can be lame if you want the person to, say, type in their name. (What’s your name? 1337)

The TI-86 has a different function called “InpSt” (Input String), which lets the user input a string of letters, not numbers. However, I’m pretty sure that the TI-83/84/80 families do not have this. Darn.

That’s all you need to know about Input to succeed in life :)

Now let’s talk about math. (Don’t worry, it’s a bit more interesting than normal math.) We’ve already touched on it, and the best way of explaining it is giving a sample program.

(Note: I’m going to use ‘->’ for the Store key throughout this entire guide. The Store key has Sto> written on it (the > is filled in though) and is used for assigning values to variables.)(I’ll also use sqrt() for the square root sign)

This program will solve for the hypotenuse of a right triangle.

Input "A:",A
Input "B:",B
A^2 + B^2->D
Disp "C squared is:",D
sqrt(D)->C
Disp "Hypotenuse is:",C

So the moral of the story (erm, program, I mean) is that you can do math with variables in programs.

Share/Save/Bookmark

Leave a Comment

TI-BASIC (Round One-and-a-Half) (Fun with Disp)

We touched on the Disp command in Round One. I’ve since realized that in my quest for simplicity, I didn’t say very much about it. So here are a few things (Every other line is the Disp command, and the other lines are the output)
Disp “2+2″
2+2
Disp 2+2
4
Disp “2+2″,2+2
2+2 (aligned left) [new line] 4 (aligned right)

Disp 2+2,2+2,2+2,3+2

4 [new line]4 [new line]4 [new line]5 [new line]

Output(1,1,”Hello World!”)

(Note: This is not Disp. This is Disp’s big brother Output. Output let’s you ‘plot’ the output on the screen. So the ‘1,1′ means that it puts the output in the first column and the first row.

Hello World!

So now you should have a better handle on the syntax

Share/Save/Bookmark

Leave a Comment

TI-BASIC (Round One) (Getting Started, Disp)

I’ve decided to make a tutorial/guide thing for TI-BASIC (that’s the programming language on Texas Instruments calculators) because….well, I want to. Hopefully people find it useful.
Before we get started with the mechanics of TI-BASIC, it’s important to say a few general things about programming:

1. There’s four main things that happen in a program. Input is what the user puts in. Output is what the calculator spits back out.
Control flow is what the programmer uses to adjust the ‘flow’ of a program (programs are ‘read’ from top to bottom, so you need to specifically state when it shouldn’t be), and finally ‘operations’ (I just made that term up) that let you modify the input and output.
2. It’s very important to know what you want a program to do before you make it. It’s no good to say “I want it do check my answers for math class”. You need to say “I want it to ask me for two numbers, add the two numbers together, and then display the result.”
3. Thinking is required. All of it doesn’t just come naturally, sometimes you need to sit down and think out what the best way of doing something is.

So, that said, let’s start with–well, how to make and run a program on your calculator.
Turn your calculator on (a lot of this is easy.) And press the PRGM on it. If you have a TI-86/85, press F2 (Edit), and type in a new name for your program. If you’re using a TI-80/81/82/83/83+/83+SE/84+/84+SE, scroll over to the “Create New” tab, and type in a new name. (Note: I didn’t include TI-89s in the list because I don’t have one. If you send me one, I’d be more than happy to include it in this guide :) After you type in the name, and press ENTER, you’ll see a screen that says
PROGRAM:A NAME
:

Where A NAME is the name you typed in.

Pressing QUIT will exit the Programming screen, save the program, and return you to the home screen all in one.

So now that that’s taken care of, let’s write a simple program. There’s a tradition among programmers to always use “Hello World!” as a first program. So
open up a new program and call it HELLO. Now let’s take a moment and think about this. You want the program to say “Hello World!” when it’s run. That’s output. There’s no input necessary. You don’t need to control the flow, because the calculator doesn’t need to react differently to anything, and finally, since there’s no input, no operations need to take place.

So press the PRGM button again. You should see a menu labelled Ctl, that has stuff like If, While, For, End, etc. Scroll over one, to the I/O menu, and select the Disp option. Disp stands for “Display”, and is the command to display something on the screen. After the Disp, type in: “Hello World!”. Now your calculator knows to display something, and it knows the thing to display is ‘Hello World!’.


PRGM: HELLO :D isp “HELLO WORLD!”

Run the program by quitting, hitting PRGM, and selecting ‘HELLO’. This’ll paste something onto your home screen. Press ENTER to run the program.


prgm_HELLO
HELLO WORLD!
Done

Round two: Basic BASIC input and math.

Share/Save/Bookmark

Leave a Comment