{"id":175,"date":"2026-09-03T08:54:01","date_gmt":"2026-09-03T08:54:01","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=175"},"modified":"2026-09-03T08:54:01","modified_gmt":"2026-09-03T08:54:01","slug":"write-a-python-program-to-define-a-class-calculator","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/write-a-python-program-to-define-a-class-calculator\/","title":{"rendered":"Write a Python program to define a class Calculator"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Write a Python program to define a class Calculator with a method calculate() that behaves differently based on the number of arguments passed: If one argument is passed, return its square. If two arguments are passed, return their sum. If three or more arguments are passed, return &#8220;Invalid number of arguments&#8221;.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Calculator:\n    # Using *args to handle variable number of arguments\n    def calculate(self, *args):\n        \"\"\"\n        Performs different operations based on the number of arguments.\n        - 1 arg: returns square of the number.\n        - 2 args: returns sum of the two numbers.\n        - 3+ args: returns an error message.\n        \"\"\"\n        num_args = len(args)\n\n        if num_args == 1:\n            # Square the single argument\n            return args[0] ** 2\n        elif num_args == 2:\n            # Sum of two arguments\n            return args[0] + args[1]\n        else:\n            # Invalid number of arguments\n            return \"Invalid number of arguments\"\n\n# --- Creating an object and testing the calculate method ---\ncalc = Calculator()\n\nprint(\"--- Testing the Calculator ---\")\n# Test case 1: One argument (e.g., 7)\nprint(f\"calculate(7) -&gt; {calc.calculate(7)}\")\n\n# Test case 2: Two arguments (e.g., 15 and 25)\nprint(f\"calculate(15, 25) -&gt; {calc.calculate(15, 25)}\")\n\n# Test case 3: Three arguments (e.g., 1, 2, 3)\nprint(f\"calculate(1, 2, 3) -&gt; {calc.calculate(1, 2, 3)}\")\n\n# Test case 4: Four arguments\nprint(f\"calculate(10, 20, 30, 40) -&gt; {calc.calculate(10, 20, 30, 40)}\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write a Python program to define a class Calculator with a method calculate() that behaves differently based on the number of arguments passed: If one argument is passed, return its square. If two arguments are passed, return their sum. If three or more arguments are passed, return &#8220;Invalid number of arguments&#8221;. class Calculator: # Using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[52,25,26,45],"class_list":["post-175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aiopd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions\/176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}