Creating and Naming Variables

Introduction

Welcome to Storing Data in Python Variables, the second course in your Python journey. You are just getting started here in Unit 1 of 4, and it is great to have you along.

So far, every value you wanted to display had to be typed directly inside print(). That works for one-off messages, but it quickly becomes painful when the same value shows up in many places or when you want it to change later. In this unit, you will learn how to store values in named containers called variables, so you can label them, reuse them, and update them cleanly. By the end, you will be able to create variables, give them clear names, and use them confidently in your programs.

Creating a Variable with =

A variable is a name that points to a value you want to keep around. To create one, you use the assignment operator, =, which stores the value on the right into the name on the left.

# Store the string "Ada" under the name customer_name
customer_name = "Ada"

Here is the pattern to remember:

  • The left side is the name you choose (customer_name).
  • The right side is the value you want to store ("Ada").
  • The = sign means "store this value under this name", not "is equal to" in the math sense.

After this line runs, Python remembers that customer_name refers to the string "Ada". You can now write customer_name anywhere later in your code, and Python will substitute the stored value for you.

Storing Numbers in Variables

Variables are not limited to text. You can store any value type in them, including the int and float numbers you met in the previous course.

age = 30                  # int: a whole number
account_balance = 125.50  # float: a decimal number

Let's break down what is happening:

  • age = 30 stores the whole number 30 (an int) under the name age.
  • account_balance = 125.50 stores the decimal number 125.50 (a float) under the name account_balance.

Notice that you do not wrap numbers in quotes; quotes would turn them into strings, which you usually don't want when you plan to do math later. Also, the type of the variable is decided automatically by the value you assign, so you never have to declare it ahead of time.

Using Variables in print() Statements

Once a variable exists, you can pass its name (with no quotes) to print() to display the value it holds. Combining a label string with the variable using comma-separated values gives you clean, readable output.

# Print each variable next to a descriptive label
print("Customer:", customer_name)
print("Age:", age)
print("Balance:", account_balance)

Each print() call above shows two things: a fixed label in quotes and the current value stored in a variable. As you may recall from the previous course, commas inside print() automatically add a space between the parts. Running this together with the assignments from earlier gives you:

Customer: Ada
Age: 30
Balance: 125.5

Notice that 125.50 prints as 125.5: Python drops the trailing zero when displaying floats, but the stored value itself is unchanged.

Reusing a Variable Multiple Times

One of the biggest wins with variables is reuse. Once a name holds a value, you can refer to it as many times as you like, in any statement that follows.

customer_name = "Ada"

# Same variable used in two different messages
print("Customer:", customer_name)
print("Welcome back,", customer_name)

Here, customer_name is written once and then used twice. If you later decide to change the customer to "Bob", you only edit the single assignment line, and every print() that uses customer_name will automatically show the new value. That is much safer than searching through your code for every place you typed "Ada" by hand.

Rules for Valid Variable Names

Python is flexible about names, but it does enforce a few rules. In this course, we will stick to beginner-friendly names that:

  • Start with an English letter (a to z, A to Z) or an underscore (_).
  • Can contain English letters, digits (0 to 9), and underscores after the first character.
  • Cannot contain spaces or special characters like -, !, @, or $.
  • Cannot be a Python keyword (reserved words such as if, for, class, or True).
  • Should not be a built-in name like print; Python allows this, but it hides the built-in print() function and can cause confusing errors later.
  • Is case-sensitive, so Age and age are treated as two different variables.

Quick examples: age, customer_name, _total, and item2 are all valid. On the other hand, 2nd_item (starts with a digit), customer name (contains a space), and account-balance (contains a hyphen) would cause Python to raise a SyntaxError.

Choosing Clear, Descriptive Names

Following the rules is only half the job. The other half is picking names that a human reader can understand at a glance. Compare these two lines:

x = 125.50               # What does x mean?
account_balance = 125.50 # Clear at a glance

Python programmers follow a convention called snake_case: all lowercase letters, with underscores between words (customer_name, account_balance, total_price). Short throwaway names like x or n are fine in tiny math snippets, but in real programs, descriptive names save you from constantly re-reading code to remember what each value represents. A good rule of thumb: If someone new opened your file, would the variable names alone tell them the story?

Conclusion and Next Steps

In this lesson, you learned how to create a variable with =, store both text and numeric values inside one, print those values by name, and reference the same variable across multiple statements. You also saw Python's rules for valid names and the snake_case convention that keeps code readable as it grows. Together, these ideas turn variables into a reliable way to label, reuse, and update the data your programs work with.

Up next, it is your turn to put these ideas into practice with a set of hands-on exercises in the CodeSignal IDE. Work through them at your own pace, and don't hesitate to look back at this lesson whenever you need a quick refresher. See you in the practice section!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal