Python
Module 6: Python FunctionsBeginner18 min read

Defining Functions & Parameters

Create reusable code blocks with def, default arguments, keyword arguments, and docstrings.

Key Takeaways & Core Concepts
  • discount_percent) / 100

Defining Functions & Parameters

### Python Functions

A function is a block of reusable code that only runs when called. You can pass data (parameters) into a function, and it can return data as a result.

PYTHON Code Example
1def calculate_discount(price, discount_percent=10):
2 """Calculates discounted amount with default 10% discount."""
3 savings = (price * discount_percent) / 100
4 final_price = price - savings
5 return final_price
6
7result = calculate_discount(2000, 25)
8print(f"Final Price: ₹{result}")
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
8 lines

#### Default Arguments Parameters can have default values. If an argument is omitted during invocation, Python uses the default value automatically.

Greeting Function with Default
1def greet(name, platform="CodeZov"):
2 return f"Hello {name}, welcome to {platform}!"
3
4print(greet("Karan"))
5print(greet("Sara", "Advanced AI Lab"))
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
5 lines
Note: The second parameter defaults to 'CodeZov' when not explicitly provided.
Practice & AssessmentPython Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)