04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPENDIX B ■ PYTHON REFERENCE 569<br />

The global Statement<br />

The global statement is used to mark a variable as global. It is used in functions to allow statements<br />

in the function body to rebind global variables. Using the global statement is generally<br />

considered poor style and should be avoided whenever possible.<br />

Example:<br />

count = 1<br />

def inc():<br />

global count<br />

count += 1<br />

The exec Statement<br />

The exec statement is used to execute strings containing Python statements, optionally with a<br />

given global and local namespace (dictionaries).<br />

Examples:<br />

exec 'print "Hello, world!"'<br />

exec 'x = 2' in myglobals, mylocals # ... where myglobals and mylocals are dicts<br />

Compound Statements<br />

Compound statements contain groups (blocks) of other statements.<br />

The if Statement<br />

The if statement is used for conditional execution, and it may include elif and else clauses.<br />

Example:<br />

if x < 10:<br />

print 'Less than ten'<br />

elif 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!