16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

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 <strong>to</strong> mark a variable as global. It is used in functions <strong>to</strong> allow statements<br />

in the function body <strong>to</strong> 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 <strong>to</strong> execute strings containing <strong>Python</strong> 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!