Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Python: Function Arguments

Profile picture for user devanshi.srivastava
Written by devanshi.srivastava on 07/06/2021 - 11:53

In Python, function that takes variable number of arguments. Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. An argument is the value that are sent to the function when it is called. We can add as many arguments as we want, just separate them with a comma.

There are other ways to define a function that can take variable number of arguments, three different forms of this type are described below:

Default Argument in Python

Function arguments can have default values in Python. We can assign a default value to an argument using the assignment operator in python(=).

When we call a function without a value for an argument, its default value is used.

Example:

def name(name,msg="Good Morning "):
    print("Hello",name+','+msg)
    
name("abc")
name("xyz","How are you?")

Output:

Hello abc,Good Morning 
Hello xyz,How are you?

In this example, the parameter name does not have a default value and is required during a call.

On the other hand, the parameter msg has a default value of "Good Morning".  So, it is optional during a call. If a value is provided, it will overwrite the default value.

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

Python Keyword Arguments

When we call a function with some values, these values get assigned to the arguments according to their position.

With keyword arguments in python, we can change the order of passing the arguments without any consequences.

Example:

def add(a, b):
    return a + b

print(add(a=3, b=4))

Output: 7

We can call this function with arguments in any order, as long as we specify which value goes into what.

add(b = 3,a = 4)

Output: 7

These are keyword python function arguments.

Python Arbitrary Arguments

In Python, sometimes we do not know in advance the number of arguments that will be passed into a function. It allows us to handle this kind of situation through function calls with an arbitrary number of arguments.

In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument.

Example:

def func_1(*names):
    for name in names:
        print("Good Morning !!",name)

func_1("A", "B", "C")

Output:

Good Morning !! A
Good Morning !! B
Good Morning !! C

This was all about the Python Function Arguments.

Related Content
Python Tutorial
Python: Types of Function
Python Function: The return Statement
Tags
Python
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use