Oop Presentation1
Oop Presentation1
To create a function In Python is defined using the def keyword and in order
to function to work we most call it name followed by parenthesis:
1- Arguments: arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you want, just separate
them with a comma.
PAGE 1
Variables in Python:
The real deal is what happens when we set a variable, will that depends to
the program language that you use, we use python, will python is a
highly object-oriented language. In fact, every item of data in a Python
program is an object of a specific type or class.
So to create a variable:
PAGE 2
There is three types of variables:
1- Global variable:
Variables that are created outside of a function. are known as global
variables.
Global variables are the one that are defined and declared outside a
function and we need to use them inside a function
The variable s is defined as the string “ISM student” before we call the
function stage2(). The only statement in stage() is the “print section”
statement. As there is no local section, the value from the global section
will be used.
Normally, when you create a variable inside a function, that variable is local,
and can only be used inside that function.
PAGE 3
2-Local Variables
A variable created inside a function belongs to the local scope of that
function, and can only be used inside that function.
So a variable declared inside the function's body or in the local scope is
known as a local variable.
In order to create a local variable:
So If you operate with the same variable name inside and outside of a
function, Python will treat them as two separate variables, one available in
the global scope (outside the function) and one available in the local scope
(inside the function):
PAGE 4
In some cases we can use global variable inside the
function:
PAGE 5
3- Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not
defined. This means that the variable can be neither in the local or the global
scope.
PAGE 6