Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Function arguments

Function arguments are the values a call passes in, by position or by name, with defaults that make some of them optional.

Last updated: 30 Sep, 2026 · Python 3.14

The functions so far took one value. A cost estimate needs the token count every time, but the price only when it differs from the usual one.

Default values, *args and **kwargs · from Tutorial 12- Python Functions, Positional and Keywords Arguments · 5:41 to 11:04
The video builds its message with .format(); the run below uses an f-string from Strings, which gives the same text. It also calls age=29 in the definition a keyword argument: in a definition it is a default value, and positional or keyword describes how a call passes a value.
ExampleFrom the video, run here
def hello(name, age=29):
    print(f"My name is {name} and age is {age}")

hello("Krish")

def hello(*args, **kwargs):
    print(args)
    print(kwargs)

hello("Krish", "Naik", age=29, dob=1990)

hello("Krish") left out age, so the default 29 was used. In the second version, *args collected the values passed by position into a tuple and **kwargs collected the named ones into a dictionary. The cost function below uses a default the same way.

Syntax:

python
def name(required, optional=default):
    ...

name(1)                  # by position
name(1, optional=2)      # by keyword

Giving a parameter a default

Example
def cost(tokens, price_per_1000=0.15):
    return round(tokens / 1000 * price_per_1000, 4)

print(cost(1200))
print(cost(1200, 2.5))
print(cost(1200, price_per_1000=2.5))

The value passed in a call is an argument. price_per_1000=0.15 in the definition means: use 0.15 when the caller does not pass one.

The last call names the argument. A keyword argument says what the value is for, which reads better than a bare 2.5 and lets you pass arguments in any order.

Leaving out a required argument

Example
print(cost())

tokens has no default, so a call must give it. The message names the function and the argument that is missing. Delete that line before you add the next example: an error stops the file where it happens.

Collecting extra settings with **

Model calls take many optional settings, and a function that wraps one often passes them on without knowing their names. Two stars in front of a parameter collect every extra keyword argument into a dictionary.

Example
def call_model(prompt, **settings):
    print("prompt:", prompt)
    print("settings:", settings)

call_model("Sort this ticket")
call_model("Sort this ticket", temperature=0.2, max_tokens=50)

temperature and max_tokens are two settings most model APIs accept: how varied the answer is, and how long it may be. Here they only land in settings, but you will see **kwargs, the usual name, in nearly every framework's source.

Positional vs keyword arguments

PositionalKeyword
Callcost(1200, 2.5)cost(1200, price_per_1000=2.5)
Order mattersYesNo
Reads asNumbers you must look upSays what each value is for

Where arguments show up in AI code

  • Model calls with many optional settings, passed by keyword: temperature=0, max_tokens=50.
  • Wrapper functions that pass **settings straight on to the library they wrap.
Watch out. A keyword argument cannot come before a positional one in a call. cost(price_per_1000=2.5, 1200) is a SyntaxError; put the positional values first.
Try it yourself
  • Give cost a third parameter currency="USD" and return an f-string with it.
  • Call cost(price_per_1000=2.5, tokens=1200).
  • Print settings.get("temperature", 1.0) inside call_model.
PreviousFunctions

Slow is fine. Stopping is the only problem.