01-01 Variables

01 - 01 Variables

As the name implies, a variable is something that can change. A variable is just a way of referring to a memory location used by a python program. Based on the datatype of the variable, the python interpreter allocates the memory and decides what can be stored in the reserved memory. This makes python a dynamically-typed language.

  • If you are familiar with other programming languages like C, C++ or Java it might be tempting to consider variable as just a container to store data. However in Python, you can at best, think of variables as pointers. This is why you can dynamically change the type of data that a variable is pointing at.

01 - 01.01 Assigning values to variables

One of the main differences between Python and strongly types languages like C++ or Java is the way it deals with the data types. In languages like C++ or Java, every variable must have a unique data type i.e if a variable is of type string it cannot store integers or floats. Moreover every variable has to be declared before it can be used, thus binding it to the data type that can be stored in it. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when a value is assigned to a variable. This means that a variable that was used to store a string can now be used to store an integer. Try it out. Do something like this:

In [1]:
var = 'I am in NYC'
I am in NYC

var is a string in the above case.

Well.. don't take my word for it. Lets confirm

In [2]:

type is a built-in function that returns the datatype of anything.. and by anything, I mean absolutely anything (give it a spin!). It is one of the cool things in Python. We will talk about this and many other cool built-in functions in the later chapters.

so now if I point our variable var to, lets say, an integer.. it will simply work

In [3]:
var = 123

So what are the rules of naming a variable?

Every language has some rules for naming the identifier of variables (aka the variable name). In Python, a valid identifier is a non-empty sequence of characters of any length with:

The start of the character can be an underscore "_" or a capital or lower case letter. However it is generally recommended to use all upper case for global variables and all lower case for local variables. The letters following the first letter can be a digit or a string. Python is a case-sensitive language. Therefore, var is not equal to VAR or vAr.

Apart from above restrictions, python keywords cannot be used as identifier names. These are:

and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

01 - 01.02 Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. For example:

In [4]:
x = y = z = a = 1

or even assign different values to different variables:

In [5]:
x, y, z, a = 'Hello', 'World', 1, 2

Print the above variables


comments powered by Disqus