What are keywords?
Keywords in Python are reserved words that cannot be used as ordinary
identifiers. They must be spelled exactly as they are written.
List of keywords
The following is a list of keywords for the Python programming language.
and | del | from | not |
while | as | elif | global |
or | with | assert | else |
if | pass | yield | break |
except | import | class | |
exec | in | raise | continue |
finally | is | return | def |
for | lambda | try |
This prints the keyword list of Python.
$ python
>>>
>>> import keyword
>>> print keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with',
'yield']
Keywords Explained
print
print to console
while
controlling the flow of the program
for
iterate over items of a collection in order that they appear
break
interrupt the (loop) cycle, if needed
continue
used to interrupt the current cycle, without jumping out of the whole cycle.
New cycle will begin.
if
used to determine, which statements are going to be executed.
elif
stands for else if.If the first test evaluates to False,
then it continues with the next one
else
is optional. The statement after the else keyword is executed,
unless the condition is True
is
tests for object identity
not
negates a boolean value
and
all conditions in a boolean expression must be met
or
at least one condition must be met.
import
import other modules into a Python script
as
if we want to give a module a different alias
from
for importing a specific variable, class or a function from a module
def
used to create a new user defined function
return
exits the function and returns a value
lambda
creates a new anonymous function
global
access variables defined outside functions
try
specifies exception handlers
except
catches the exception and executes codes
finally
is always executed in the end. Used to clean up resources.
raise
create a user defined exception
del
deletes objects
pass
does nothing
assert
used for debugging purposes
class
used to create new user defined objects
exec
executes Python code dynamically
yield
is used with generators
More reading
For a more detailed explanation and examples of keywords, please see:
http://zetcode.com/lang/python/keywords/
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.