Variables

A variable is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type. Every variable has a name which can consist of letters, numbers, and the underscore character _.

The equal sign = is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed.

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, grade, grocery_list).

Rules for Python variables:

  • A variable name must start with a letter or the underscore character. It cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
  • Variable names are case-sensitive (num, Num, and NUM are three different variables).

Examples

These are all valid variable names and assignment:

user_name = "@sonny420"
user_id = 100
verified = False

A variable’s value can be changed after assignment

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy

Contributors