Blog Post #8: What’s in a Name? An Introduction to Variables

So far, we’ve learned how to make Python display text directly with print("Hello, World!") in Post #5 and how to leave notes for ourselves with comments in Post #7. But to write truly useful programs, we need a way to store, label, and work with data. This is where variables come in.

In this post, you will learn about one of the most essential building blocks of programming: variables. We’ll cover what they are, how to create them, and the important rules and conventions for naming them.

What is a Variable?

The easiest way to think of a variable is as a labeled box for storing data. You can put some information inside the box, and the label on the outside allows you to find, use, and even change that information later.

  • The variable name is the label on the box.
  • The value is the data you store inside the box.

Variables are fundamental because they let our programs be dynamic. They hold information that can change, or vary, while the program is running, like a user’s name, a player’s score in a game, or the current temperature.

Creating and Using Variables

Creating a variable in Python is simple. You use a single equals sign (=), which is known as the assignment operator. This operator “assigns” the value on its right to the variable name on its left.

The syntax is: variable_name = value

Let’s look at a couple of examples.

# Storing a number in a variable named 'player_score'
player_score = 100

# Storing text (a string) in a variable named 'player_name'
player_name = "Alex"

Once you’ve stored data in a variable, you can use its name to access the data. For example, we can print the values using the print() function.

print(player_score)
print(player_name)

Important: Notice we don’t use quotes around the variable names in the print() function. print(player_name) prints the value stored inside the variable (“Alex”), whereas print("player_name") would print the literal text “player_name”.

The value of a variable can be updated by assigning a new value to it.

# The player starts with a score of 100
player_score = 100
print(player_score)  # Output: 100

# Later in the game, the player scores more points!
player_score = 150
print(player_score)  # Output: 150

The Rules of Naming

Python has a few strict rules you must follow when naming your variables. If you break these rules, your program will produce an error.

  1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
  2. Variable names cannot start with a number.
  3. The rest of the name can only contain letters, numbers, and underscores. No special characters or spaces are allowed (e.g., !, @, -, $).
  4. As we learned in Post #6, variable names are case-sensitive. This means score, Score, and SCORE are three different variables.

Here are some examples:

  • Valid Names: my_score, user1, _internal_data, name_of_player
  • Invalid Names: 1st_place (starts with a number), user-name (contains a hyphen), my score (contains a space)

Naming Conventions: Writing Pythonic Names

Following the rules above will prevent errors. Following conventions will make your code clean, readable, and “Pythonic”—a term for code that follows the community’s best practices.

The standard convention for variable names in Python is called snake_case.

  • All letters are lowercase.
  • Words are separated by underscores.

For example: first_name, items_in_cart, has_completed_level.

Furthermore, always use descriptive names. A variable’s name should tell you what it does. x = "John" is valid, but it’s bad practice. customer_name = "John" is much better because it’s self-documenting. Good names mean you have to write fewer comments.

What’s Next?

You now understand the fundamentals of variables: they are named containers for data, created with the = operator, and must follow specific naming rules, with snake_case being the Pythonic standard.

In our examples, we stored different kinds of data—numbers (100) and text ("Alex"). These are known as different data types. In Post #9, we will begin exploring Python’s built-in data types, starting with the two most common types of numbers: integers and floats.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment