Wednesday, May 6, 2015

Python: Some Keywords

KEYWORDDESCRIPTIONEXAMPLE
andLogical and.True and False == False
asPart of the with-as statement.with X as Y: pass
assertAssert (ensure) that something is true.assert False, "Error!"
breakStop this loop right now.while True: break
classDefine a class.class Person(object)
continueDon't process more of the loop, do it again.while True: continue
defDefine a function.def X(): pass
delDelete from dictionary.del X[Y]
elifElse if condition.if: X; elif: Y; else: J
elseElse condition.if: X; elif: Y; else: J
exceptIf an exception happens, do this.except ValueError, e: print e
execRun a string as Python.exec 'print "hello"'
finallyExceptions or not, finally do this no matter what.finally: pass
forLoop over a collection of things.for X in Y: pass
fromImporting specific parts of a module.import X from Y
globalDeclare that you want a global variable.global X
ifIf condition.if: X; elif: Y; else: J
importImport a module into this one to use.import os
inPart of for-loops. Also a test of X in Y.for X in Y: pass also 1 in [1] == True
isLike == to test equality.1 is 1 == True
lambdaCreate a short anonymous function.s = lambda y: y ** y; s(3)
notLogical not.not True == False
orLogical or.True or False == True
passThis block is empty.def empty(): pass
printPrint this string.print 'this string'
raiseRaise an exception when things go wrong.raise ValueError("No")
returnExit the function with a return value.def X(): return Y
tryTry this block, and if exception, go to except.try: pass
whileWhile loop.while X: pass
withWith an expression as a variable do.with X as Y: pass
yieldPause here and return to caller.def X(): yield Y; X().next()

No comments: