{"id":80066,"date":"2020-10-01T12:40:09","date_gmt":"2020-10-01T07:10:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80066"},"modified":"2026-06-03T15:31:15","modified_gmt":"2026-06-03T10:01:15","slug":"python-calculator-program","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/","title":{"rendered":"Python Calculator &#8211; Create a Calculator using Python"},"content":{"rendered":"<p>In today&#8217;s era, everyone wants to become a python expert, but how? The answer is to work on projects. In this article, TechVidvan presents a python project for beginners &#8211; <strong>Simple Calculator<\/strong>, so let&#8217;s start the work.<\/p>\n<p><strong>Simple Calculator in Python<\/strong><\/p>\n<p>Doing a mathematical calculation in your head, not trusting your answer, so you ended up using a calculator anyway. So we have just proved the importance of the simplest machine you\u2019ve ever seen. Now let\u2019s see how we can create this simple calculator using Python.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/python-calculator-project.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80071\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/python-calculator-project.jpg\" alt=\"python calculator project\" width=\"1200\" height=\"628\" \/><\/a><\/p>\n<h3>Project Prerequisites<\/h3>\n<p>To implement this project you need to know the following:<\/p>\n<ol>\n<li>Basic concepts of Python<\/li>\n<li>Tkinter &#8211; To create GUI<\/li>\n<\/ol>\n<p>To install the libraries, you can use pip installer from the cmd\/Terminal:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Pip install tkinter<\/pre>\n<h2>Steps to Create Simple Calculator using Python<\/h2>\n<h3>Download Python Calculator Code<\/h3>\n<p>Please download source code of calculator project: <a href=\"https:\/\/drive.google.com\/file\/d\/1f5a1deE07RsMIsFSaVVV45sGy9DhIcIn\/view?usp=drive_link\"><strong>Python Calculator<\/strong><\/a><\/p>\n<p><strong>Let&#8217;s start the coding<\/strong><\/p>\n<p>Now, we will write the python program to create a calculator<\/p>\n<h3>Create main.py<\/h3>\n<p>Create main.py file and add the following code (alternatively, you can use the code which you downloaded in previous step):<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from tkinter import *\r\n\r\ndef click(num):\r\n    global op\r\n    op=op+str(num)\r\n    iptext.set(op)\r\n\r\ndef evaluate():\r\n    global op\r\n    output=str(eval(op))\r\n    iptext.set(output)\r\n\r\ndef clearDisplay():\r\n    global op\r\n    op=\"\"\r\n    iptext.set(op)\r\n\r\ncalc=Tk()\r\ncalc.title(\"TechVidvan Calculator\")\r\nop=\"\"\r\n\r\niptext=StringVar()\r\niparea=Entry(calc,font=('large,_font',15,'bold'),bd=10,justify=\"right\",insertwidth=4,textvariable=iptext).grid(columnspan=10)\r\n\r\nbt7=Button(calc,font=('arial',15,'bold'),command=lambda:click(7),bg=\"lavender\",text=\"7\",bd=5,padx=15,pady=10).grid(row=1,column=0)\r\n\r\nbt8=Button(calc,font=('arial',15,'bold'),command=lambda:click(8),bg=\"lavender\",text=\"8\",bd=5,padx=15,pady=10).grid(row=1,column=1)\r\n\r\nbt9=Button(calc,font=('arial',15,'bold'),command=lambda:click(9),bg=\"lavender\",text=\"9\",bd=5,padx=15,pady=10).grid(row=1,column=2)\r\n\r\nadd=Button(calc,font=('arial',15,'bold'),command=lambda:click('+'),bg=\"lavender\",text=\"+\",bd=5,padx=15,pady=10).grid(row=1,column=3)\r\n\r\nbt4=Button(calc,font=('arial',15,'bold'),command=lambda:click(4),bg=\"lavender\",text=\"4\",bd=5,padx=15,pady=10).grid(row=2,column=0)\r\n\r\nbt5=Button(calc,font=('arial',15,'bold'),command=lambda:click(5),bg=\"lavender\",text=\"5\",bd=5,padx=15,pady=10).grid(row=2,column=1)\r\n\r\nbt6=Button(calc,font=('arial',15,'bold'),command=lambda:click(6),bg=\"lavender\",text=\"6\",bd=5,padx=15,pady=10).grid(row=2,column=2)\r\n\r\nsub=Button(calc,font=('arial',15,'bold'),command=lambda:click('-'),bg=\"lavender\",text=\"-\",bd=5,padx=15,pady=10).grid(row=2,column=3)\r\n\r\nbt1=Button(calc,font=('arial',15,'bold'),command=lambda:click(1),bg=\"lavender\",text=\"1\",bd=5,padx=15,pady=10).grid(row=3,column=0)\r\n\r\nbt2=Button(calc,font=('arial',15,'bold'),command=lambda:click(2),bg=\"lavender\",text=\"2\",bd=5,padx=15,pady=10).grid(row=3,column=1)\r\n\r\nbt3=Button(calc,font=('arial',15,'bold'),command=lambda:click(3),bg=\"lavender\",text=\"3\",bd=5,padx=15,pady=10).grid(row=3,column=2)\r\n\r\nmul=Button(calc,font=('arial',15,'bold'),command=lambda:click('*'),bg=\"lavender\",text=\"*\",bd=5,padx=15,pady=10).grid(row=3,column=3)\r\n\r\nbt0=Button(calc,font=('arial',15,'bold'),command=lambda:click(0),bg=\"lavender\",text=\"0\",bd=5,padx=15,pady=10).grid(row=4,column=0)\r\n\r\nbtC=Button(calc,font=('arial',15,'bold'),command=clearDisplay,bg=\"lavender\",text=\"C\",bd=5,padx=15,pady=10).grid(row=4,column=1)\r\n\r\neql=Button(calc,font=('arial',15,'bold'),command=evaluate,bg=\"lavender\",text=\"=\",bd=5,padx=15,pady=10).grid(row=4,column=2)\r\n\r\ndiv=Button(calc,font=('arial',15,'bold'),command=lambda:click('\/'),bg=\"lavender\",text=\"\/\",bd=5,padx=15,pady=10).grid(row=4,column=3)\r\n\r\ncalc.mainloop(),<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>After importing tkinter, we have defined following functions:<\/p>\n<p><strong>1. Click(num):<\/strong> this function takes a numeric parameter. It first converts num to string and then the function appends the string to op and displays final value of op in input area of the calculator.<\/p>\n<p><strong>2. evaluate():<\/strong> This function is executed everytime the user clicks \u201c=\u201d i.e. when we need to evaluate the equation. Eval() is an inbuilt function in python which takes a string argument (equation) and returns a string (the output of the equation).<\/p>\n<p>So this function just calls eval method and displays the output.<\/p>\n<p><strong>3. clearDisplay():<\/strong> It just clears the input area.<\/p>\n<p>Then we have just created an entry box to display input and output. And then finally the twelve buttons (0-9,+,-,*,\/,=,C)<\/p>\n<p>0-9,+,-,*,\/, calls \u2018click()\u2019 when clicked.<\/p>\n<p>C calls \u2018clearDisplay()\u2019.<\/p>\n<p>= calls \u2018evaluate()\u2019<\/p>\n<h4>Calculator Program Output:<\/h4>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/python-calculator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-80069\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/10\/python-calculator.png\" alt=\"python calculator output\" width=\"400\" height=\"550\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>We have successfully developed Python calculator project, with basic concepts of Python and Tkinter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s era, everyone wants to become a python expert, but how? The answer is to work on projects. In this article, TechVidvan presents a python project for beginners &#8211; Simple Calculator, so let&#8217;s&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80071,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3314,3315,483],"class_list":["post-80066","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-calculator-project","tag-python-calculator","tag-python-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Calculator - Create a Calculator using Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Calculator - Create a simple calculator using Python. Develop Python program with basic concepts of tkinter to create the GUI.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Calculator - Create a Calculator using Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Calculator - Create a simple calculator using Python. Develop Python program with basic concepts of tkinter to create the GUI.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-01T07:10:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T10:01:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Calculator - Create a Calculator using Python - TechVidvan","description":"Python Calculator - Create a simple calculator using Python. Develop Python program with basic concepts of tkinter to create the GUI.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/","og_locale":"en_US","og_type":"article","og_title":"Python Calculator - Create a Calculator using Python - TechVidvan","og_description":"Python Calculator - Create a simple calculator using Python. Develop Python program with basic concepts of tkinter to create the GUI.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-10-01T07:10:09+00:00","article_modified_time":"2026-06-03T10:01:15+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Calculator &#8211; Create a Calculator using Python","datePublished":"2020-10-01T07:10:09+00:00","dateModified":"2026-06-03T10:01:15+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/"},"wordCount":347,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg","keywords":["calculator project","python calculator","Python project"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/","url":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/","name":"Python Calculator - Create a Calculator using Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg","datePublished":"2020-10-01T07:10:09+00:00","dateModified":"2026-06-03T10:01:15+00:00","description":"Python Calculator - Create a simple calculator using Python. Develop Python program with basic concepts of tkinter to create the GUI.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/10\/python-calculator-project.jpg","width":1200,"height":628,"caption":"python calculator project"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-calculator-program\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Calculator &#8211; Create a Calculator using Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80066"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80066\/revisions"}],"predecessor-version":[{"id":448090,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80066\/revisions\/448090"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80071"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}