Functions
Return Values and return Statements
When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:
The return keyword.
The value or expression that the function should return.
The None Value
Note: never compare to None
with the ==
operator. Always use is
.
print Keyword Arguments
Local and Global Scope
Code in the global scope cannot use any local variables.
However, a local scope can access global variables.
Code in a function's local scope cannot use variables in any other local scope.
You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.
The global Statement
If you need to modify a global variable from within a function, use the global statement:
There are four rules to tell whether a variable is in a local scope or global scope:
If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.
If there is a global statement for that variable in a function, it is a global variable.
Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
But if the variable is not used in an assignment statement, it is a global variable.
Last updated