{"id":4751,"date":"2024-03-17T21:56:24","date_gmt":"2024-03-17T16:26:24","guid":{"rendered":"https:\/\/cbsepython.in\/?p=4751"},"modified":"2026-01-05T13:47:34","modified_gmt":"2026-01-05T08:17:34","slug":"python-function-arguments-and-parameters-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/python-function-arguments-and-parameters-notes\/","title":{"rendered":"Python Function Arguments and Parameters Notes Class 12"},"content":{"rendered":"<h2>Python Function Arguments and Parameters Notes (Positional, Keyword, Default)<\/h2>\n<p><strong data-start=\"92\" data-end=\"136\">Python function arguments and parameters<\/strong> are important concepts frequently asked in programming exams and practical assessments. They define how values are passed to functions and how functions process data. This topic covers <strong data-start=\"322\" data-end=\"398\">positional arguments, keyword arguments, and default arguments in Python<\/strong>, explained in a simple and exam-oriented manner to help students understand, remember, and apply them correctly in coding questions.<\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In Python, arguments are values passed to a function when it is called. There are different types of arguments that can be used in Python functions:<\/span><\/p>\n<p><strong><span style=\"color: #000000; font-family: georgia, palatino, serif;\">1. Positional Arguments:<\/span><\/strong><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">These are the most common type of arguments in Python.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">They are passed to a function based on their position.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">The order of arguments matters.<\/span><\/li>\n<li style=\"list-style-type: none;\"><\/li>\n<\/ul>\n<p align=\"left\"><strong><span style=\"color: #000000; font-family: georgia, palatino, serif;\"><span style=\"font-size: medium;\">2. Keyword Arguments:<\/span><\/span><\/strong><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In this type of argument, each argument is preceded by a keyword and an equals sign.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">The order of arguments doesn&#8217;t matter.<\/span><\/li>\n<\/ul>\n<p align=\"left\"><strong><span style=\"color: #000000; font-family: georgia, palatino, serif;\"><span style=\"font-size: medium;\">3. Default Arguments:<\/span><\/span><\/strong><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Default arguments are those that take a default value if no value is provided.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">They are specified in the function definition.<\/span><\/li>\n<\/ul>\n<p align=\"left\"><strong><span style=\"color: #000000; font-family: georgia, palatino, serif;\"><span style=\"font-size: medium;\">4. Variable-length Arguments:<\/span><\/span><\/strong><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Sometimes, you might want to pass a variable number of arguments to a function.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Python provides two ways to handle this: *args (non-keyword variable-length arguments) and **kwargs (keyword variable-length arguments).<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Let&#8217;s delve deeper into each type of argument using the example of adding two numbers in Python.<\/span><\/strong><\/p>\n<h3 class=\"western\"><span style=\"color: #000000; font-family: georgia, palatino, serif;\">1. Positional Arguments:<\/span><\/h3>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Positional arguments are the most common type of arguments in Python functions. They are passed to a function based on their position, meaning the order in which they are passed matters.<\/span><\/p>\n<p><span style=\"color: #000000;\">Example:\u00a0<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def add_numbers(num1, num2):\r\n\r\n    return num1 + num2\r\n\r\n# Calling the function with positional arguments\r\n\r\nresult = add_numbers(5, 3)\r\n\r\nprint(\"Result (Positional Arguments):\", result)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In this example:<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\"><code class=\"western\">num1<\/code> and <code class=\"western\">num2<\/code> are positional arguments.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">When the function <code class=\"western\">add_numbers<\/code> is called, the first argument <code class=\"western\">5<\/code> is assigned to <code class=\"western\">num1<\/code>, and the second argument <code class=\"western\">3<\/code> is assigned to <code class=\"western\">num2<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">The function then adds these two numbers and returns the result.<\/span><\/li>\n<\/ul>\n<h3><\/h3>\n<h3 class=\"western\"><span style=\"color: #000000; font-family: georgia, palatino, serif;\">2. Default Arguments:<\/span><\/h3>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Default arguments are parameters in a function that have a default value. If a value is not provided for these parameters during the function call, they take on their default value.<\/span><\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def add_numbers(num1, num2=0):\r\n\r\n    return num1 + num2\r\n\r\n# Calling the function without specifying num2 (using default value)\r\n\r\nresult = add_numbers(5)\r\n\r\nprint(\"Result (Default Argument):\", result)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In this example:<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\"><code class=\"western\">num2<\/code> is a default argument with a default value of <code class=\"western\">0<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">If <code class=\"western\">num2<\/code> is not provided during the function call, it automatically takes on the value of <code class=\"western\">0<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">When the function <code class=\"western\">add_numbers<\/code> is called with only one argument <code class=\"western\">5<\/code>, it uses the default value of <code class=\"western\">0<\/code> for <code class=\"western\">num2<\/code>, resulting in <code class=\"western\">5<br \/>\n+ 0<\/code><\/span><\/li>\n<\/ul>\n<h3><\/h3>\n<h3 class=\"western\"><span style=\"color: #000000; font-family: georgia, palatino, serif;\">3. Keyword Arguments:<\/span><\/h3>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Keyword arguments allow you to specify arguments by their parameter names, regardless of their order. This provides clarity and flexibility in function calls.<\/span><\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def add_numbers(num1, num2):\r\n\r\n    return num1 + num2\r\n\r\n# Calling the function with keyword arguments\r\n\r\nresult = add_numbers(num1=5, num2=3)\r\n\r\nprint(\"Result (Keyword Arguments):\", result)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In this example:<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">The function <code class=\"western\">add_numbers<\/code> is called with keyword arguments <code class=\"western\">num1=5<\/code> and <code class=\"western\">num2=3<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Order of arguments doesn&#8217;t matter, as the arguments are passed with their respective parameter names.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">This provides clarity, especially when dealing with functions that have many arguments.<\/span><\/li>\n<\/ul>\n<h3><\/h3>\n<h3 class=\"western\"><span style=\"color: #000000; font-family: georgia, palatino, serif;\">4. Variable-length Arguments:<\/span><\/h3>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Variable-length arguments allow a function to accept any number of arguments. In Python, you can achieve this using <code class=\"western\">*args<\/code>, which collects positional arguments into a tuple.<\/span><\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def add_numbers(*args):\r\n\r\n    total = 0\r\n\r\n    for num in args:\r\n\r\n        total += num\r\n\r\n        return total\r\n\r\n# Calling the function with variable number of arguments\r\n\r\nresult = add_numbers(5, 3, 7, 2)\r\n\r\nprint(\"Result (Variable-length Arguments):\", result)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">In this example:<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">The function <code class=\"western\">add_numbers<\/code> accepts any number of arguments using <code class=\"western\">*args<\/code>.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">It then iterates over these arguments and adds them together to get the result.<\/span><\/li>\n<li><span style=\"color: #000000; font-family: georgia, palatino, serif;\">This allows flexibility in function calls, as you can pass any number of arguments to the function.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000; font-family: georgia, palatino, serif;\">These different types of arguments provide flexibility and clarity in function calls, allowing you to write more versatile and expressive code in Python.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Function Arguments and Parameters Notes (Positional, Keyword, Default) Python function arguments and parameters are important concepts frequently asked in programming exams and practical assessments. They define how values are passed to functions and how functions process data. This topic covers positional arguments, keyword arguments, and default arguments in Python, explained in a simple and [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,20],"tags":[],"class_list":["post-4751","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-12","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/4751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=4751"}],"version-history":[{"count":2,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/4751\/revisions"}],"predecessor-version":[{"id":5574,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/4751\/revisions\/5574"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=4751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=4751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=4751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}