Functions

A simple ‘hello’ function

A very simple function that says ‘hello’ to a person’s name.

# define a function
def hello(name):
    print 'hello ' + name

# call the function
hello('Antonio')

# call the function for a list of names
persons = ['John', 'Maria', 'Fritz']
for person in persons:
    hello(person)

A variation of the hello function above, which says ‘hello’ n amount of times.

# define a function
def hello(name, n=1):
    print 'hello ' * n + name

# call the function (only one argument)
hello('Antonio')

# call the function for a list of items
persons = [ 'John', 'Maria', 'Fritz']
for person in persons:
    # pass a second argument to repeat 'hello'
    hello(person, n=3)