Categories: PythonPython Function

How to pass arbitrary number of information when calling a function in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

After a function is defined and will be called, it is natural to pass required information in the calling statement. In the following example, we create a function about traveling with parameter person and country, for printing the country people have recently traveled.

#define a function of travel, with two parameter, person and country
def travel(person, country):
    """Print the country people have traveled"""
    print(person+ " have recently been to " + country)
#call the function
travel("Wilson", "USA")
#output
Wilson have recently been to USA

Sometimes, there are multiple elements that will be passed as the particular type of information when calling the function. Say, we assume there may have more than one country that a person have traveled in the past year, but we do not know how many. In this situation, a parameter following an asterisk can be used in function definition. Python packs all the remaining information after ‘person’ into a tuple ‘countries’.

#a function for person and countries have been traveled
def travel(person, *countries):
    """Print the country people have traveled"""
    print(person+ " have recently been to ")
    for country in countries:
        print("- " + country)
#calling function
travel("Wilson", "USA")
#output
Wilson have recently been to 
- USA
#calling function again
travel("Wilson", "USA", "Japan","China")
#output
Wilson have recently been to 
- USA
- Japan
- China

Moreover, if the required information passed to a function is key value pairs, then we can add double asterisk in front of the parameter in function definition. In the following example, both the country and city information are needed for the function calling, and Python packs all the passing information into a dictionary ‘cities’.


#define a function, accepting key value pair information
def travel(person, **cities):
    """Print the country people have traveled"""       
    print(person+ " have recently been to ")
    for key, value in cities.items():
        print("- " + key + ": " + value)
#call function
travel("Wilson", USA = "Boston")
#output
Wilson have recently been to 
- USA: Boston
#call function again
travel("Wilson", USA = "Boston", Japan="Tokyo",China="Shanghai")
#output
Wilson have recently been to 
- USA: Boston
- Japan: Tokyo
- China: Shanghai

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

3 weeks ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

3 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 weeks ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

3 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

12 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago