Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Combination of Positional and Optional Arguments | Arbitrary Arguments
Python Functions Tutorial
course content

Course Content

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Combination of Positional and Optional Arguments

Consider a function designed to calculate the total cost of smartphones, allowing you to specify various attributes during both initialization and function invocation.

123456789101112131415161718192021222324
def calculate_smartphone_cost(model, price, quantity=1, discount=0): total_cost = price * quantity discount_amount = total_cost * (discount / 100) discounted_cost = total_cost - discount_amount print(f"Model: {model}") print(f"Unit price: ${price}") print(f"Quantity: {quantity}") print(f"Total cost before discount: ${total_cost}") if discount > 0: print(f"Discount: {discount}%") print(f"Discount amount: ${discount_amount}") print(f"Discounted cost: ${discounted_cost}") else: print("No discount applied.") print(f"Final cost: ${discounted_cost}") print() # Examples of using the function calculate_smartphone_cost("iPhone 13", 1099, 2) calculate_smartphone_cost("Samsung Galaxy S21", 999, 1, 10) calculate_smartphone_cost("Google Pixel 6", 799, quantity=3, discount=5)
copy

Rules for Specifying Arguments

Positional Arguments

Positional arguments must be specified in the order they are defined in the function declaration. For example, in the function calculate_smartphone_cost, model and price are mandatory positional arguments.

Optional (Named) Arguments

Optional arguments can be specified either positionally or using named parameters. In the example, quantity and discount are optional arguments with default values that can be altered using named parameters during function invocation.

Default Values

If an optional argument is not specified during function call, its default value will be used. For instance, in the calculate_smartphone_cost function, if quantity and discount are not provided, they automatically take the values 1 and 0, respectively.

Named Parameters

Named parameters allow precise specification of values for optional arguments and determine their order of appearance. This enhances code clarity and readability, especially when dealing with multiple optional parameters.

This example and the rules for specifying arguments illustrate how to effectively use a combination of positional and named (optional) arguments to create functions that offer flexibility and ease of use, while maintaining code clarity and expected behavior.

1. What is the combination of positional and optional arguments in functions?
2. How do you define a function with positional arguments followed by optional arguments?
What is the combination of positional and optional arguments in functions?

What is the combination of positional and optional arguments in functions?

Select the correct answer

How do you define a function with positional arguments followed by optional arguments?

How do you define a function with positional arguments followed by optional arguments?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt