Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Unknown Number of Arguments | An Unknown Number of Arguments
Python Functions: From Zero to Hero
course content

Зміст курсу

Python Functions: From Zero to Hero

Python Functions: From Zero to Hero

1. What is a Function?
2. Arguments or Parameters
3. Assigning a Default Value to a Parameter
4. An Unknown Number of Arguments
5. Functions within Functions
6. Recursion

bookUnknown Number of Arguments

We used to work with functions when we knew how many parameters we were working with. What if we don't know exactly how many arguments we will send. Let's say we sometimes want to buy bananas, but not always. Let's look at the following example for this.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples=10, price_of_carrots=7, price_of_oranges=12, price_of_bananas=13)
copy

If we run the code above, we get a TypeError error, which is normal because this function expects 3 arguments, but gets 4 arguments instead, here the price of bananas is an unexpected argument.

However, functions in Python can handle an unknown number of arguments. This can be done in two different ways. We'll look at each of them separately in the following chapters.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 1
toggle bottom row

bookUnknown Number of Arguments

We used to work with functions when we knew how many parameters we were working with. What if we don't know exactly how many arguments we will send. Let's say we sometimes want to buy bananas, but not always. Let's look at the following example for this.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples=10, price_of_carrots=7, price_of_oranges=12, price_of_bananas=13)
copy

If we run the code above, we get a TypeError error, which is normal because this function expects 3 arguments, but gets 4 arguments instead, here the price of bananas is an unexpected argument.

However, functions in Python can handle an unknown number of arguments. This can be done in two different ways. We'll look at each of them separately in the following chapters.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 1
toggle bottom row

bookUnknown Number of Arguments

We used to work with functions when we knew how many parameters we were working with. What if we don't know exactly how many arguments we will send. Let's say we sometimes want to buy bananas, but not always. Let's look at the following example for this.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples=10, price_of_carrots=7, price_of_oranges=12, price_of_bananas=13)
copy

If we run the code above, we get a TypeError error, which is normal because this function expects 3 arguments, but gets 4 arguments instead, here the price of bananas is an unexpected argument.

However, functions in Python can handle an unknown number of arguments. This can be done in two different ways. We'll look at each of them separately in the following chapters.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

We used to work with functions when we knew how many parameters we were working with. What if we don't know exactly how many arguments we will send. Let's say we sometimes want to buy bananas, but not always. Let's look at the following example for this.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples=10, price_of_carrots=7, price_of_oranges=12, price_of_bananas=13)
copy

If we run the code above, we get a TypeError error, which is normal because this function expects 3 arguments, but gets 4 arguments instead, here the price of bananas is an unexpected argument.

However, functions in Python can handle an unknown number of arguments. This can be done in two different ways. We'll look at each of them separately in the following chapters.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt