Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Combination of Positional and Optional Arguments | Positional and Optional Arguments
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Functions Tutorial

bookCombination 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 follow the order in the function definition. In calculate_smartphone_cost, model and price are required positional arguments.

Optional (Named) Arguments

Optional arguments may be passed positionally or by name. quantity and discount have default values that can be changed using named parameters.

Default Values

If an optional argument is omitted, its default value is used. In the example, quantity defaults to 1 and discount to 0.

Named Parameters

Named parameters improve clarity by explicitly assigning values, especially when several optional arguments exist.

These rules show how combining positional and named arguments keeps functions flexible and readable.

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?

question mark

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

Select the correct answer

question mark

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Β 2. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to use named parameters in this function?

What happens if I omit the optional arguments?

Can you show more examples with different argument combinations?

bookCombination of Positional and Optional Arguments

Swipe to show menu

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 follow the order in the function definition. In calculate_smartphone_cost, model and price are required positional arguments.

Optional (Named) Arguments

Optional arguments may be passed positionally or by name. quantity and discount have default values that can be changed using named parameters.

Default Values

If an optional argument is omitted, its default value is used. In the example, quantity defaults to 1 and discount to 0.

Named Parameters

Named parameters improve clarity by explicitly assigning values, especially when several optional arguments exist.

These rules show how combining positional and named arguments keeps functions flexible and readable.

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?

question mark

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

Select the correct answer

question mark

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Β 2. ChapterΒ 5
some-alt