Python 3: Keywords

I’m learning Python 3 at the moment.  I’ve done some Python 2 before.  Working through Learn Python 3 the Hard Way is interesting.  Lots of making little text adventures.  Interesting to me because I played a lot of them in their short golden era.

Anyway, the author has you periodically enumerate all the interesting symbols you’ve seen in the language and briefly state what you think they mean.  I’m going to do that just for Python 3 keywords here.

and
Logical and with shortcut evaluation.
True and False == False

as
Rename symbols when imported; also used with “with as”.
from sys import argv as clas # Command-line args.
with X as Y: pass

assert
Assert that a boolean expression is true.
assert False, "Error!"

break
Exit from innermost loop.

while True:
    if 1 == 1: break

class
Define a class.
class SoftwareDeveloper(Superhuman): pass

continue
Start innermost loop again (skip any remaining instructions in current iteration).

while True:
    if 1 == 1: continue

def
Define a function.
def myfunction(arg): pass

del
Delete an item from a dictionary.
del d['property']

elif
Start an else if block.
if : pass
elif : pass

else
Start an else block.
if : pass
else: pass

except
Catch an exception.
try: pass
except ValueError, e: print(e)

False
Boolean false literal.
False

finally
Execute this block even if exceptions were raised.

try: pass
except: pass
finally: pass

for
Loop over an iterable.
for element in list: pass

from
Import specific symbols from a module into the current namespace.
from sys import argv

global
Declare that the variable you mention is a global, not a local.
global some_otherwise_local_variable

if
Start an if block.
if True: pass

import
Import a module as a whole.
import sys

in
Check if an element is contained in something else or form for loops.
if choice in "Yy": pass
for element in list: pass

is
Tests equality somewhat like ==. Is this like Java equals()?
1 is 1 == True

lambda
Create a short anonymous function.

squared = lambda y: y ** y
value = squared(4)

not
Logical not.
if not 1 == 1: pass

or
Logical or.
True or False == True

None
Literal value for NoneType.
o = None

nonlocal
This is not local, but it’s not quite global either.
nonlocal var_in_some_enclosing_nonglobal_nonlocal_scope

pass
Do nothing (used to create empty block).
def noop(): pass

print
Okay, this is not a keyword. Builtin to print string and trailing newline.
print("Hello, Python 3!")

raise
Raise an exception.
raise ValueError("No")

return
Return a value (can be a tuple) from a function.
return result

True
Boolean true literal.
True

try
Start an exception handler block.
try: pass

while
Start a while loop.
while True: pass

with
Do a block with an expression as a variable.
with X as Y: pass

yield
Pause and return to caller. Used in generators.
def x(): yield y; x().next()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s