Variables
Published May 6, 2021Updated Sep 9, 2021
Contribute to Docs
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
, andNUM
are three different variables).
Examples
These are all valid variable names and assignment:
user_name = "@sonny420"user_id = 100verified = False
A variable’s value can be changed after assignment
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours