Programming Essentials Python - User Defined Functions - Keyword Arguments

Introduction to Keyword Arguments in Python

Description Paragraph
This article provides a comprehensive guide on understanding and using keyword arguments in Python programming. From basic concepts to hands-on tasks, this article will help readers grasp the essence of keyword arguments and implement them effectively in their Python code.

Explanation for the video
The video linked in this article delves deeper into the concept of keyword arguments in Python. It provides visual demonstrations and explanations to complement the text and enhance the learning experience for the readers.

Key Concepts Explanation

Keyword Argument

A keyword argument in Python is similar to passing arguments by name. It allows you to specify parameters for varying keyword arguments using ** at the beginning. When passing arguments to satisfy the parameter with **, both the key and value must be provided.

def add_employee(employee_id, **degrees):
    print(f'Length of degrees is: {len(degrees)}')
    print(f'Type of degrees is: {type(degrees)}')
    print(degrees)

add_employee(1, bachelors='B. Sc', masters='M. C. A')

Varying Keyword Arguments

Varying Keyword Arguments can be processed as a dictionary in the function body, enabling flexibility and dynamic keyword argument handling in Python functions.

def add_employee(employee_id, *phone_numbers, **degrees):
    print(f'Length of phone_numbers is: {len(phone_numbers)}')
    print(f'Type of phone_numbers is: {type(phone_numbers)}')
    print(phone_numbers)    
    print(f'Length of degrees is: {len(degrees)}')
    print(f'Type of degrees is: {type(degrees)}')
    print(degrees)

add_employee(1, '1234567890', '1234567890', bachelors='B. Sc', masters='M. C. A')

Hands-On Tasks

Explore the following hands-on tasks to deepen your understanding of keyword arguments in Python:

  1. Define a function that accepts varying keyword arguments and prints out the key-value pairs.
  2. Call the function with different sets of keyword arguments to see how the function processes them.

Conclusion

In conclusion, mastering keyword arguments in Python is essential for writing flexible and reusable code. By understanding the key concepts and practicing with hands-on tasks, you can leverage the power of keyword arguments to enhance your Python programming skills.

Keyword Arguments

Let us go through the details related to Keyword Arguments using Python as Programming Language.

  • Keyword Argument is same as passing argument by name.

  • You can also specify parameter for varying keyword arguments with ** at the beginning - example: **degrees

  • While passing arguments to satisfy the parameter with **, you have to pass key as well as value.

  • Varying Keyword Arguments can be processed as dict in the Function body.

def add_employee(employee_id, **degrees):

    print(f'Length of degrees is: {len(degrees)}')

    print(f'Type of degrees is: {type(degrees)}')

    print(degrees)
add_employee(1, bachelors='B. Sc', masters='M. C. A')
degrees = {'bachelors': 'B. Sc', 'masters': 'M. C. A'}

add_employee(1, **degrees)
def add_employee(employee_id, *phone_numbers, **degrees):

    print(f'Length of phone_numbers is: {len(phone_numbers)}')

    print(f'Type of phone_numbers is: {type(phone_numbers)}')

    print(phone_numbers)    

    print(f'Length of degrees is: {len(degrees)}')

    print(f'Type of degrees is: {type(degrees)}')

    print(degrees)
add_employee(1, '1234567890', '1234567890', bachelors='B. Sc', masters='M. C. A')

Watch the video tutorial here