Python Functions Tutorial

Click here to load reader

  • date post

    07-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

description

A python functions tutorial in Greek

Transcript of Python Functions Tutorial

  • . . 7

    Python 7

  • . . 7

    def Functions

  • . . 7

    :

    Python

  • . . 7

    :

  • . . 7

    def (function)

    -

    :

    () ,

    ()

    :

    :

    :

  • . . 7

    def

    def: ( define )

    name: , (identifier)

    (arg1, arg2, argN): (parameters) ( (arguments)) ()

    statements: block

    return: , value ()

    value: ( ) ()

  • . . 7

    def - 1

    return x*y 2

    Enter

    : print(ginomeno(2,4))

  • . . 7

    def - 2 (parameter):

    (arguments): ( )

    () ( )

    :

    2 x

    4 y

  • . . 7

    def - 3

    a, b x & y

    . x, y () 3 & 5

  • . . 7

    def - 4

    :

    ()

    () ( c)

  • . . 7

    def - 5

    print; ;

    ;

    : (, , .)

  • . . 7

  • . . 7

    def - 1

    1. / RETURN

    return

    print()

    return () None

  • . . 7

    def - 2

    2. / RETURN

    ( ) return

    :

  • . . 7

    def - 3

    3. / RETURN

    return

  • . . 7

    def - 4

    4. / RETURN

    return

  • . . 7

    def - 5

    5.

    (override)

    print ;

  • . . 7

    Python

  • . . 7

    Python ; - 1

    def ( )

    def (assignment, =) /

    2 :

    Function object

    func() fname

  • . . 7

    Python ; - 2 (1)

    , (. )

  • . . 7

    Python ; - 3 (2)

    power = dynamh

    power

  • . . 7

    Scope

  • . . 7

    (Variable scope) - 1

    (local) ()

    (local)

    (local scope)

    (local namespace)

    (Global)

    (. ) (global)

    (global namespace)

    : res, x

    : s1, s2

    : ;;

  • . . 7

    - 2

    - (nonlocal variable)

    - (nonlocal) .

    -: first, last nonlocal first & last

    nested

    nonlocal - first, last intersect

  • . . 7

    : ;

    LEGB

    LEGB = Local / Enclosing / Global / Built-in

    H Python 4 :

    Local ()

    Enclosing ()

    Global ()

    Built-in () standard / / , .

  • . . 7

    LEGB

    : local , global nonlocal

  • . . 7

    - 1

    global ( )

    : Global

    Z, Y: Locals

    Global

    Local

    88 (global)

  • . . 7

    - 2

    global

    : Global

    99

    Global

    y & z

    global LEGB

  • . . 7

    - 3

    global

  • . . 7

    - 4

    local

    Global

    Local func1()

    Non-local func2()

    88

  • . . 7

    - 5 has_upper local

    checkPass

    checkUpper nonlocal

    checkPass

    has_lower & has_num

  • . . 7

    Arguments

  • . . 7

    (Arguments) - 1

    (. (local) )

  • . . 7

    (Arguments) - 2

    () - (Immutable) (. , ):

    .

    () (Mutable) :

    .

  • . . 7

    - 1

    (1)

    -

  • . . 7

    - 1

    (2) :

  • . . 7

    - 1

    (3)

    a (immutable) 4

    b (mutable) : b L

  • . . 7

    - 1

    (4)

    (5)

    :

    L:

  • . . 7

    (mutable)

  • . . 7

    ;

    ():

    . myfunc(X, L) myfunc(X, L[:])

    ():

    . :

  • . . 7

    :

    Python