Programming the Hokeydex (Intro to Python Variables and Data Types)

The following post is a simple introduction to variables and the sorts of things commonly assigned to them in Python. If you are new to Python and/or programming, I encourage you to run each code block in your own editor as you read (repl.it is great for quickly running a little code). Think up questions like “What would happen if I put an integer on the left side of the equals sign and the variable name on the right?”, and then try to answer them by playing around with the code and Googling anything you need extra help understanding. The building of a working Hokeydex complete with GUI and relational database is left as an exercise for the ambitious reader and hokeymon enthusiast.

Professor Woke enters the laboratory.

Woke: Is the Hokeydex finished? Bash and Jerry each need one, and as you know they leave tomorrow morning on their quest to catch all the hokeymon.

Aide: Sorry Professor, I’m still working out some of the bugs in the software.

Woke: Alright, well, let’s see what you’ve got so far. For example, assuming hokeymon have been seen in the following order:

  1. Myrtle
  2. Charmanderp
  3. Bulbasnore
  4. Ninefails
  5. Machump
  6. Bulbasnore
  7. Ninefails

…what does the Hokeydex do?

## Data Type: Boolean
have_hokeymon_been_seen = True
print(type(have_hokeymon_been_seen))
print(have_hokeymon_been_seen)
<class 'bool'>
True

Woke: So all it does is tell us whether or not any hokeymon have been seen?

Aide: Uh…well, yes. Isn’t that enough, Professor?

Woke: Sigh, no, not even close. At the very least it needs to tell us how many hokeymon we’ve seen.

Aide: Oh, that’s a great idea Professor! How about something like this?

## Data Type: Integer
how_many_hokeymon = 7
print(type(how_many_hokeymon))
print(how_many_hokeymon)
<class 'int'>
7

Woke: But I feel like that number is too round. What if we also see just half of a Dewrong? Maybe half of it got eaten by a Drawini, and we found the other half washed up on shore. Now what?

## Data Type: Float
how_many_hokeymon = 7.5
print(type(how_many_hokeymon))
print(how_many_hokeymon)
<class 'float'>
7.5

Woke: That’s an improvement. But honestly, now that I think about it, we’re going about this all wrong. What we really need is for the Hokeydex to actually record the names of the hokeymon it sees, not just how many.

## Data Type: String
hokeymon = "Myrtle Charmanderp Bulbasnore Ninefails Machump Bulbasnore Ninefails"
print(type(hokeymon))
print(hokeymon)
<class 'str'>
Myrtle Charmanderp Bulbasnore Ninefails Machump Bulbasnore Ninefails

Woke: Hmm…better, but we need the names to be separated somehow.

## variable assignments on individual lines
hokeymon1 = "Myrtle"
hokeymon2 = "Charmanderp"
hokeymon3 = "Bulbasnore"
hokeymon4 = "Ninefails"
hokeymon5 = "Machump"
hokeymon6 = "Bulbasnore"
hokeymon7 = "Ninefails"

Woke: Eh, too many lines of code. Can’t you somehow condense that into one line?

## multiple variable assignments on one line
h1, h2, h3, h4, h5, h6, h7 = "Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"

Woke: Warmer, definitely getting warmer. But a separate variable for each observed hokeymon seems a little inconvenient, doesn’t it?

Aide: Oh, I think I have it!

## Data Type: List
hokeymon = ["Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"]
print(type(hokeymon))
print(hokeymon)
<class 'list'>
['Myrtle', 'Charmanderp', 'Bulbasnore', 'Ninefails', 'Machump', 'Bulbasnore', 'Ninefails']

Woke: Much better! Now, say we want a version of this list that can’t be altered, just to make sure no careless changes corrupt our precious data. Is there a way to do that?

## Data Type: Tuple
hokeymon = ("Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails")
print(type(hokeymon))
print(hokeymon)
<class 'tuple'>
('Myrtle', 'Charmanderp', 'Bulbasnore', 'Ninefails', 'Machump', 'Bulbasnore', 'Ninefails')

Woke: Very nice. But you know what, it looks like the duplicates are still there. The most important thing is to record the types of hokeymon we’ve seen. Recording every individual observed hokeymon one by one could get unorganized fairly quickly. Can we somehow get rid of the duplicates?

## Data Type: Set
hokeymon = {"Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"}
print(type(hokeymon))
print(hokeymon)
<class 'set'>
{'Charmanderp', 'Bulbasnore', 'Machump', 'Myrtle', 'Ninefails'}

Woke: Beautiful! And say again that we want a version of the hokeymon set that can’t be accidentally changed.

## Data Type: Frozenset
hokeymon = frozenset(["Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"])
print(type(hokeymon))
print(hokeymon)
<class 'frozenset'>
frozenset({'Charmanderp', 'Machump', 'Bulbasnore', 'Ninefails', 'Myrtle'})

Woke: I feel like we are on the verge of greatness!

Aide: Thank you Professor, this is certainly easier with your help.

Woke: Finally, let’s say we want to somehow record how many times we’ve seen a hokeymon. Is there a convenient way that can be done? To store the number of appearances alongside the name?

## Data Type: Dictionary
hokeymon = {"Myrtle": 1, "Charmanderp": 1, "Bulbasnore": 2, "Ninefails": 2, "Machump": 1}
print(type(hokeymon))
print(hokeymon)
<class 'dict'>
{'Charmanderp': 1, 'Bulbasnore': 2, 'Ninefails': 2, 'Machump': 1, 'Myrtle': 1}

Woke: Bingo! Okay, I think that’s a wrap. Bash and Jerry should be very pleased with their new Hokeydexes. And just think of all the experiments we can do once they bring back their samples. A few months of intensive hokeymon testing, and my cosmetics company will be up and running in no time!

Aide: Wow, I feel so accomplished!

Woke: You certainly should. Don’t dawdle though, we need this Hokeydex software fully functioning by morning. Go ahead and polish off the details tonight.

Aide: Uhh…well Professor, all we’ve really done so far is just assign instances of different data types to variables. It’s not quite a functioning application yet. I could really use some help with…

Woke: I’m sure you’ll figure it out. You have all night for goodness’ sake. See you bright and early!

Professor Woke exits the laboratory.

Aide: Gulp