Course contentsShow
← Python
Lesson 11 of 1,0601. Foundations and EnvironmentFree lesson

Declaring Variables and Assigning Values

You will learn how to store data in named variables using the assignment operator to manage information throughout your program's execution.

Storing Information with Variables

In this lesson, you will learn how to save pieces of data into named containers called variables so you can easily reference or update that information later in your program.

The Power of Assignment

Think of a variable as a labeled box. When you want to store data, you give the box a name and place the information inside using the assignment operator, which is the equals sign (=).

Unlike some other programming languages, Python does not require you to declare a specific "type" for your variable beforehand. You simply pick a name and assign a value to it.

# Here, 'user_score' is the name of our variable
# We are assigning the value 10 to it
user_score = 10

# We can also store text (strings)
player_name = "Alex"

Once you have performed the assignment, your program remembers these values. You can use the variable name anywhere you would normally use the data itself. If you decide to change the value later, you simply assign a new value to the same name:

# Updating the variable with a new value
user_score = 15

Best Practices for Naming

When choosing names for your variables, clarity is key. It is standard practice in Python to use snake_case—this means using lowercase letters and separating words with underscores (e.g., total_items instead of TotalItems or totalitems).

When your program executes, Python keeps track of these names and their associated values in memory. This allows your script to "remember" inputs, track calculations, and manage the state of your application as it runs from the first line of code to the last. Always ensure your variable names describe exactly what is being stored so that your code remains readable to you and your teammates.

Key Takeaway: A variable is a named reference that stores a value, created by using the = operator to assign data to a label.