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




and logical and in conditional expressions

follows the with statement or import statement

assigns a name


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"

 defines a class object

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

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

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

executes the string that follows as python code.

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

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]

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]

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
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_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).


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

return_stmt ::=  "return" [expression_list]

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

used for repeated execution as long as an expression is true

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

 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]

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 #