Join Now
Quality Rating:
  • Currently 2.5 / 5
(2.5 / 5 - 2 votes cast)
Expertise Level:
  • Currently 1.0 / 5
(1.0 / 5 - 1 vote cast)

This page was last modified 23:17, 14 July 2008.

Variables in Python

From Forum Nokia Wiki


Variables in Python

As the name itself says defines variables. Variables can be defined as the items which can be changed. Variables refer to a value which we sometime assign to them. Playing with variables is very easy in python. In python we are not required to declare a variable i.e. there is no need to specify the type of variable we usually do in other object oriented languages. In Python Variables are implicitly type which means that the type of the variable is the type it is refers to.

In Python the first time you declare a variable just use the "=" operator. This creates a new variable and gives the variable its value. One example is shown below:

var = 3
easy = "PyS60"

Variables in python can be categorized in two different forms. They are:

Local Variable

Global Variables

Above mentioned classification of variables are totally based on the the scope of the variable or in simple language the region up to which a variable can be used.

The variables defined in a function by default have a limited reach and visibility. They are visible only inside the function in which they are created and they disappear when the execution is returned from the function. The case of global variables is totally opposite to the local variables they have a scope in throughout the whole program. The example below will easily explain all the conditions.

z = "global"
 
def func_1():
     global x
     x = "global"
     y = "local"
def func_2():
     print x
     print z
 
func_1()
func_2()

In the above example "z" is a global variable. One important thing to notice is that if we want to use the same variable in two functions we have to declare it as global as in the case of "x". One more important thing to observe is func_1() is declared first which emphasize the fact that we have to assign a value to the variable before declaring it.Otherwise the PyS60 interpreter will give a error like "global variable x is not defined." Thats enough to know about variables in Python.

Related Discussions
Thread Thread Starter Forum Replies Last Post
Script-level modules not allowed in 3rd Ed? kevin_s2f Python 6 2007-07-05 18:46
View architecture? abrahim Symbian User Interface 3 2003-05-22 17:58
seconds 2 hh:mm:ss heuri Python 7 2008-07-16 16:37
xyz.obj : error LNK2001: unresolved external symbol __chkstk cl_fragen General Symbian C++ 1 2001-12-12 06:50
Alloc Error jufren_chen General Symbian C++ 4 2006-04-30 08:55
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZpythonQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX