Title: Using Python Optional Arguments When Defining Functions – Real Python
Open Graph Title: Using Python Optional Arguments When Defining Functions – Real Python
Description: Learn to use Python's optional arguments to handle variable inputs. Build flexible functions and avoid common errors when setting defaults.
Open Graph Description: Learn to use Python's optional arguments to handle variable inputs. Build flexible functions and avoid common errors when setting defaults.
Mail addresses
?subject=Python article for you&body=Using Python Optional Arguments When Defining Functions on Real Python
https://realpython.com/python-optional-arguments/
Opengraph URL: https://realpython.com/python-optional-arguments/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "Using Python Optional Arguments When Defining Functions",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/How-to-Create-Python-Functions-with-Optional-Arguments_Watermarked.f2c8b582aff0.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/python-optional-arguments/",
"lastReviewed": "2025-10-27",
"author": {
"@type": "Person",
"name": "Stephen Gruppetta",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Stephen_inside_2_BW_2_square_crop_2_low_res_2_copy.4a7e2d8bc19c.png",
"url": "https://realpython.com/team/sgruppetta/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Aldren Santos",
"image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
"url": "https://realpython.com/team/asantos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Bartosz Zaczy\u0144ski",
"image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
"url": "https://realpython.com/team/bzaczynski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Joanna Jablonski",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jjablonksi-avatar.e37c4f83308e.jpg",
"url": "https://realpython.com/team/jjablonski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Jacob Schmitt",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/profile-small_js.2f4d0d8da1ca.jpg",
"url": "https://realpython.com/team/jschmitt/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Leodanis Pozo Ramos",
"image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
"url": "https://realpython.com/team/lpozoramos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Martin Breuss",
"image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
"url": "https://realpython.com/team/mbreuss/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2025-10-27T14:00:00+00:00",
"dateModified": "2025-10-27T14:09:22.730538+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Stephen Gruppetta",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Stephen_inside_2_BW_2_square_crop_2_low_res_2_copy.4a7e2d8bc19c.png",
"url": "https://realpython.com/team/sgruppetta/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "Learn to use Python's optional arguments to handle variable inputs. Build flexible functions and avoid common errors when setting defaults.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between parameters and arguments in Python functions?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You define parameters when you write a function, and you provide arguments when you call it. Parameters are names inside the function definition, while arguments are the actual values you pass in.
"
}
},
{
"@type": "Question",
"name": "Why should you avoid using mutable default arguments like lists or dictionaries?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You should avoid mutable defaults because Python reuses the same object across calls. This can lead to unexpected behavior when changes persist between function calls.
"
}
},
{
"@type": "Question",
"name": "How do args and kwargs make functions more flexible?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You use *args to accept any number of positional arguments and **kwargs to accept any number of keyword arguments. This makes your functions flexible because they can handle inputs of varying size without rewriting code.
"
}
},
{
"@type": "Question",
"name": "What happens if you misorder required and optional parameters in a function definition?",
"acceptedAnswer": {
"@type": "Answer",
"text": "If you place a parameter with a default value before one without, then Python raises a SyntaxError. Required parameters must always come before optional ones.
"
}
},
{
"@type": "Question",
"name": "When should you use None as a default value instead of a mutable object?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You use None when you want to create a new object inside the function each time it runs. This prevents functions from sharing the same mutable default object between calls.
"
}
}
]
}
}
| author | Real Python |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/How-to-Create-Python-Functions-with-Optional-Arguments_Watermarked.f2c8b582aff0.jpg |
| og:image | https://files.realpython.com/media/How-to-Create-Python-Functions-with-Optional-Arguments_Watermarked.f2c8b582aff0.jpg |
| twitter:creator | @realpython |
| og:type | article |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large