Optional Arguments
The positional arguments are required to be filled in. However, you can assign a default value to the positional argument using the =
operator:
1234567def user_data(name="UnknownName", surname="UnknownSurname"): print("Name:", name) print("Surname:", surname) user_data("John", "Smith") user_data("John") user_data()
In the example above, if you don't pass a value to the name
argument, it will be assigned the default value "UnknownName"
. The default value "UnknownSurname"
and the surname
argument work similarly.
The default value makes the corresponding positional arguments optional, but the total number of positional arguments required to be passed remains strict.
12345def user_data(name="UnknownName", surname="UnknownSurname"): print("Name:", name) print("Surname:", surname) user_data("John", "Smith", "18 years old")
In the next chapter, we will describe how to define a function that can take a variable number of arguments.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Запитайте мені питання про цей предмет
Сумаризуйте цей розділ
Покажіть реальні приклади
Awesome!
Completion rate improved to 4.35
Optional Arguments
Свайпніть щоб показати меню
The positional arguments are required to be filled in. However, you can assign a default value to the positional argument using the =
operator:
1234567def user_data(name="UnknownName", surname="UnknownSurname"): print("Name:", name) print("Surname:", surname) user_data("John", "Smith") user_data("John") user_data()
In the example above, if you don't pass a value to the name
argument, it will be assigned the default value "UnknownName"
. The default value "UnknownSurname"
and the surname
argument work similarly.
The default value makes the corresponding positional arguments optional, but the total number of positional arguments required to be passed remains strict.
12345def user_data(name="UnknownName", surname="UnknownSurname"): print("Name:", name) print("Surname:", surname) user_data("John", "Smith", "18 years old")
In the next chapter, we will describe how to define a function that can take a variable number of arguments.
Дякуємо за ваш відгук!