Pyhton Functions

Embed Size (px)

DESCRIPTION

Python functions

Citation preview

  • Functions in PythonJagsel Biosolutions

    Jagsel Biosolutions

  • FunctionsBlock of statements with a name which perform an operation on one or more values and return a resultFunctions are reusable pieces of programsCan run at anytime anywhere in program (Calling a function)

    Jagsel Biosolutions

    Jagsel Biosolutions

  • Continue

    Types:User defined functions creating our own function in PythonBuilt in functionsPython with pre-defined functions

    Jagsel Biosolutions

    Jagsel Biosolutions

  • User defined FunctionSyntax:def fun_name(arguments/parameters):..Ex:

    Jagsel Biosolutions

    Jagsel Biosolutions

  • defFunctions are defined using the keyword deffollowed by an identifier name for the function followed by a pair of parentheses which may enclose some names of variables and the line ends with a colon.Followed by a block of statements that are part of this functionJagsel Biosolutions

    Jagsel Biosolutions

  • ParametersA function can take parameters which are just values you supply to the function so that the function can do something utilising those values.Parameters variables except that the values of these are definedSpecified within parantheses seperated by commasJagsel Biosolutions

    Jagsel Biosolutions

  • Arguments and parameters

    the names given in the function definition are called parameters whereas the values you supply in the function call are called arguments

    def subtract(x,y): Parametersprint x-y def seq(ATGCC): Argument print dna

    Jagsel Biosolutions

    Jagsel Biosolutions

  • Return statement The return statement returns with a value from a functionreturn without an expression argument returns NoneSyntax:return_stmt ::= "return" [expression_list]If an expression list is present, it is evaluated, else None is substitutedJagsel Biosolutions

    Jagsel Biosolutions

  • Reverse a sequenceJagsel Biosolutions

    Jagsel Biosolutions

  • Functions can call functionsJagsel Biosolutions

    Jagsel Biosolutions

  • Use of for and if statementJagsel Biosolutions

    Jagsel Biosolutions

  • Built in FunctionsPython interpreter has a number of functions built into it that are always available.Most Frequently used built in functions in python are:len()raw_input()open()Etc.,Jagsel Biosolutions

    Jagsel Biosolutions

  • ContinueJagsel Biosolutions

    Jagsel Biosolutions

  • ContinueJagsel Biosolutions

    Jagsel Biosolutions

  • len() and range()len()

    range()

    Jagsel Biosolutions

    Jagsel Biosolutions

  • split()Jagsel Biosolutions

    Jagsel Biosolutions

  • join()Jagsel Biosolutions

    Jagsel Biosolutions

  • Count() & reverse()Jagsel Biosolutions

    Jagsel Biosolutions

  • raw_input()Easiest way to obtain user input from the command-line.Jagsel Biosolutions

    Jagsel Biosolutions

    ***