Wednesday, May 06, 2009

Python Variables -- Declaring & Using
 
Variable is a name for a memory location where we store/keep some value. It is very easy to work with variables in Python.  Python does not need any "Declaration" of variables before using them.  Python is "not stongly typed".  I mean, in a languages like C/Jave we need to declare a vairiable specifing the type of the variable before using it.  In Python a variable is created whenever some value is assigned to it.  The type of variable is also dynamic.  We can assign any type of value (string/number/list/etc.) to the same variable in the same program.  This gives a great flexibility of using variables, but sometimes wrong values can be assigned to a wrong vairable.  Here are some points to note about variables:
 
~ No Declaration needed, assign value to create a variable
~ Any type of value can be assigned to a variable
~ Alphabets (a..z, A..Z), Digits(0..9) and underscore (_) can be used in the variable names.
~ Variable names should start with alphabets or underscore.
~ Don't know how long a variable name can be. 
~ Variable names are case sensitive
~ There is some special meaning of using underscores at the starting of a vairable name, we will discuss that in later posts.
~ Can not refer a variable without creating it.
 
Examples:
 
>>> a = 10
>>> print a
>>> a = 'Python'
>>> print a
>>> a = [1,'a',2,'b']
>>> print a

No comments: