Scripting >> Python >> Syntax >> Python 2.x reserved words aka vocabulary

 

 

 

and logical and in conditional expressions
as

follows the with statement or import statement

assigns a name

assert

insert debugging assertions into a program

assert_stmt ::=  "assert" expression ["," expression]
break It terminates the nearest enclosing loop
- in for loop, break keeps current value of the iteration variable
- in try with finally, the finally clause is executed before leaving the loop
break_stmt ::=  "break"
class

 defines a class object

classdef    ::=  "class" classname [inheritance] ":" suite
inheritance ::=  "(" [expression_list] ")"
classname   ::=  identifier
continue

It continues with the next cycle of nearest enclosing for or while loop

- in try with finally, the finally clause is executed before leaving the loop

def  Defines a function
def whats_on_the_telly(penguin=None):
    if penguin is None:
        penguin = []
    penguin.append("property of the zoo")
    return penguin
del

Delete variables, opposite of assignment. e.g.

>>> t = ['a', 'b', 'c']
>>> del t[1]
>>> print t
['a', 'c']
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del t[1:5]
>>> print t
['a', 'f']
del t[0]
elif used in if block
else used within if block
except used with try block
exec

executes the string that follows as python code.

exec_stmt ::=  "exec" or_expr ["in" expression ["," expression]]
finally used in try block
for

used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]
from  
global

declares the identifiers that follow as global for the entire code block.

global_stmt ::=  "global" identifier ("," identifier)*
if  used for conditional execution
if_stmt ::=  "if" expression ":" suite
             ( "elif" expression ":" suite )*
             ["else" ":" suite]
import

imports external modules into your code

import_stmt     ::=  "import" module ["as" name] ( "," module ["as" name] )*
                     | "from" relative_module "import" identifier ["as" name]
                     ( "," identifier ["as" name] )*
                     | "from" relative_module "import" "(" identifier ["as" name]
                     ( "," identifier ["as" name] )* [","] ")"
                     | "from" module "import" "*"
module          ::=  (identifier ".")* identifier
relative_module ::=  "."* module | "."+
name            ::=  identifier
in  
is  
lambda  
not  
or  
pass  is a null operation - nothing happens when it is executed.  Useful as placeholder when a statement is required syntactically but no code needs to be execuoted
print  evaluates each expression and writes the result to standard output
print_stmt ::=  "print" ([expression ("," expression)* [","]]
                | ">>" expression [("," expression)+ [","]])
raise
raise_stmt ::=  "raise" [expression ["," expression ["," expression]]]

If no expressions are present, raise re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a TypeError exception is raised indicating that this is an error (if running under IDLE, a Queue.Empty exception is raised instead).

return

 it leaves the current function call with the expression list (or None) as the return value

return_stmt ::=  "return" [expression_list]
try

specifies exception handlers and/or cleanup code for a group of statements:

try_stmt  ::=  try1_stmt | try2_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression [("as" | ",") identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               "finally" ":" suite
while

used for repeated execution as long as an expression is true

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]
with

 used to wrap the execution of a block with methods defined by a context manager. This allows common try...except...finally usage patterns to be encapsulated for convenient reuse.

with_stmt ::=  "with" with_item ("," with_item)* ":" suite
with_item ::=  expression ["as" target]
yield

The yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function

# to put an example here #