Writing
Article
AI
Art
Angular
Photoshop
Premiere
Animal Crossing
Blog
Story
Android
Android Studio
Davinci Resolve
CSS
Clipchamp
ChatGPT
Crypto
DALL-E
Discord
Davinci Resolve
Davinci Resolve 18
Dream Studio
Express
Filmora
Flutter
PC Games
Git
GPT
GPT3
GPT4
GTA
GTA 6
Ghost Together
Ghost Together
HTML
iOS
iMovie
Illustrator
JavaScript
Mac
Mongo
Midjourney
Minecraft
Node.js
NBA 2k24
OSX
PHP
Palworld
Python
Playground AI
Roblox
React
Recipe (Cooking)
React Native
Semicolon.dev
Starfield PC Game
Snapchat
Steam
Stable Diffusion
SVG
Threads
Text To Image (AI)
VSCode
Vim
Web Development
Windows
WebGL

Python Function Arguments

Function add is defined with parameters a and b:

def add(a, b):
  return a + b

Arguments are the values passed to a function.

5 and 7 are arguments:

result = add(5, 7)

print(result)

Output:

12

Function Argument Types

In Python there are four function argument types:

  • Positional arguments: arguments that follow specific order.
  • Default arguments: fall back arguments if none provided.
  • Keyword arguments: can rewriting order of positional arguments.
  • Arbitrary arguments: function is defined to take an undetermined number of parameters. Arbitrary parameter name is preceded by star (*) character.

Tags above will be converted to Title and Author info. It's recommended to keep author tag so that readers who come from search engine traffic can also discover your bio.

Note: You can use regular HTML in this article but some things will be removed like JavaScript and script execution.

Positional Arguments

Positional arguments are just the regular arguments.

The function's positional arguments are passed to its parameter names in the same order they appear in function's definition:

def test(number, list, string):
  print(a, b, c)

test(1, ['a', 'x'], 'hello')

Output:

1 ['a', 'x'] hello

Here number, list and string values are passed to the positional argument list in the same order in which they appear in the function's definition.

Default Arguments

Python's default arguments are provided as fall back values in case none were supplied by the function call.

def say(name = "John", message = "Hello"):
  print(f"{name} : {message}")

Running the function with empty argument list:

say()

Output:

John : Hello

The function gracefully falls back on default values. Let's see what happens when you pass partial arguments to the same function. Here we will pass name argument and leave message blank:

say("Steve")

Output:

Steve: Hello

Or you can overwrite both arguments:

say("Steve", "Goodbye")

Output:

Steve : Goodbye

Keyword Arguments

Keyword arguments are almost like default arguments in reverse. You provide them when arguments are being passed to the function and not in its definition:

def say(name, message):
  print(f"{name} : {message}")

To call this function with keyword arguments:

say(name = "John", message = "Hello")

Output is as follows:

John : Hello

Mixing keywords and default arguments

It's possible to mix default and keyword arguments together. But the only proper way of doing that is to prioritize non-keyword parameters in the function definition:

def say(name, message = "Greetings"):

Here positional non-keyword parameter name is listed first without a default value.

This is acceptable and it works as expected. After setting name to "Steve" parameter message is automatically assigned to "Greetings" by default:

Steve : Greetings

Arbitrary Arguments

Arbitrary arguments are used in case when the number of arguments that will be supplied to the function is not known. They are not explicitly defined in function's definition. In some way they can be thought of as optional arguments.

Arbitrary parameters are defined by using the star * character. Not to be mistaken for memory pointer from the C language:

def test(*arb):
  print(arb)

If none of the arguments are passed:

test()

Output will be an empty tuple:

()

The result is an empty tuple which is one of the many data types in Python.

You can iterate over all arguments with a for in loop;

def list(*arguments):
  for value in arguments:
    print(value)

Now let's pass some common animal names:

list("Fox", "Wolf", "Coyote", "Bear", "Rabbit")

Output:

Fox

Wolf

Coyote

Bear

Rabbit

Since arbitrary arguments are iterable you can use for loops on them.

CSS tutorials

CSS grid tutorial learn to code curriculum
Write For Us
Sign Up Now  -  It's Free!

Python Function Arguments

Comments (2) New! (You can now post comments on articles!)

(By posting a comment you are registering a Ghost Together account.)
Register
Register
Post It
DM Coming Soon
f f