Tuesday, 28 July 2026

πŸš€ Day 92/150 – Function with Default Arguments in Python

 

πŸš€ Day 92/150 – Function with Default Arguments in Python

Default arguments allow you to assign a default value to a function parameter. If the caller doesn't provide a value for that parameter, Python automatically uses the default value. This makes functions more flexible and easier to use.

In this post, we'll explore four different ways to use default arguments in Python.


Method 1 – Basic Default Argument

Assign a default value to a function parameter.

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






Output

Hello, Guest 
Hello, Alice

Explanation

  • The parameter name has a default value of "Guest".
  • Calling greet() uses the default value.
  • Calling greet("Alice") replaces the default value with "Alice".

Method 2 – Multiple Default Arguments

A function can have more than one default argument.

def student(name="Unknown", age=18): print("Name:", name) print("Age:", age) student() student("John", 20)







Output
Name: Unknown
Age: 18

Name: John
Age: 20

Explanation

  • Both parameters have default values.
  • If no arguments are provided, the defaults are used.
  • Passing values overrides the defaults.

Method 3 – Mix of Required and Default Arguments

A function can have both required and default parameters.

def power(number, exponent=2): print(number ** exponent) power(5) power(5, 3)






Output
25
125

Explanation
  • number is required.
  • exponent has a default value of 2.
  • power(5) calculates 5².
  • power(5, 3) calculates 5³.

Method 4 – Taking User Input

Use user input with a function that has a default argument.

def welcome(name="Guest"): print("Welcome,", name) name = input("Enter your name (press Enter to skip): ") if name: welcome(name) else: welcome()





Sample Input

Sam

Output

Welcome, Sam

Sample Input

Output

Welcome, Guest

Explanation
  • If the user enters a name, it is passed to the function.
  • If the user presses Enter without typing anything, the default value "Guest" is used.

Comparison of Methods

MethodBest For
Basic Default ArgumentSimple functions
Multiple Default ArgumentsFunctions with several optional values
Required + Default ArgumentsFlexible function design
User InputInteractive programs

πŸ”₯ Key Takeaways

  • Default arguments provide values that are used when no argument is passed.
  • They make functions more flexible and reduce the need to pass common values repeatedly.
  • Required parameters must come before default parameters in a function definition.
  • Default arguments can be overridden by passing your own values.
  • They are commonly used to simplify function calls and improve code readability.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (321) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (309) Bootcamp (13) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (33) data (10) Data Analysis (42) Data Analytics (31) data management (16) Data Science (409) Data Strucures (23) Deep Learning (206) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (12) flask (4) flutter (1) FPL (17) Generative AI (77) Git (12) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (364) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1415) Python Coding Challenge (1206) Python Mathematics (8) Python Mistakes (51) Python Quiz (583) Python Tips (27) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)