HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

6) Variables and Types Lesson

Python Variable Assignment & Naming Rules

9 min to complete · By Martin Breuss

After getting a high-level idea of what a variable is in programming, you'll dig a little deeper and learn about:

  • How you can assign variables in Python
  • How to speak about what you are doing when you do that
  • What naming rules and conventions to follow

This lesson will cover the most important concepts about Python variables and variable assignment.

Variables and Variable Assignment

On a more technical level, you can think of variables as reserved memory locations in your computer that store values. When a variable is assigned to a value, space in memory gets reserved for that value.

How to Store a Value

To store a value, you use the assignment operator (=):

number = 5

Using proper programming speech to describe this process, you would say:

I am assigning the value 5 to the variable number

By assigning a value to the variable number, you can later retrieve the value by using the variable's name, number. Variable names are a combination of characters---and there is a common naming convention for Python that you're encouraged to follow!

Variable Naming Rules

Some of the naming rules and conventions are really just conventions that are here to make it easier to communicate your code and read your own and others' code in the future. There are some rules, however, that are required for your code to run properly.

Following these rules when naming your variables is necessary to build a functional Python script:

Start With a Character

Your variable names can include numbers, but they can't start with a number.

Don't Use Spaces

A variable name can't include any whitespace. It has to be one consecutive word consisting only of characters, numbers, and underscores.

Avoid Reserved Keywords

There are some words that have a specific meaning in Python. You therefore can't use them as variable names. You will learn more about them in an upcoming lesson.

If you break any of these rules, Python will let you know immediately with a SyntaxError.

Variables Naming Conventions

Following these conventions is best practice and will make your code easier to read, understand, and debug. Breaking these conventions won't break your code, but you should still try to stick to them as much as possible:

Use Lowercase

Python variable names should be written in lowercase. It has a specific meaning if a variable name is not written in lowercase. Therefore, unless you know exactly why you need to, you should always use lowercase for your variable names.

Use Snake Case

When you use longer variable names that consist of multiple words, you should separate the words with underscores (_). This helps to improve the legibility of your code. A valid snake-case variable name could look like this: snake_case_variable.

Use Descriptive Names

Always try to name your variables as descriptively as possible. Your code will be much easier to understand. Code you find online will often include one-letter variables such as x. There are some situations where this makes sense, but in general, it pays off to be detailed and descriptive with your variable names.

Following these guidelines and requirements right from the beginning will help you build good habits when writing Python code. This makes reading and maintaining your scripts so much easier. Your collaborators will like you more, and also your future self will be thankful if you follow these guidelines.

Colorful illustration of a light bulb

Info: There's more to proper naming. If you want to dive deeper, check out Python's PEP-8 naming conventions.

What you have just read about represents the most common way of assigning values to variables in Python. With this knowledge screwed tightly into your mind---you're set! There is, however, another way of assigning values to variables that can sometimes speed up your development a tiny bit.

Multiple Variable Assignments

Python has a trick that allows you to assign multiple variables in one go. It works almost the same as the standard variable assignment you used above:

start, end = 0, 100

This line of code assigns the variable 0 to start and the value 100 to end.

Only When Necessary

While using multiple variable assignments is okay in the example above, there's a fine line between when it's useful and when it makes your code harder to read.

Generally, you should only use it for data that is closely related, such as a start and end value of a progress bar for example.

You should not use multiple assignments if the data you're assigning isn't closely related, for example:

counter, price = 0, 123.45

In this code snippet, you assigned the value 0 to counter and 123.45 to price. But it's unclear whether these two variables have anything to do with each other; they're not inherently related. Also, the values are more complex than in the previous example.

To keep your code as clear as possible, it's better to use two separate lines in this situation. Doing so makes your code better readable and easier to understand:

counter = 0
price = 123.45

Readability is everything when writing code. Always optimize your code to make it easier to understand rather than using as few lines or characters as possible.

Summary: Assigning Variables in Python

  • Variables are reserved memory locations on your computer
  • Variables can be assigned values of almost any type
  • Space in the computer memory is reserved for the value of a variable

Variable Naming Rules

  • Start with a character
  • Don't use spaces
  • Avoid reserved keywords

Variable Naming Conventions

  • Use lower case
  • Use snake case
  • Use descriptive names