Variables
In Python, a variable is simply a name or a label that point to an object.

>>> x = 5
>>> print(x)
>>> print(type(x))
>>> y = complex(3, 5)
>>> print(y)
>>> print(type(y))
You can have different variables pointing to the same object.

Use id() to get the address of the object in memory. If two variables have the same id, they both point to the same object.
>>> a = "I love to learn"
>>> print(a)
>>> print(type(a))
>>> b = a
>>> print(b)
>>> print(type(b))
>>> print(id(a))
>>> print(id(b))
>>> print(id("I love to learn"))
You can also reassign an existing variable to point to a different object (of any type) at any stage of your program.

Try out the following, and make sure you understand what is happening.
>>> x = 10
>>> print(x)
>>> print(type(x))
>>> y = 7.11
>>> print(y)
>>> print(type(y))
>>> x = "I am no longer the number 10"
>>> print(x)
>>> print(type(x))
>>> x = y
>>> print(x)
>>> print(type(x))
>>> y = "Imperial College London"
>>> print(y)
>>> print(type(y))
>>> print(x)
>>> print(type(x))
How am I allowed to name my variables?
A variable name in Python:
- Can consist of letters, digits, or underscores (
_) - Must start with a letter or an underscore
- Cannot be a reserved keyword (e.g.
Trueorwhile)
Can you figure out which variable names below are valid? Test them out to confirm (what error message would you receive?)
>>> my_variable123 = 2
>>> _12is_thisvalid = 2
>>> 123abc = 2
>>> try = 2
>>> __twounderscores = 2
>>> 1 = 2
Variable names are case-sensitive, so myvar is not the same as MyVar or MYVAR
>>> university = "London"
>>> University = "Imperial"
>>> print(university)
>>> print(University)
How should I name my variables?
Of course, what you are allowed do is different from what you should do.
Just because a variable name is valid does not mean that it is a sensible name.
- Use semantically meaningful names
x = 25does not tell you much.age = 25is self-explanatory. -
Never use
l,O, orIalone as a single character variable name. You yourself may have found that confusing to read. Was that a0or anO? Or is that a1or anlor anI? -
Ending your variable names with an
lor1might also be confusing. Is that anidealor anidea1? Use with caution. - Never use Python built-ins (some of these are not keywords) as variable names (e.g.
str,list). You can do it, but you will confuse yourself, others, and the Python interpreter, and suffer the consequences later.>>> x = str() >>> type(x) >>> str = 3 >>> type(str) # str is an int. Say that again? >>> str() # the str type is gone! >>> str.capitalize() # this is not a string! -
The official style guide to Python recommends your variable names to be “lowercase, with words separated by underscores as necessary to improve readability…
mixedCaseis allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.” So they are essentially happy with something likeheightormy_beautiful_home, but do not recommendmyBeautifulHome.Josiah says: “I am not overly strict about this. I myself keep changing my mind about whether to use
lowercase_with_underscoresormixedCase. But try to uselowercase_with_underscoresespecially in a collaborative project.”