×
Namespaces

Variants
Actions
(Difference between revisions)

Basic Python Elements

From Nokia Developer Wiki
Jump to: navigation, search
bogdan.galiceanu (Talk | contribs)
kiran10182 (Talk | contribs)
m (Python and PyS60 are directed to internal links)
Line 1: Line 1:
This article aims to provide information about the most basic Python and PyS60 operations. It is intended for beginners in the Python programming language.
+
This article aims to provide information about the most basic [[Python]] and [[PyS60]] operations. It is intended for beginners in the [[Python]] programming language.
  
 
For an overview and installation instructions, see [http://wiki.forum.nokia.com/index.php/Category:Python FNWiki Python]
 
For an overview and installation instructions, see [http://wiki.forum.nokia.com/index.php/Category:Python FNWiki Python]
  
 
==Description==
 
==Description==
Python is a programming language similar to C++, yet more automated. The user has to do less in order to achieve the same result (i.e. the type of a variable doesn't have to be declared, Python analyzes it and stores it accordingly).
+
[[Python]] is a programming language similar to C++, yet more automated. The user has to do less in order to achieve the same result (i.e. the type of a variable doesn't have to be declared, [[Python]] analyzes it and stores it accordingly).
Python consists of two parts: the PC editor and compiler, used to write and test the code, and the on-phone interpreter, which reads and executes the code.
+
[[Python]] consists of two parts: the PC editor and compiler, used to write and test the code, and the on-phone interpreter, which reads and executes the code.
While the Python editor (IDLE or command line) is useful for testing applications in real time, due to the fact that operations are performed in real time, some users may prefer entering code in a traditional text editor, such as Notepad.
+
While the [[Python]] editor (IDLE or command line) is useful for testing applications in real time, due to the fact that operations are performed in real time, some users may prefer entering code in a traditional text editor, such as Notepad.
  
 
==Variables==
 
==Variables==
Line 15: Line 15:
 
stores the value 2 in the variable named x.
 
stores the value 2 in the variable named x.
  
In Python, there are the following types of variable:
+
In [[Python]], there are the following types of variable:
 
-integer - a number
 
-integer - a number
 
-long - an extended version of integer
 
-long - an extended version of integer
Line 73: Line 73:
 
2.14
 
2.14
 
</code>
 
</code>
"#" in the program code starts a comment (a line that is not interpreted by Python, and is for the programmer to describe certain operations)
+
"#" in the program code starts a comment (a line that is not interpreted by [[Python]], and is for the programmer to describe certain operations)
  
 
==Operators and operations==
 
==Operators and operations==
Line 188: Line 188:
  
 
==Functions==
 
==Functions==
Functions in Python are used to perform a set of operations to modify a certain element, or for many other purposes.
+
Functions in [[Python]] are used to perform a set of operations to modify a certain element, or for many other purposes.
 
An example of a function is the "pow" function:
 
An example of a function is the "pow" function:
 
<code python>
 
<code python>
Line 215: Line 215:
 
</code>
 
</code>
 
==Importing modules==
 
==Importing modules==
Modules contain certain functions that are not loaded with Python by default, but instead need to be loaded by the user.
+
Modules contain certain functions that are not loaded with [[Python]] by default, but instead need to be loaded by the user.
 
There are two import methods:
 
There are two import methods:
 
<code python>
 
<code python>
Line 228: Line 228:
 
The difference is that the first method imports the entire "math" module, while the second only imports the needed function, "sin".
 
The difference is that the first method imports the entire "math" module, while the second only imports the needed function, "sin".
  
An important module for PyS60 is "appuifw". It contains the functions used to get certain types of input and output.
+
An important module for [[PyS60]] is "appuifw". It contains the functions used to get certain types of input and output.
 
Example:
 
Example:
 
<code python>
 
<code python>

Revision as of 07:57, 18 November 2007

This article aims to provide information about the most basic Python and PyS60 operations. It is intended for beginners in the Python programming language.

For an overview and installation instructions, see FNWiki Python

Contents

Description

Python is a programming language similar to C++, yet more automated. The user has to do less in order to achieve the same result (i.e. the type of a variable doesn't have to be declared, Python analyzes it and stores it accordingly). Python consists of two parts: the PC editor and compiler, used to write and test the code, and the on-phone interpreter, which reads and executes the code. While the Python editor (IDLE or command line) is useful for testing applications in real time, due to the fact that operations are performed in real time, some users may prefer entering code in a traditional text editor, such as Notepad.

Variables

Variables are "containers" used to store values. For example:

x=2

stores the value 2 in the variable named x.

In Python, there are the following types of variable: -integer - a number -long - an extended version of integer -float - a number with decimals -boolean - a logical variable (can be TRUE or FALSE) -character - a text character (like '1' or 'f') -string - a sequence of 1 or more characters (like 'Nokia' or '2')

There are other kind os variables called arrays of elements (strings and lists). An array is a collection of items, like:

list=[1,2,3,4] #a list of numbers
list=['a','b'] # a list of characters
#etc

An array is characterised by its length (the number of elements). For example, the length of [1,2,3,6,1,'d'] is 6. Note that an array is evaluated from position 0 (element 1, in this case) to its length-1 (element d).

>>>a=[1,2,3]
>>>a[2]
3
 
>>>s="alabala"
>>>s[3]
b

To check whether an element is in an array, if the array is short and well known, we do the following:

element in array #returns true if the element is in that array, and false if not.
#Example:
>>>x=2
>>>if(x in [1,5,2,7,2,4]):print 'yes'
else:print 'no'
 
yes

Program sytax and other general info

The input is what the user tells the program. x=2, x=raw_input() are ways to give x a value. Output is what the program shows the user. print x outputs the value of x. You can also use print "%letter" % (value). The letters and associated types are:

f - float

d - integer

s - string

Example:

>>>print "%d" % (5)
5
>>>print "%s" % ('series 60')
series 60
>>>x=2.14
>>>print "%f" % (x)
2.14

"#" in the program code starts a comment (a line that is not interpreted by Python, and is for the programmer to describe certain operations)

Operators and operations

Operators

Operators are used to compare variables. < (smaller), > (greater), <= (smaller or equal), == (equal), >= (greater or equal), != (different)

x=2
y=5
 
x<y
x<=y
x!=y
x>=y
#return true
x>y
x==y
#return false

"and", "or", "not" - used with boolean variables (true and true=true, true and false=false, t or t=t, t or f=t, not t=f, not f=t)

x=2
y=3
(x<y) and (y!=0) #returns true
not(x==2) #returns false

General operations

Like in any programming language, the programmer can manipulate the variables in almost any way they desire. Here is a list of operations, the types of variables they can be applied to and examples:

- "+" -integer (2+5=7), long (500000000+1000000000=6000000000), float (2.34+1.2=3.54), character ('f'+'g'='fg') and string ('nokia'+' n95'='nokia n95')

- "-" -integer, long, float

- "*" -integer, long, float, or between an integer and a character or string (3*'hi'='hihihi')

- "/" -integer, long, float

- "%" -the modulus, meaning the number you get after dividing a number to another number, long, float (2.4%2=0.4)

Special operations

These apply to arrays

-Changing an element

>>> x = [1, 1, 1]
>>> x[1] = 2
>>> x
[1, 2, 1]

-Deleting elements

>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Earl']

-Separating elements

>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']

-Appending

>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

Conditionals and loops

In order to check if a condition is true or not, the 'if' structure is used. Based on the validity of the condition, instructions can be given.

if <condition> : 
<instruction1>
<instruction2>
.
.
.
else :
<instra>
<instrb>
.
.
.

The condition is checked. If it is true, the instructions 1 to ... will be carried out. If, on the other hand, the condition is false, instructions a to ... will be carried out. Example:

>>>x=2
>>>if(x==2):print 'ok'
ok
 
>>>if(x==56):print '56'
else: print x
2

When an action needs to be repeated for a certain number of times, or until a condition is met, we use the 'while' loop:

while <cond>:
<instr1>
<instr2>

If the condition is true, the loop is entered and the instructions will be performed until the condition becomes false. Example:

i=1
while(i<=10):print 'ok'
okokokokokokokokokok

Functions

Functions in Python are used to perform a set of operations to modify a certain element, or for many other purposes. An example of a function is the "pow" function:

>>>x=2
>>>y=3
>>>pow(x,y)
8

A function is declared like this:

def functionname(parameters):
<operations>
return result

Functions can also be used to convert a type of variable into another. The structure is variable2=function(variable1) These functions are: int(var) and long(var)->number, float(var)->number with decimals, str(var)->string, chr(var)->character. Example:

>>>x=2.34
>>>print int(x)
2
>>>str(x)
'2.34'

Importing modules

Modules contain certain functions that are not loaded with Python by default, but instead need to be loaded by the user. There are two import methods:

>>>import math
>>>sin(3.14159)
0
#or
>>>from math import sin
>>>sin(3.14159)
0

The difference is that the first method imports the entire "math" module, while the second only imports the needed function, "sin".

An important module for PyS60 is "appuifw". It contains the functions used to get certain types of input and output. Example:

import appuifw
appuifw.note(u"This is an example")
# will give you a message containing the text "This is an example"

Other important modules are "graphics" and "e32".

148 page views in the last 30 days.
×