Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Keyword Arguments (Complete Guide for Beginners)

In Python, keyword arguments allow you to pass values to a function using parameter names instead of relying on position. This makes your code more readable, flexible, and less error-prone.


🔹 What are Keyword Arguments in Python?

A keyword argument is when you call a function using:

parameter_name=value

Instead of:

just passing values in order


🔹 Syntax of Keyword Arguments

def function_name(param1, param2):
# code block
pass

function_name(param1=value1, param2=value2)

🔹 Simple Example of Keyword Arguments

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

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

🔸 Output:

Name: Alex
Age: 20

🔹 Order Does NOT Matter in Keyword Arguments

def info(name, city):
print(name, city)

info(city="Phnom Penh", name="Sophy")

🔸 Output:

Sophy Phnom Penh

🔍 Explanation:

  • Order is ignored
  • Python matches arguments using parameter names

🔹 Keyword vs Positional Arguments

def show(a, b, c):
print(a, b, c)

show(1, 2, 3) # positional
show(a=1, b=2, c=3) # keyword

🔸 Output:

1 2 3
1 2 3

🔹 Mixing Positional and Keyword Arguments

def product(name, price):
print(name, price)

product("Laptop", price=1200)

🔸 Output:

Laptop 1200

⚠️ Rule:

  • Positional arguments must come first
  • Keyword arguments must come after

🔹 Invalid Example (Wrong Usage)

def demo(a, b):
print(a, b)

demo(a=10, 20)

❌ This will cause an error

🔍 Why?

  • Positional argument (20) comes after keyword argument

🔹 Real-Life Example: User Registration System

def register(name, email, age):
print("Name:", name)
print("Email:", email)
print("Age:", age)

register(name="John", email="john@gmail.com", age=25)

🔸 Output:

Name: John
Email: john@gmail.com
Age: 25

🔹 Real-Life Example: Hotel Booking System

def book_hotel(name, room_type, nights):
print("Guest:", name)
print("Room:", room_type)
print("Nights:", nights)

book_hotel(name="Sokha", room_type="Deluxe", nights=3)

🔹 Using Keyword Arguments for Readability

def create_account(username, password, country):
print(username, country)

create_account(password="1234", country="Cambodia", username="user01")

🔍 Benefit:

  • Easy to understand
  • No confusion about parameter order

🔹 Keyword Arguments with Default Values

def greet(name="Guest", message="Welcome"):
print(message, name)

greet(name="Alex", message="Hello")
greet(message="Hi")

🔸 Output:

Hello Alex
Hi Guest

🔹 **kwargs (Keyword Variable Arguments)

Python also supports unlimited keyword arguments using **kwargs.

def user_info(**data):
print(data)

user_info(name="John", age=30, city="Phnom Penh")

🔸 Output:

{'name': 'John', 'age': 30, 'city': 'Phnom Penh'}

🔹 Real-Life Example of **kwargs

def profile(**details):
for key, value in details.items():
print(key, ":", value)

profile(name="Anna", job="Developer", country="USA")

🔹 Why Use Keyword Arguments?

✔ Improves code readability
✔ Avoids mistakes in parameter order
✔ Makes function calls clearer
✔ Easier maintenance
✔ Supports flexible function design


🔹 Keyword vs Positional Arguments

FeaturePositionalKeyword
Order matters           Yes               No
Readability           Low               High
Syntax           values only               name=value

🚀 Conclusion

Python keyword arguments make functions easier to understand and use. Instead of worrying about order, you can directly specify parameter names, making your code clean and professional.

They are widely used in:

  • APIs
  • Web development
  • Data processing
  • Real-world applications

 




Post a Comment

0 Comments