Dungeon Coder

Chests and the Treasures Therein

An Introduction to Variables

Every good adventure involves treasure-hunting. Whilst dungeon crawling, it might be your good fortune to happen upon a chest or two filled with things of great value.

The adventure known as programming is no different! The chests we happen across (or create ourselves) are called variables. The treasures found inside are known as values.

You may have already come across the term "variable" in some other course like mathematics. This is the very same concept. In math, you may have seen something like "x/2 = 4", which is asking, "What divided by two gives you four?" The answer, of course, is 8.

In the following repl widget, you'll find some python code in which I've created some variables, and used that print() function from the Hello, Dungeon! lesson to put those variables (treasure chests) to use.

As in the last lesson, press the "play" button to test out the code. Then, feel free to try out the exercises mentioned afterward. Remember, if you are a student of mine, or a Patreon subscriber, you will be able to submit your responses to those exercises to me directly. I will review those submissions and provide you with feedback.

See how the creation of variables in python is just like when you create variables in math! You simply come up with a name for the variable, put an = sign, and put whatever value you want stored in the variable to the right of the = sign. In the above examples, then, heroName is a variable in which is stored the value "Pat". Likewise, villainName is a variable in which is stored the value "Alex". See how there are quotation marks around the values? That's only necessary in this case because those values are what are called strings. Strings are a type of data that can be stored in a variable. Other types of data (or "data types") that can be stored in a variable are integers, characters, boolean values, doubles, and floats. While you might be able to guess what some of the data types look like, we will cover such data types throughout coming lessons.

Notice the "+" sign in between "Our hero's name is " and heroName. This + sign is not exactly the same as your standard mathematics addition operator. In python, when using the print() function to output strings (not numbers) to standard output, the + sign is known as a concatenation operator. Concatenation is just a fancy word for "stick these two things together." If you press the play button, and analyze the output, you'll get the gist of what the + sign does in this case.


  1. Add another line of code inside the coding area of the repl widget. This line of code should create a variable called heroAge. You should place a numerical value inside the heroAge variable. Since we are specifically going to be using a numeric data type, you should not place the value inside quotation marks. If you need help, feel free to see the video below where I give some examples.
  2. Add another line of code, this time a print() statement that prints something like "Our hero's age is " along with the contents that are stored inside the heroAge variable. Remember to use the + concatenation operator. Try to run the program. What happened?
  3. The error from the previous question is due to the fact that we tried to use the + operator on two different data types. This isn't allowed. We will cover the resolution to this problem over the course of this...course. Of course, you may feel free to skip ahead by Googling something like "casting int into string python print". Did you find the solution? What is it?
  4. What happens if you replace the + sign with a , instead? (that's a comma)
  5. Finally, add a print statement that prints something like "Our hero's name is " along with the contents of heroName AND ", and their age is " AND the contents of heroAge.

Join the Dungeon Crawl: A place for programmers to ponder, partake, and peruse postulations pertaining to programming, politics, potions, and pizza.

If you subscribe on Patreon, you will be granted access to the Dojo, a growing collection of quality computer science classes created and actively mantained and frequented by the Dungeon Master!

Become a Patron!


Contact: dungeon_master@dungeoncoder.com