Start learning free

WELCOME TO PYTHON

Accessing Variables in Python

Lesson Read the full explanation

TL ; DR:

  • After creating a variable, you can access the value in the variable, to print it for example:

    # Creation:
    car = "Toyota"
    
    # Access:
    print(car) # Prints "Toyota"


Full lesson:

Accessing values in variables:

Now we can use this label anywhere in our program to access the value in that box. We can print it for example:

# We create two variables:
name = "AlgoCademy"
age = 10

# We access the variables:
print(name)
print(age)

The output of this code is:

AlgoCademy
10

Assignment
Follow the Coding Tutorial and let's create some variables!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · WELCOME TO PYTHON

    Access Variables in Python, explained

    Python Variables: How to Store and Use Data in Your Programs

    Every useful program needs to remember things — a player's score, a customer's name, the price of an item, whether a user is logged in. Variables are how Python stores these values so your code can work with them. Think of a variable as a labeled container: you put a value in, give it a name, and retrieve it by that name whenever you need it.

    Variables are the foundation of every program you'll ever write. Once you understand how to create and access them, you can start building calculators, games, data tools, and web apps.

    How Accessing a Variable Works

    Once you create a variable, you use its name anywhere you'd use the value itself. Python looks up the stored value at runtime and substitutes it in:

    # Create variables:
    username = "Alex"
    score = 42
    
    # Access them — Python replaces the name with its value:
    print(username)   # Alex
    print(score)      # 42
    print("Score:", score + 10)  # Score: 52
    

    You can read a variable as many times as you like. Reading doesn't change the value — only assigning with = updates it.

    Updating a Variable's Value

    Variables aren't fixed — you can reassign them at any point. Python always uses the most recent value:

    lives = 3
    print(lives)   # 3
    
    lives = 2      # reassign
    print(lives)   # 2
    
    lives = lives - 1   # use the current value to compute a new one
    print(lives)   # 1
    

    Real-World Example: A Simple User Profile

    Variables shine when you need to store related pieces of information and use them together:

    first_name = "Maria"
    last_name = "Santos"
    age = 28
    is_premium = True
    
    full_name = first_name + " " + last_name
    
    print("Welcome,", full_name)         # Welcome, Maria Santos
    print("Age:", age)                    # Age: 28
    print("Premium member:", is_premium)  # Premium member: True
    
    # Calculate birth year
    birth_year = 2024 - age
    print("Born around:", birth_year)     # Born around: 1996
    

    Variable Naming Rules and Best Practices

    Good names make your code read almost like English. Follow these rules:

    • Names can contain letters, digits, and underscores — but cannot start with a digit.
    • Names are case-sensitive: score and Score are two different variables.
    • Use snake_case for multi-word names: player_name, high_score, is_game_over.
    • Make names descriptive: item_price is clearer than x or p.
    # Clear, descriptive names:
    player_name = "Jordan"
    high_score = 9500
    is_game_over = False
    
    # Avoid single-letter or cryptic names in real code:
    # n = "Jordan"   # What is n? Unclear.
    # hs = 9500      # What is hs? Hard to read.
    

    Common Mistakes to Avoid

    • Using a variable before creating it: Python raises a NameError. Always assign a value before you try to read it.
    • Typos in variable names: usernme and username are different names. Python won't auto-correct — it raises a NameError at runtime.
    • Accidental reassignment: If you reuse a variable name for a different purpose, the old value is gone. Use distinct names for distinct values.
    # NameError: using before creating
    # print(player)  # NameError: name 'player' is not defined
    
    player = "Sam"
    print(player)  # Sam — works correctly
    
    # Accidental overwrite:
    result = 10
    result = "hello"   # previous value (10) is lost
    print(result)      # hello
    

    Key Takeaways

    • Create a variable with name = value — use it anywhere by writing the name.
    • Python substitutes the stored value wherever the variable name appears.
    • Variables can be reassigned; Python always uses the latest value.
    • Use snake_case, keep names descriptive, and always initialize before reading.

    What to Learn Next

    Now that you can store and retrieve values, explore Python data types to understand what kinds of values variables can hold — integers, floats, strings, and booleans. Then try arithmetic operators to start transforming those values into something useful.

    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free