HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

6) Variables and Types Lesson

Python Variables

6 min to complete · By Martin Breuss

In this lesson, you'll learn about what a variable is in programming, and in this case, Python.

Colorful illustration of a light bulb

Info: To be able to talk about anything precisely, you need to refer to it with a name. That reference is what a variable is in programming. It is a name for a value.

Python Variables Example

Here comes a weird example. Start off by picturing some kind of writing table:

Writing table on Wikipedia. Writing table (Bureau plat), about 1750, Gaspard Feilt (designer and maker), France V&A Museum no. 1052:1 to 5-1882 Gaspard Feilt, who made this table, was a native of Germany, although he worked in Paris. Many Parisian cabinet-makers came from Germany. CC BY-SA 3.0, https://en.wikipedia.org/w/index.php?curid

Photo by by Dominikmatus https://en.wikipedia.org/wiki/User:Dominikmatus

Provide a Name

Next, go ahead and give a name to this table. For example, you could call it "Grandpa". After your conversation partner understood that Grandpa is your name for this writing table, you can now go ahead and say:

Please put the papers over there onto Grandpa.

Or you could say:

Looks like Grandpa has gotten messy. Can you please clean Grandpa?

Even if that sounds pretty weird, anyone who is in on the joke would understand such a request and know how to correctly interpret it. The situation works out because they know that when you say "Grandpa" you are referencing the writing table that you showed them before. By showing the picture and giving it a name, you established a reference:

Grandpa Table Assignment. Edited from: https://en.wikipedia.org/w/index.php?

Name to Value

That process of pointing a name to a value is essentially what variables are in programming. Threading this weird example back into programming terms, you could say:

  • Grandpa is your variable
  • The table is your value

Looking at it in Python code, establishing the connection between the two, also called the variable assignment, goes as follows:

grandpa = "table"
Colorful illustration of a light bulb

Note: This is a bad example of variable naming in programming. In general, you'll want to be as descriptive as possible to avoid confusion when you read over your code at a later time. A much better variable name for this table would therefore be table.

After executing the line of code shown above, you can now reference the value of your variable throughout your program. Go ahead and recreate the requests from further up, this time using Python code and a reference to the table:

print("Please put the papers over there onto " + grandpa)

If you correctly assigned the variable grandpa to the string "table", then running this line of code will output:

# OUTPUT
Please put the papers over there onto table

Alright, it's not the best English sentence you ever wrote. But you successfully assigned a value to a variable and referenced that value through the variable using Python code. Nice work!

Define Variable

Variables are references that point to values or names of values, depending on how you want to look at them.

Remember that naming your variables descriptively is a practice you should try to get used to right from the beginning. Many programmers keep struggling with descriptive variable naming throughout their careers. In fact, it's so common that there are a bunch of jokes related to variable naming, for example:

There are only 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

So keep in mind to never name your Table object Grandpa, and once that's anchored in your brain, you can move on to the next lesson where you'll learn more about how to handle variables in Python.

Summary: Python Variables

  • Variables are references or names of values that we assign to them
  • Having clear, easy-to-understand variables is incredibly important in writing consistent code
  • Variables should be descriptively named