Variables

walden systems, geeks corner, programming, languages, python, tuples, lists, string, variables, developer
Python is a programming language that lets you work quickly and integrate systems more effectively.

A variable is something which can change. A variable is a way of referring to a memory location used by a computer program. A variable is a symbolic name for this physical location. This memory location contains values, like numbers, text or more complicated types.

A variable can be visualized as a container to store certain values. While the program is running, variables are accessed and sometimes changed. One of the main differences between Python and strongly-typed languages like C, C++ or Java is the way it deals with types. In strongly-typed languages every variable must have a unique data type; if a variable is of type integer, only integers can be saved in the variable. In Java or C, every variable has to be declared before it can be used. Declaring a variable means binding it to a data type.

Declaration of variables is not required in Python. If you need a variable, you think of a name and start using it as a variable. In Python, the value and the type of a variable may change during program execution. You can assign an integer value to a variable, use it as an integer for a while and then assign a string to the variable.

You assign values to a variable using the equal "=" sign. The "=" shouldn't be thought of as "is equal to" but interpreted as "is set to".

Below are some examples of assigning variables.


Multiple assignments

In Python, multiple variable assignments can be done at once. Since the "=" is interpreted as is set to, you can assign multiple variables at once. The examples below are perfectly valid Python statements.

a = b = c = d = 1
a, b , and c are all set to 1

x = y = z = "John"
x, y, and z are all set to "John"

Standard data types

Python has five standard data types. Each of the data types has different methods for manipulating them.

Changing data types

Python automatically takes care of the physical representation for the different data types, i.e. an integer values will be stored in a different memory location than a float or a string. Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function. There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value. Below are some examples of conversion methods, the parameter between brackets are optional.

Convert to integer
int(x [,base] )
Converts x to an integer. base specifies the base if x is a string.

Convert to long integer
long(x [,base] )
Converts variable to a long integer. base specifies the base if x is a string.

Convert to floating point number
float(x )
Converts variable to a floating point number.

Convert to list
list(x )
Converts variable to a list.

Convert to tuple
tuple(x )
Converts variable to a tuple.

Convert to dictionary
dict(x )
Creates a dictionary. x must be a sequence of ( key,value ) tuples.