Python Function Define And Arguments Examples

Function is the important concept in Python. You can define function to implement special task, and then use it in other python programs. This article will show you how to define python function and how to pass arguments to the function.

1. Define Python Function.

Use def keyword to define a function as below. The return value is optional, if not return anything python return None by default. It is similar with return void in java. Please note, you must add : at the end of the function_name.

def function_name(arg1, arg2 [, ...]):
        function_statement
        [return value]

Below defines login function which will print login status info in the console. The login function is defined in the terminal with command line. After the function definition ( the last print statement ), you should press enter key to add a blank line to complete the function definition. And then invoke login function with two arguments.

>>> def login(username, password):
...     if(username=='hello') and (password == 'hello'):
...             print("Login Success!")
...     else:
...             print("Login Failed!")
... 

>>> login('hello','hello')
Login Success!
>>> 

2. Python Function Arguments.

2.1 Use String As Default Argument Value.

Function argument can have default value, the default value is passed in function definition. Below example pass string data as the argument default value.

>>> def login(username = 'admin', password = 'hello'):
...     if(username=='hello') and (password == 'hello'):
...             print("Login Success!")
...     else:
...             print("Login Failed!")
... 
>>> login('hello')
Login Success!
>>> 

2.2 Argument Value As A List.

The argument can be a strng, and it can also be a list or tuple.

>>> def login(usernameList = [], password = 'hello'):
...     username = usernameList[0]
...     if(username=='hello') and (password == 'hello'):
...             print("Login Success!")
...     else:
...             print("Login Failed!")
... 
>>> login(['hello','admin'])
Login Success!
>>> 

2.3 Argument Value As A Variable Length Object.

You can also use a * to define a variable length argument. It can contain all the arguments you want to pass in a tuple.

def login(* usernamepassword):
    # Get username in the list.
    username = usernamepassword[0]
    # Get password in the list.
    password = usernamepassword[1]

    if(username=='hello') and (password == 'hello'):
        print("Login Success!")
    else:
        print("Login Failed!")
>>> login('hello','hello')
Login Success

2.4 Argument Value As A Dictionary.

def loginWithDictArguments(**upDict):
    # Get all keys in the dictionary.
    keys = upDict.keys();

    username='';
    password='';
    email='';

    # Loop in the dict keys.
    for key in keys:
        if(key=='username'):
            username=upDict[key]
        
        if(key=='password'):
            password=upDict[key]

        if(key=='email'):
            email=upDict[key]

    if(username=='hello') and (password=='hello'):
        print('Login Success. Your email is ' + email)
    else:
        print('Login fail. Your email is ' + email)     

>>>loginWithDictArguments(username='hello', password='hello',email='[email protected]') 

Login Success. Your email is [email protected]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Index