Python Variables
Variable Assignment
Think of a variable as a name attached to a particular object. In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign (=):
>>>n=300
This is read or interpreted as “n is assigned the value 300.” Once this is done, n can be used in a statement or expression, and its value will be substituted:
>>> print(n)
300
Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without the need for print(), so can a variable:
>>>n
300
Later, if you change the value of n and use it again, the new value will be substituted instead
>>> n = 1000
>>> print(n)
1000
>>> n
1000
Python also allows chained assignment, which makes it possible to assign the same value to several variables simultaneously:
>>> a = b = c = 300
>>> print(a, b, c)
300 300 300
The chained assignment above assigns 300 to the variables a, b, and c simultaneously.
Variable Types in Python
>>> var = 23.5
>>> print(var)
23.5
>>> var = "Now I'm a string"
>>> print(var)
Now I'm a string
Object References
>>> print(300)
300
When presented with the statement print(300), the interpreter does the following:
- Creates an integer object
- Gives it the value 300
- Displays it to the console
You can see that an integer object is created using the built-in type() function:
>>>type(300)
< class 'int'>
>>>n = 300
This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.
The following code verifies that n points to an integer object:
>>> print(n)
300
>>> type(n)
< class 'int'>
Now consider the following statement:
m=n
What happens when it is executed? Python does not create another object. It simply creates a new symbolic name or reference, m, which points to the same object that n points to.
Next, suppose you do this:
>>>m=400
Now Python creates a new integer object with the value 400, and m becomes a reference to it.
Lastly, suppose this statement is executed next:
>>>n="foo"
Now Python creates a string object with the value "foo" and makes n reference that.
Variable Names
- Note: One of the additions to Python 3 was full Unicode support, which allows for Unicode characters in a variable name as well. You will learn about Unicode in greater depth in a future tutorial.
For example, all of the following are valid variable names:
>>> name = "Bob"
>>> Age = 54
>>> has_W2 = True
>>> print(name, Age, has_W2)
Bob 54 True
But this one is not, because a variable name can’t begin with a digit:
>>> 1099_filed = False
SyntaxError: invalid token
Note that case is significant. Lowercase and uppercase letters are not the same. Use of the underscore character is significant as well. Each of the following defines a different variable:
>>> age = 1
>>> Age = 2
>>> aGe = 3
>>> AGE = 4
>>> a_g_e = 5
>>> _age = 6
>>> age_ = 7
>>> _AGE_ = 8
>>> print(age, Age, aGe, AGE, a_g_e, _age, age_, _AGE_)
1 2 3 4 5 6 7 8
There is nothing stopping you from creating two different variables in the same program called age and Age, or for that matter agE. But it is probably ill-advised. It would certainly be likely to confuse anyone trying to read your code, and even you yourself, after you’d been away from it awhile.