Header Ads Widget

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

Python Default Arguments (Complete Guide for Beginners)

In Python, default arguments allow a function to have predefined values for parameters. If the caller does not provide a value, Python automatically uses the default one.

This makes functions more flexible and reduces the need to pass arguments every time.


🔹 What are Default Arguments in Python?

A default argument is a parameter that already has a value assigned in the function definition.

👉 If you don’t pass a value → default is used
👉 If you pass a value → it overrides the default


🔹 Syntax of Default Arguments

def function_name(parameter=default_value):
# code block

🔹 Simple Example of Default Argument

def greet(name="Guest"):
print("Hello", name)

greet()
greet("Coco")

🔸 Output:

Hello Guest
Hello Coco

🔍 Explanation:

  • First call uses default value "Guest"
  • Second call overrides it with "Coco"

🔹 Multiple Default Arguments

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

student()
student("Alex", 20)

🔸 Output:

Name: Unknown
Age: 0

Name: Alex
Age: 20

🔹 Default + Non-Default Arguments

def info(name, country="Cambodia"):
print(name, "is from", country)

info("John")
info("Sophy", "USA")

🔸 Output:

John is from Cambodia
Sophy is from USA

🔍 Explanation:

  • name → required argument
  • country → default argument

🔹 Important Rule: Order Matters ⚠️

❌ Wrong:

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

✔ Correct:

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

demo(5)

🔍 Rule:

Default arguments must come after non-default arguments


🔹 Real-Life Example: Login System

def login(username, role="user"):
print("Username:", username)
print("Role:", role)

login("admin")
login("admin", "superuser")

🔸 Output:

Username: admin
Role: user

Username: admin
Role: superuser

🔹 Real-Life Example: Calculator App

def multiply(a, b=2):
return a * b

print(multiply(5))
print(multiply(5, 10))

🔸 Output:

10
50

🔍 Explanation:

  • Default multiplier is 2
  • You can override it when needed

🔹 Default Arguments in Functions with Lists

def show_items(items=["apple", "banana"]):
for item in items:
print(item)

show_items()
show_items(["mango", "orange"])

🔸 Output:

apple
banana

mango
orange

🔹 ⚠️ Common Mistake with Mutable Defaults

❌ Wrong practice:

def add_item(item, my_list=[]):
my_list.append(item)
return my_list

print(add_item("A"))
print(add_item("B"))

🔍 Problem:

  • Default list is reused every time
  • Causes unexpected results

✔ Correct way:

def add_item(item, my_list=None):
if my_list is None:
my_list = []
my_list.append(item)
return my_list

print(add_item("A"))
print(add_item("B"))

🔹 Why Use Default Arguments?

✔ Makes functions flexible
✔ Reduces repetitive code
✔ Improves readability
✔ Provides fallback values
✔ Easier function calls


🔹 Default Arguments vs Required Arguments

FeatureDefault ArgumentRequired Argument
Value              Predefined                  Must be provided
Flexibility              High                  Low
Example        name="Guest"          name

🚀 Conclusion

Python default arguments are very useful for writing flexible and clean functions. They allow you to set fallback values and make your code easier to use and maintain.

Once you master default arguments, you can build more powerful and user-friendly Python applications. 




Post a Comment

0 Comments