Python Variables

Variables are a major part of computer programming. Variables allow you to store a value that you can use later in the program. You can therefore make the program do different things, depending on the value of the variable.

Variable Assignment

Variables are assigned with the assignment operator (=). Like this:

In this example, we created three variables and named them a, b, and c.

We assigned 1 to the first variable, 2 to second, and Hey to the third.

In Python you don't need to declare the variable before assigning a value to it. You also don't need to declare its data type. This is in contrast to some other languages (such as Java, C, C++, etc), where you must declare the variable first and explicitly state its data type.

Multiple Assignments

In Python, you can assign multiple variables within a single statement.

So we could take the previous example and change it to this:

If you need to assign the same value to multiple variables, you can do this:

Output a Variable

Variables can be used in many ways within a program. One of the most basic things you can do with a variable is output its value to the screen. In Python you can do this with the print() function.

Open your Python editor and enter this:

This outputs the following:

Result
Earth

Here's what we did.

  1. We created a variable named planet. We set the value of this variable to Earth.
  2. We then called the print() function and passed the planet variable to it.

The print() function prints out the value of its argument. So in this case, the argument was the planet variable, and its value was Earth. So the result is that the program prints out the string Earth.

You can output multiple values with the print() function. For example, you can do this:

Result
Earth Australia

You can also combine the variables with other text. Like this:

Result
My name is Homer and I live on planet Earth

Identifier Naming Rules

A variable name is an identifier. In Python, an identifier can be a combination of letters in lowercase (a to z), uppercase (A to Z), digits (0 to 9), and the underscore (_) character.

Valid Examples

So the following variable assignments are all valid:

However, there are some things you can't do with your identifiers.

❌ Must Not Begin with a Number

You can't start the identifier with a number.

❌ Must Not use a Python Keyword

You can't use any Python keywords as your identifier name. Here are the Python keywords:

Case Sensitivity

In Python, variable names (as with all identifiers) are case sensitive. Therefore, the following are two different variables:

So printing these two variables will result in two different values being displayed:

Result
Earth Jupiter

How to Determine a Variable's Type

You can use the type() function to find out what type a variable is. You can also pass the type() function to a print() function to print the result to the screen.

So if we do this:

The output is this:

Result
<type 'str'>
<type 'int'>

This tells us that the first variable is a string and the second one is an integer.