Variables in PY Games

Updated: February 17, 2022
No Credit Card Required


You may have covered Variables in your math class, but well, they're a little bit different in Python. Variables are location inside the computer's memory that is dedicated to storing specific data values. They are an important tool in keeping track of data like names, numbers, Lists, and whether something is true or false.

A Variable is created the moment you first assign a value to it. Below is an example of how to create a Variable. “My String” is a String, “num” is a number.

Here's some Variables that are properly named. And here are some Variables that are not properly named. Hmm, why are these incorrect? Variable names can be short, like x or y, and descriptive like speed and max speed. But Variables are Case sensitive. So lowerCase x and upperCase X are different Variables. This is actually pretty important. So make a note.

To change the value of a Variable just assign a new value to it. Here's a great shortcut. Let's say we want to create many Variables each with the same value. Instead of creating a new Variable, every line, multiple Variables can be assigned to the same value in one line of code.

Variables with more than one word are written in one of three ways called Cases for clarity. These are camel Case, Pascal Case and snake Case. Camel Case begins the Variable in lowerCase for the first word, then you capitalize subsequent words. Pascal Case capitalizes the first letter of each word, and Snake Case capitalizes the first word the snake's head with the following words in lowerCase separated by underscores expert tip, choose the convention that you like and then stick with it. My personal favorite is camel Case. Don't use multiple naming conventions though.

Okay, now let's look at datatypes. The term data types refers to the different types of data our programs can use or store. There are five principal data types that you'll be working with Integers, like 1234, Strings, like “apples” or “red”, there are numbers with decimal places like 1.002 and Lists, like January, February, March, and booleans. True or false.

Let's learn about the different data types. Starting with Integers. You probably remember that an integer is a whole number positive or negative like -32,0, 2361. All of these are examples of Integers. So the integer datatype that stores whole numbers is labeled INT. In this example, num is a Python Variable with a value of zero. Strings are the second data type that we work with. Strings are used to display text, including a word, a sentence or even an entire paragraph. They are grouping of characters inside quotation marks. Examples of Strings are email ids, names, favorite meals, etc, etc. The String datatype is str. Now numbers with decimal places or non Integers are represented by our third data type. This is called the Float type and is used to denote numbers with decimal places. You can use them to denote data like temperature, weight percentages, or mathematical constants like Pi, that is 3.14159, etc, etc. Pi continues forever. In Python, whenever you divide one number with another, the output is a Float. Lists are a data type that we use to order a sequence of elements such as names or numbers. For example, the months of the year, January, February, March, etc make a list.

Finally, we have a data type called Boolean. It's also known as bool. In Python, there are two boolean values true and false. And as their name suggests, they denote true and false values. The Boolean datatype might seem kind of weird, but it's very useful as it helps us identify if certain conditions are met or not. We'll use them a lot later.

In Python, there's a handy mechanism with which we can find the datatype of a Variable or value. It's called a type function. When we provide the value or Variable to the type function inside its parentheses We get the name of its datatype. We can also print this name using print. As an example, print the data type of

String “I like summer”. Using the type function we get class STR as an output, meaning the value is of type String. When we do the same with a Variable called ‘user number’ that holds 14.54 as its value, you get class Float as output. It means the user number Variable is of Float datatype.

Awesome. For now, we have reached the end of this lesson. Remember, the more you practice, the better you get. I'll see you in the next lesson.