Basic Python Objects, Variables, and Operators¶
Variables¶
Everything in python can be considered to be either a variable, an object, or an operator. An object can be everything from a number, to a function, to something more complex like a class. For the moment let’s not worry too much about objects, in fact most of this course is about how to create and use the plethora of objects that exist in the python language. Briefly an operator is something that does something to a variable or an object (e.g. =, +, -, *). We’ll talk about operators in a moment. Instead for now let’s focus on the variable in python.
A python variable is basically the name we give to an object in a script. Assignment of a variable is simple using the = operator:
In [1]: x = 42 # the variable in this case is x and we have assigned the value of 42 to the variable
A variable can be named anything except one of the built in keywords of python. To get a list of these keywords you can use the help function:
In [2]: help('keywords')
Here is a list of the Python keywords. Enter any keyword to get more help.
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass
While you can name a variable anything, some options are better than others. As previously mentioned style is important. The PEP 8 standards suggest that variables should be lowercase, with words separated by underscores as necessary to improve readability. While PEP 8 isn’t necessary, it is a really good habit to get into. It is also very important not to give a variable the name of any of the builtin functions and variables, otherwise you cannot use the builtin function again in your script. That said don’t panic about knowing every builtin variable, most integrated development editors will raise some sort of warning when you overwrite a builtin name. Also if you try to use a builtin function again it will simply raise an exception, for example:
# now we'll be naughty and overwrite the help function, really don't do this...
In [3]: help = 42
# if we try to use the help function it will raise an exception
In [4]: help('keywords')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-9d2f16847670> in <module>()
----> 1 help('keywords')
TypeError: 'int' object is not callable
if you make this mistake, fix it in your script and reload you interpreter.
Why did this happen? It has to do with how python assigns variables. When we assigned the value of 42 to x above the number 42 was created in the computer’s memory and the variable x was pointed to that memory via a unique object ID. python has a built in function id(), which allows us to see the this ID. This is helpful as we can see how python handles memory. Take a look at the example below:
In [5]: x = 42
In [6]: id(x)
Out[6]: 93997891682112
In [7]: y = x
In [8]: id(y)
Out[8]: 93997891682112
In [9]: x = 15
In [10]: y
Out[10]: 42
In [11]: id(x)