python_lectures_5.ppt - Python Crash Course Functions...

This preview shows page 1 - 8 out of 28 pages.

Python Crash CoursePython Crash CourseFunctions, ModulesFunctions, ModulesBachelorsV1.0dd 20-01-2014Hour 1
Introduction to language - functionsIntroduction to language - functionsWhat are functionsA function is a piece of code in a program. The function performs a specific task. The advantages of using functions are:•Reducing duplication of code•Decomposing complex problems into simpler piecesImproving clarity of the code•Reuse of code•Information hidingFunctions in Python are first-class citizens. It means that functions have equal status with other objects in Python. Functions can be assigned to variables, stored in collections or passed as arguments. This brings additional flexibility to the language.Function typesThere are two basic types of functions. Built-in functions and user defined ones. The built-in functions are part of the Python language. Examples are: dir(), len() or abs().
Introduction to language - functionsIntroduction to language - functions>>> def my_func(x, y, z): ... a = x + y ... b = a * z ... return b ... >>>Defining FunctionsHere are simple rules to define a function in Python:•Function blocks begin with the keyword deffollowed by the function name and parentheses ( ).•Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.•The code block within every function starts with a colon : and is indented.•The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.>>> my_func(1.0, 3.0, 2.0) 8.0 >>> my_func(1.0, 3.0, 1.0) 4.0 >>> my_func(5.0, 0.0, 1.0) 5.0 >>> my_func(2.0, 0,0 3.0) 6.0
Introduction to language - functionsIntroduction to language - functions#!/usr/bin/pythondef f1():print "f1()"f1()#f2()def f2():print "f2()"Defining FunctionsFunction must be denifed preceding their usage:#!/usr/bin/pythondef f():print "f() function"def g():def f():print "f() inner function"f() f()g()uncommenting f2()will cause a NameErrorwhere to define functionsinner function definition
Introduction to language - functionsIntroduction to language - functionsFunctions are objects>>> def f():... """This function prints a message """... print "Today it is a cloudy day"...>>> f.__doc__'This function prints a message '>>> f()Today it is a cloudy day>>> id(f)140491806602016>>>>>> def f():... pass...>>> def g():... pass...>>> def h(f):... print id(f)...>>> a=(f, g, h)>>> for i in a:... print i...<function f at 0x7fc6cc39f320><function g at 0x7fc6cc39f398><function h at 0x7fc6caab1c80>>>> h(f)140491806602016>>> h(g)140491806602136>>>
Introduction to language - functionsIntroduction to language - functionsFunctions types•always available for usage•those contained in external modules•programmer defined>>> from math import sqrt>>> def cube(x):... return x * x * x ... >>> print abs(-1)1>>> print cube(9)729>>> print sqrt(81)9.0
Introduction to language - functionsIntroduction to language - functionsThe return keywordis used to return valueno return returns None>>> def cube(x):... return x * x * x...

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture