Python Program on Function Arguments
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Embarking on the practical aspects of Python, our focus shifts to understanding Python function arguments and their types. In simple terms, function arguments are the values passed to a function, influencing its behavior. Whether it’s specifying values in a specific order or explicitly identifying them by name, Python functions handle varied inputs. Our exploration aims to demystify the various argument types, offering clarity on how Python functions can flexibly receive and handle input.
Topic Explanation:
In Python, function arguments come in different types, with the most common being positional and keyword arguments. Positional arguments are values passed to a function in a specific order, while keyword arguments are explicitly identified by their parameter names. This flexibility allows functions to handle varied inputs. Additionally, default values can be assigned to parameters, ensuring smooth execution even when certain arguments are omitted.
As we delve deeper, we explore the use of variable-length argument lists, denoted by ‘*’ and ‘**’ symbols, providing a dynamic way to handle varying numbers of inputs. This versatility in argument handling contributes to the adaptability and robustness of Python functions.
Prerequisite:
- Basic understanding of Python syntax and functions.
- Familiarity with variable assignments and data types in Python.
Code 1 With Comments:
# Importing a module named mymodule using an alias 'my'
# n is assigned the user-input integer value
# The reversenum function from mymodule is called with n as an argument
import mymodule as my
n = int(input("Enter a number"))
my.reversenum(n)
# Nested functions example
# A function display(s) is defined, which contains a nested function msg()
# The msg function returns "Data Flair"
# display returns the concatenation of the result of msg() and the input string s
s1 = display("Free Courses")
print(s1)
# Function display(f) is defined, which returns the concatenation of "Hello" and the input parameter f
# Another function msg() is defined, which returns the string "Friends"
# The display function is called with the result of msg() as the argument, and the result is stored in x
# Finally, x is printed
def display(f):
return "Hello" + f
def msg():
return "Friends"
x = display(msg())
print(x)Code 1 Output:
HelloFriends
Code 1 Explanation:
- The first part imports a module named mymodule with the alias my.
- User input is taken as an integer n, and the reversenum function from the imported module is called.
- The second part demonstrates nested functions. The display function contains a nested function msg(), and the result of the concatenation is printed.
- In the third part, another display function is defined, and a separate msg function returns a string.
- The display function is called with the result of msg() as an argument, and the final output is printed. The output will be “HelloFriends”.
Code 2 With Comments:
# Definition of a function named myfactorial that takes an integer parameter n
def myfactorial(n):
f = 1 # Initializing a variable f to 1
while n != 0:
f = f * n
n = n - 1
print("Factorial is %d " % f) # Printing the factorial result
# Definition of a function named reversenum that takes an integer parameter n
def reversenum(n):
s = 0 # Initializing a variable s to 0
while n != 0:
r = n % 10
s = s * 10 + r
n = n // 10
print("Reverse of number is %d " % s) # Printing the reversed number result
# Note: The code doesn't include any libraries, and the keywords used are standard Python syntax.Output for Code 2:
Inputs Given:
myfactorial(5)
reversenum(123)
Output based on above input:
Factorial is 120
Reverse of number is 321
Code 2 Explanation:
- Two functions are defined: myfactorial and reversenum.
- myfactorial calculates the factorial of a given number using a while loop and prints the result.
- reversenum reverses the digits of a number using a while loop and prints the reversed number.
- Both functions use basic arithmetic operations and control flow statements (while loop, assignment, etc.).
- The code does not involve any external libraries. The functions are standalone and can be called with appropriate parameters.
Code 3 With Comments:
# Definition of a function named addition with default arguments
def addition(a, b=600, c, d=50):
z = a + b + c + d
print("Addition is %d" % z)
# Calling the function with specific arguments
addition(1, 2, 3, 4)
addition(1, 2, 3)
addition(1, 2)Output For code 3:
Addition is 10
Addition is 656
Addition is 653
Code 3 Explanation:
- The addition function is defined with four parameters (a, b, c, and d).
- Default values are assigned to some of the parameters: b is set to 600, and d is set to 50.
- Inside the function, the sum of all parameters is calculated and printed.
- The function is called three times with different combinations of arguments.
- The first call has all four arguments specified (1, 2, 3, 4).
- The second call has three arguments, and the default value for d (50) is used.
- The third call has two arguments, and default values are used for b and d.
Code 4 With Comments:
# Definition of a function named stud_info with keyword arguments
def stud_info(rollno, name, course):
print("Roll no is ", rollno)
print("Name is ", name)
print("Course is ", course)
# Calling the function with keyword arguments
stud_info(name="Rahul", course="B.Tech", rollno=101)Output For Code 4:
Roll no is 101
Name is Rahul
Course is B.Tech
Code 4 Explanation:
- The stud_info function is defined with three parameters (rollno, name, and course).
- When calling the function, the arguments are specified using keywords (name=”Rahul”, course=”B.Tech”, rollno=101).
- The order of the arguments in the function call doesn’t matter because they are explicitly matched with parameter names.
- This usage of keyword arguments enhances code readability and allows skipping default values for unspecified parameters.
Code 5 With Comments:
# Definition of a function named addition with variable-length arguments (*args)
def addition(*args):
for m in args:
print(m)
# Calling the function with variable-length arguments
addition(10, 20, 30, "Hello", "Data-Flair")Code 5 Output:
10
20
30
Hello
Data-Flair
Code 5 Explanation:
- The addition function is defined with variable-length arguments denoted by *args.
- Inside the function, a loop iterates through all the arguments passed and prints each one.
- This function can accept any number of arguments, and they are accessed as a tuple named args.
Conclusion:
Concluding our exploration into Python function arguments, we’ve uncovered the versatility and adaptability they bring to function design. Positional, keyword, default, and variable-length arguments collectively provide a powerful toolkit for handling diverse input scenarios. The ability to tailor function behavior based on different argument types enhances code readability and flexibility. As we reflect on this topic, the understanding of Python function arguments becomes integral to writing efficient and adaptable code, offering a valuable skill set for Python developers.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

