Important Notice:

Keyword Arguments in Python

Keyword Arguments in Python

17 views 2 min read

Keyword Arguments in Python :-

Python में Keyword Arguments वे arguments होते हैं जिन्हें function call करते समय parameter के नाम के साथ value देकर pass किया जाता है। इसमें argument की value, parameter के नाम से identify होती है।

 in English

Keyword arguments are arguments passed to a function by explicitly specifying the parameter names along with their values during the function call.

Example

def student(name, age):
    print("Name:", name)
    print("Age:", age)

student(name="Rahul", age=20)

Output:

Name: Rahul
Age: 20

Explanation:

  • name="Rahul" एक keyword argument है।

  • age=20 भी एक keyword argument है।

  • Parameter names (name और age) के माध्यम से values pass की गई हैं।

 Using Keyword Arguments

Function call करते समय parameter का नाम लिखकर और उसके बाद = operator की सहायता से value देना Using Keyword Arguments कहलाता है।

 in English

Using keyword arguments means passing values to a function by specifying the parameter names in the function call.

Syntax function_name(parameter1=value1, parameter2=value2) Example 1: Using Keyword Arguments

def display(name, course, city):
    print("Name:", name)
    print("Course:", course)
    print("City:", city)

display(name="Rahul", course="Python", city="Azamgarh")

Output:

Name: Rahul
Course: Python
City: Azamgarh

Example 2: Changing the Order of Arguments

def student(name, age, course):
    print("Name:", name)
    print("Age:", age)
    print("Course:", course)

student(course="O Level", name="Amit", age=21)

Output:

Name: Amit
Age: 21
Course: O Level

Explanation:

इस example में arguments का order function definition से अलग है। फिर भी सही output मिलता है क्योंकि प्रत्येक value उसके parameter name से जुड़ी है।

Example 3: Using Keyword Arguments with Default Values

def student(name, course="Python", city="Azamgarh"):
    print("Name:", name)
    print("Course:", course)
    print("City:", city)

student(name="Rahul", course="O Level")

Output:

Name: Rahul
Course: O Level
City: Azamgarh

Explanation:

  • name="Rahul" से name की value दी गई।

  • course="O Level" से course की default value बदल गई।

  • city के लिए कोई value नहीं दी गई, इसलिए उसकी default value "Azamgarh" उपयोग हुई।

Important Rule: Positional arguments को keyword arguments से पहले रखना चाहिए।

Incorrect Example:

def student(name, age):
    print(name, age)

student(name="Rahul", 20)

 

Output:

SyntaxError: positional argument follows keyword argument

Correct Example:

def student(name, age):
    print(name, age)

student("Rahul", 20)

Output:

Rahul 20

या:

student(name="Rahul", age=20)

Output:

Rahul 20

Positional vs Keyword Arguments :- Example: Positional Arguments

def student(name, age):
    print("Name:", name)
    print("Age:", age)

student("Rahul", 20)

Output:

Name: Rahul
Age: 20

यहाँ "Rahul" पहले parameter name और 20 दूसरे parameter age को assign होता है।

Example: Keyword Arguments

def student(name, age):
    print("Name:", name)
    print("Age:", age)

student(age=20, name="Rahul")

Output:

Name: Rahul
Age: 20

यहाँ arguments का order बदल गया है, लेकिन parameter names के कारण values सही parameters को assign हुई हैं।

 

Related Notes