Functions in Python (def)

Simple Hello-name function in Python

def hello_name(name):
    return 'Hello ' + name + '!'

To execute:

print(hello_name('Peter'))
The output will be:

Hello Peter!

________

Other examples of functions in Python:



def string_times(str,n):
    return str * n
or...
def first_last6(nums):
    if nums([0]) == 6 or nums[-1] == 6:
        return True    else:
        return False
or...
def sleep_in(weekday,vacation):
    if weekday == False or vacation == True:
        return True    else:
        return False
or (simplified from above)...
def sleep_in2(weekday,vacation):
    if not weekday or vacation:
        return True    else:
        return False
or...
def first_last6(nums):
    if nums([0]) == 6 or nums[-1] == 6:
        return True    else:
        return False

Comments

Popular posts from this blog

Installing Microsoft ODBC Driver to connect to SQL from Python on Mac OS X

Connection to MSSQL with Pythonan and pyodbc (on Mac OS X)

Installing PyCharm on Mac