Think of a variable as a name attached to a particular data object. In Python, you don't need to declare your variable or the type of data you'd like it to store at the start of your program, as is the case in many other programming languages. You can create and declare variables as the need arises. To create a variable, you assign it a value and then start using it in future instructions to the computer. Assignment is done with a single equals sign (=):
x = 5 print(x)
You can ask Python report what type of data is stored to a variable by using the
a = 'hello' type(a)
b = 3.1 type(b)
type() function can be helpful to inspect what type of data you have stored to a variable if you are receiving error messages that state you are using the wrong data type.
The examples you have seen so far have used short and non-descriptive names like
b. Programming best practices will suggest use of more descriptive variable names that indicate what type of data the variable refers to.
Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (
a-z), digits (
0-9), and the underscore character (
_ ). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.
In order to make longer, multi-word variables more readable in your code, there are a variety of capitalization protocols you could use. All of these are standard, so choose which one best suits your style and use it consistently.
- Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see.
- Pascal Case: Identical to Camel Case, except the first word is also capitalized.
- Snake Case: Words are separated by underscores.
There are several (33 to be exact) variable names that are off-limits to you in Python, as they are reserved as keywords for other purposes. The list of keywords can be found by typing
help("keywords") into the command line.
Here is a list of the Python keywords. Enter any keyword to get more help. False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not
A Python variable is a symbolic name that is a reference, or pointer, to an object stored in memory of the computer. Once an object is assigned to a variable, you can refer to the object by the name of the variable, but the data itself is still contained within the object.
For example the code:
x = 5
creates an integer object with the value of
5, and then creates a reference to that object with the name
If later in your program, the code
y = x
were to be run, it would not create a new integer object with value of
5, since once already exists. Instead, Python creates a new reference with the name
y to the existing object.
This can be verified using the
id() function, which returns the unique id number assigned to every object created by Python.
If you were to assign the value of
7 to the variable
x, Python will need to create a new integer object, since there currently is not one with the needed value. This will not impact that value of
y, which will still point back to the same integer object with value
x = 7 id(x)
If all references to an object are removed, the object is deleted from memory in a process called garbage collection.