Docsity

Programacion I: funciones en Python (transparencias) - Business Analytics, Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programacion I: funciones en Python (transparencias) - Business Analytics y más Diapositivas en PDF de Programación Informática solo en Docsity!

Last updated on 28/07/2020 09:

Business Analytics

PROGRAMACIÓN I

FUNCTIONS

‣ Definition

‣ Arguments and parameters

‣ Annotations/type hinting, docstrings

‣ Execution frames and namespaces

‣ Arbitrary length arguments

‣ Lambda (anonymous functions)

FUNCTIONS

1. Title

§ Groups of related statements

§ Parameters, returns a value

§ DRY: do not repeat yourself

§ When invoked, body of the function is called before control returns.

Functions def function_name(): … …

Start of program

… … function_name() … function_name() beginnerspython3/chapter

1. Title

Built-in functions are those provided by the language and we have seen several of these already:

§ For example, both print() and input()

User-defined functions are those written by developers

Built in and user defined functions

1. Function, definition and structure Title def print_msg(): print( 'Hello World!' ) print_msg() def print_my_msg(msg): print(msg) print_my_msg( 'Hello World' ) def square(n: int) - > int: return n * n # Store result from square in a variable result = square(n= 4 ) print(result)

‣ Definition

‣ Arguments and parameters

‣ Annotations/type hinting, docstrings

‣ Execution frames and namespaces

‣ Arbitrary length arguments

‣ Lambda (anonymous functions)

FUNCTIONS

1. Named/keyword arguments Title

§ Named arguments (or keyword arguments):

We provide the name of the parameter we want an argument/value to be

assigned to

def make_list_of_range(start: int, end: int) - > list: """ Makes a list of consecutive integers between start and end :param start: first integer :param end: last ingeter :return: a list with consecutive integers """ new_list = list(range(start, end)) return new_list make_list_of_range(start= 2 , end= 4 ) make_list_of_range(end= 2 , start= 4 ) prog_1/functions/parameters_example.py Keyword arguments

1. Named/keyword arguments Title

§ Positional arguments:

Arguments assigned to parameters based on position

def make_list_of_range(start: int, end: int) - > list: """ Makes a list of consecutive integers between start and end :param start: first integer :param end: last ingeter :return: a list with consecutive integers """ newList = list(range(start, end)) return newList make_list_of_range( 2 , 4 ) prog_1/functions/parameters_example.py Positional arguments

1. Default values Title

§ With default values:

Function arguments can have default values in Python.

We can provide a default value to an argument by using the assignment

operator (=).

def make_list_of_range_default(start: int, end: int = 10 ) - > list: """ Makes a list of consecutive integers between start and end :param start: first integer :param end: last integer :return: a list with consecutive integers """ newList = list(range(start, end)) return newList make_list_of_range_default( 2 ) prog_1/functions/parameters_example.py

1. Default values Title

  • Any number of arguments in a function can have a default value.
  • Once we have a default argument, all the arguments to its right must also have

default values.

  • That is, non-default arguments cannot follow default arguments.

1. Title

  • It is possible to return multiple values from a function
  • We can then assign the values returned to variables at the point when the function is called:

Notice the annotation. May need to import Typing (python < 3.9 or use tuple, P>= 3.9)

Return, multiple values def swap(a: int, b: int) - > tuple[int, int]: return b, a a = 2 b = 3 x, y = swap(a, b) print(x, ',' , y)

‣ Definition

‣ Arguments and parameters

‣ Annotations/type hinting, docstrings

‣ Execution frames and namespaces

‣ Arbitrary length arguments

‣ Lambda (anonymous functions)

FUNCTIONS

1. Docstring and annotation (type hinting) Title def make_a_list_annotated(start: int, end: int) - > List: """ Makes a list of consecutive integers between start and end :param start: first integer :param end: last integer :return: a list with consecutive integers """ new_list = list(range(start, end)) return new_list beginnerspython3/chapter11/helloworldfunction.py

1. Docstring and annotation (type hinting) Title def make_a_list_of_range_default(start: int, end: int = 10) - > list: """ Makes a list of consecutive integers between start and end :param start: first integer :param end: last integer :return: a list with consecutive integers """ new_list = list(range(start, end)) return new_list beginnerspython3/chapter11/helloworldfunction.py

  • Good practice
  • IDEs will show documentation when

used

  • After 6 months, any code, even yours,

will look like new

  • Required in homework