Showing posts with label Mobile python - 28. Show all posts
Showing posts with label Mobile python - 28. Show all posts

Saturday, 12 January 2013

White Space in Python Code


2.4 White Space in Python Code
Python does not use curly brackets or special begin and end statements
to structure code. Instead, the indentation level of a line determines to
which code block the line belongs. A code block is a group of statements
that belongs to an if clause, a for-loop, a function or a similar structure.
Indentation starts a code block and it ends when the indentation stops.
In the following example, the first two print statements form a code
block that belongs to the if clause. The last print statement does not
belong to the code block and it is executed regardless of the if condition.

if x > 5:
print "X is greater than 5"
print "Also this line belongs to the if-clause"
print "This line does not belong to the if-clause"


You can use spaces or tabs for indentation but you must be consistent.
In this book, we use four spaces for indentation. You can follow the
same style in your own code. Many text editors that support Python
programming, such as Emacs or PythonWin, automatically make sure
that the indentation is consistent. Using an editor like this is highly
recommended.
In some cases you must split a long expression over several lines. As
white space is significant only at the beginning of a statement, you can
split a long expression freely over several lines. However, in some cases
the Python interpreter cannot know whether the subsequent lines belong
to one statement or several statements. To make your intention clear, you
can put a backslash character ‘\’ at the end of a line to specify that the
next line continues the same statement.

Writing a Program in Python for S60


2.3 Writing a Program in Python for S60
Example 1: First PyS60 program
We write a simple program consisting of three lines of code as our first
real example. It should do the following:
1. Display a text input field on the screen; the instruction in the text
field should say ‘Type your name’ (see Figure 2.11(a)).
2. Display a popup note stating ‘Greetings from:’ followed by whatever
the user typed into the text input field (see Figure 2.11(b)).


import appuifw
name = appuifw.query(u"Type your name:", "text")
appuifw.note(u"Hello world! Greetings from: " + str(name), "info")

In the first line of code, we import the appuifw module, which
handles user interface elements such as text input fields and popup notes.
In the second line of code, we create a single-field dialog (Figure
2.11(a)) using the query() function of the appuifw module with
two parameters: label and type. For the first parameter, label, we put
the text u"Type your name:". The u is required because the phone
understands only text declared as Unicode and the quotation marks are
needed because the label parameter must be given as a string. As the
second parameter, type, we put "text", to declare the input field as
a text dialog. Other possible types are "number", "date", "time",
"query" and "code". The two parameters are separated by a comma.
This line of code also creates a variable called name which will receive
the result of the text input field after the user has typed something into
the dialog.
In the third line of code, we create a popup note (Figure 2.11(b))
using the note() function of the appuifw module with two parameters:
label and type. For the first parameter, label, we put the text u"Hello
world! Greetings from: "+ str(name). This is the text that will
appear inside the popup note. Again, the string must be given in quotation
marks but the popup note will also include the result that the user has
typed in, so we add the contents of the variable name to the string.
As the second parameter, type, we put "info", to declare the popup
note as information; that causes a green exclamation mark to appear
inside the popup note. Other possible types are "error", showing a
red exclamation mark, and "conf"(confirmation), showing an animated
check mark. The two parameters are separated with a comma.
Type this example into a file and upload it to your phone for testing,
by following the instructions in Sections 2.1 or 2.2.