beautypg.com

4 conditional execution, 5 loops – Rainbow Electronics GM862-GPS User Manual

Page 16

background image





Easy Script

in Python

80000ST10020a Rev.8 - 01/10/08

Reproduction forbidden without Telit Communications S.p.A. written authorization - All Rights Reserved

page 16 of 100

1.5.4 Conditional execution

• Python

uses

if, elif (not elsif or elseif), and else to denote conditional execution of statements.

For example:

if a > b:

print 'a is greater than b.'

elif a < b:

print 'a is lower than b.'

else:

print 'a equals b.'

• You can use "abbreviated" interval tests:

if 2 <= a <= 7:

print 'a is in the interval [2, 7].'

1.5.5 Loops

• Loops in Python are defined by the keywords for and while.

• The following example uses a while loop to collect all numbers from 0 to 99 in a list.

numbers = [ ]

i = 0

while i < 100:

numbers.append(i)

i = i + 1 # or i += 1 since Python 2.0

• A

similar

for loop looks like:

numbers = [ ]

for i in range(100):

numbers.append(i)

• Instead of the explicit loops above also an implicit loop is possible:

numbers = range(100)

range(100) generates a list of all integers from 0 to 99 (not 100).