Flat_Eric Contemplating variables

Variables

I am whatever you say I am

What are Variables?

Think of a variable like a labeled box. In programming, we use these "boxes" to store information that we want to use or remember later in our code. This could be a number, a piece of text, or even the result of a calculation.

A great example of this is user input. If you ask a user for their name, you need a place to "hold" that name so you can greet them later.

How it looks in code:

To create a variable, we give it a name, use the equals sign (=), and then give it a value.


user_name = input("What is your name?")
print("Hello, " + user_name)
    

Expected output


What is your name?Fred
Hello, Fred
    

How Programs Use Data

At their core, programs are quite simple: they store, retrieve, and change data.

Variables are the mechanism we use to manage this data. They allow our programs to:

Without variables, a program would have no "memory"—it would forget every piece of information as soon as it processed it!

Rules for Naming Variables

In Python, you can name your variables almost anything, but there are a few important rules and "best practices" you must follow. If you break these rules, your program won't run!

  1. The Starting Character
  2. A variable name must start with a letter or an underscore (_). It cannot start with a number.

    
    ✅ user_name = 25
    ❌ 1st_place = "gold" # this will cause an error
        
  3. Letters, Numbers, and Underscores
  4. After the first character, you can use letters, numbers, and underscores. You cannot use spaces or special symbols (like !, @, #, or $).

    
    ✅ player_score_1 = 100
    ❌ player score = 100 # (Spaces are not allowed)
        
  5. Case Sensitivity
  6. Python is case-sensitive. This means Username, username, and USER_NAME are three completely different variables.

    • age = 20
    • Age = 30
    • (In Python's eyes, these are two different boxes!)
  7. Reserved Words
  8. You cannot use words that Python already uses for its own instructions (these are called Keywords). For example, you can't name a variable print or if.

  9. "Snake Case"
  10. In Python, the most common way to name variables with multiple words is to use snake_case . This means using all lowercase letters and separating words with an underscore.

    Good: total_price, is_logged_in, favorite_color

The Underscore Prefix

Sometimes, you might see a variable name that starts with a single underscore, like this:

_my_dog = "fat"

In Python, starting a variable name with an underscore is a special signal to other programmers. It indicates that the variable is intended to be "private." This means the variable is meant for internal use within a specific part of the code and shouldn't be messed with from the outside. Don't worry about the technical details of "private" data yet—we will dive much deeper into this in a later lesson! For now, just remember that the underscore acts like a "Do Not Disturb" sign.

Test Your Knowledge

Select the correct answer below to see if you've mastered Python Variables.

Loading quiz...

Practice Naming Variables

Time to Experiment!

Open VS Code and create a new file named variables_practice.py in your part7 folder. For each exercise below, write the code and use comments (#) to record your observations.

Exercise 1: The Case-Sensitive Trap

Create a variable called user_score and set it to 100. On the next line, try to print the variable using a different casing, such as print(User_Score).

The Task: Run the code and look at your terminal. In a comment in your code, write down the specific name of the error that Python gives you when it can't find a variable due to a case mismatch.

Exercise 2: Illegal Names Hunt

Try to create three variables that intentionally break the naming rules: one starting with a number, one containing a space, and one using a special symbol (like $ or @).

The Task: Run your code after each attempt. In your script, use a comment to note whether Python gives you the same error type for all three, or if the errors are different.

Exercise 3: The "Snake Case" Reformat

In the professional world, consistency is key. You have been handed these "badly named" variables:

The Task: Rewrite these three variables in your script using the proper snake_case convention. Use print() to output each one to ensure your new names are valid and the script runs without errors.

Exercise 4: The Variable Update

Variables are dynamic! Create a variable called wallet and set it to 50. On the next line, "update" it by writing: wallet = wallet + 20.

The Task: Print the wallet variable to see the new total. Below that line, add a comment explaining in your own words how Python used the "old" value of the box to calculate the "new" value.

Exercise 5: Keywords & Syntax Highlighting

Python reserves certain words for its own use. Attempt to create a variable named pass = True or class = "Python".

The Task: Look up a list of Python Keywords. Pick three keywords you haven't used yet and try to use them as variable names in VS Code. In a comment, describe how the color of the text changed to warn you that these are special reserved words.

Click on the GitHub cat, to open github desktop.

Don't Forget to commit and Push!

Advanced Naming Conventions

There are a couple more things to keep in mind when naming your variables as you progress. First, you might see names that use a double underscore (like: __init__). While it might look like a single long line, it is actually two underscores, and in Python, this has a distinct meaning. These are often called "Dunder" (Double-Under) variables, and they are reserved for special internal functions that we will cover in a later module.

Additionally, you may encounter variables written in ALL CAPS, such as MAX_SPEED = 100. In the programming world, this is a signal that the variable is a Constant—a value that is set once and is not intended to be changed while the program is running.

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course