{"id":77956,"date":"2020-04-03T18:07:59","date_gmt":"2020-04-03T12:37:59","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77956"},"modified":"2024-08-22T18:09:24","modified_gmt":"2024-08-22T12:39:24","slug":"python-methods-vs-functions","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/","title":{"rendered":"Python Methods vs Functions &#8211; What really differentiates them?"},"content":{"rendered":"<p>In this article, we will learn about <strong>Python Methods<\/strong> vs <strong>Functions<\/strong>.<\/p>\n<p>Every beginner tends to get a little confused over how a <strong>function<\/strong> is <strong>different<\/strong> from a <strong>method<\/strong>. Well, both of them have <strong>one sole purpose<\/strong> in life, to <strong>perform<\/strong> some <strong>operation<\/strong>. And both of them can <strong>return<\/strong> some <strong>value<\/strong>.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-methods-vs-functions.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77962\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/python-methods-vs-functions.jpg\" alt=\"\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<p>But before we do, we quickly need to recall the two of them that is, <strong>Python Functions<\/strong> and <strong>Python Methods<\/strong> <strong>separately<\/strong>.<\/p>\n<h3>Python Functions<\/h3>\n<p>Simply put, a function is a <strong>series <\/strong>of <strong>steps<\/strong> <strong>executed<\/strong> as a <strong>single unit<\/strong> and \u201c<strong>encapsulated<\/strong>\u201d under a <strong>single name<\/strong>. It is a <strong>group<\/strong> of <strong>statements<\/strong> <strong>capable<\/strong> of <strong>performing<\/strong> some <strong>tasks<\/strong>.<\/p>\n<p>A function in a program can take <strong>arguments<\/strong> (that is, the <strong>data to operate on<\/strong>) and can <strong>optionally return<\/strong> some <strong>data<\/strong> after <strong>performing<\/strong> the <strong>intended task<\/strong>. <strong>Creating<\/strong> a <strong>function<\/strong> is basically giving your computer <strong>well-defined instructions<\/strong> to <strong>perform a task<\/strong>.<\/p>\n<p>So we can create a simple function in <strong>3<\/strong> simple <strong>steps<\/strong>:<\/p>\n<h4>1. def add()<\/h4>\n<p>says, hey computer, give me a function called <strong>add<\/strong>.<\/p>\n<h4>2. def add(a, b)<\/h4>\n<p>is saying, hey computer, the <strong>add function<\/strong> should <strong>take two arguments<\/strong>. This is where the concept of <strong>code-reusability<\/strong> comes in.<\/p>\n<p>By giving the <strong>arguments <\/strong>generalized <strong>names a<\/strong> and <strong>b<\/strong>, we\u2019re saying that in some instances <strong>a<\/strong> can be <strong>2<\/strong> and <strong>b<\/strong> can be <strong>3<\/strong>, while in some other instance <strong>a<\/strong> can be <strong>10<\/strong> and <strong>b<\/strong> can be <strong>30<\/strong>.<\/p>\n<h4>3. def add(a, b)<\/h4>\n<p><strong>return a+b<\/strong><\/p>\n<p>This says, hey computer, let my function return some stuff, let it <strong>return<\/strong> the <strong>sum of a and b<\/strong>.<\/p>\n<p>Finally, you call the function as:<\/p>\n<p><strong>&gt;&gt; add(10,30)<\/strong><\/p>\n<p>This <strong>returns<\/strong> the <strong>sum of 10<\/strong> and <strong>30<\/strong>, that is, <strong>40<\/strong> to us.<\/p>\n<h3>Types of Python Functions<\/h3>\n<p><strong>Functions in Python<\/strong>, like many other programming languages, are of two types:<\/p>\n<h4>1. Built-in functions<\/h4>\n<p><strong>Built-in functions<\/strong> come included with the <strong>rich standard library<\/strong> of <strong>Python<\/strong>. These are the functions that some programmers somewhere in the world <strong>defined<\/strong> a long time ago and now you can use these functions in your <strong>program<\/strong> <strong>without<\/strong> <strong>having<\/strong> <strong>to write<\/strong> <strong>them all over<\/strong> <strong>again<\/strong>.<\/p>\n<p>They provide essential functionality, from basic operations like print() and len() to more complex ones like map() and filter(). Using these built-in functions can save time and effort, allowing you to focus on writing the unique parts of your code. Moreover, since these functions are part of Python&#8217;s standard library, they are well-tested and optimized for performance.<\/p>\n<p>Like the<strong> print() function<\/strong> which <strong>prints<\/strong> some <strong>output<\/strong> on the screen.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print (\"Techvidvan\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Techvidvan<br \/>\n&gt;&gt;&gt;<\/div>\n<h4>2. User-defined functions<\/h4>\n<p>Python lets us <strong>define<\/strong> and <strong>use our own functions<\/strong> which we can <strong>use anywhere<\/strong> in our <strong>program<\/strong>. One such example is the<strong> add() function<\/strong> we created above. These functions allow us to encapsulate code into reusable blocks, making our programs more modular and easier to maintain. You can define a function using the def keyword, followed by the function name and parentheses containing any parameters. Once defined, you can call your function wherever needed, passing the appropriate arguments to perform specific tasks or calculations.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def add(a, b):\r\n        Return a + b\r\n\r\n&gt;&gt;&gt; print(add(10, 30))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">40<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Python Methods<\/h3>\n<p><strong>Objects<\/strong> in <strong>object-oriented programming<\/strong> have attributes, i.e. <strong>data values<\/strong> within them. But how do we access these attribute values? We <strong>access<\/strong> them through <strong>methods<\/strong>.<\/p>\n<p><strong>Methods<\/strong> represent the <strong>behavior of an Object<\/strong>.<\/p>\n<p>A method is a <strong>piece of code<\/strong> that is <strong>associated<\/strong> with an <strong>object<\/strong> and <strong>operates<\/strong> upon the<strong> data of that object<\/strong>. In most respects, a method is <strong>identical<\/strong> to a <strong>function<\/strong>.<\/p>\n<p>Except for two major differences:<\/p>\n<ul>\n<li>It is associated with an object and we call it<strong> \u2018on\u2019<\/strong> that object.<\/li>\n<li>It operates on the data which is contained <strong>within<\/strong> the <strong>class<\/strong>.<br \/>\n(Recall that class is a template and object is an instance of a class)<\/li>\n<\/ul>\n<p>Since a method is associated with an <strong>object<\/strong>, <strong>defining<\/strong> and <strong>creating<\/strong> a <strong>method<\/strong> is little <strong>different<\/strong> from <strong>defining functions<\/strong>:<\/p>\n<ul>\n<li>We always define a method <strong>inside a class<\/strong>.<\/li>\n<li>A method definition always includes<strong> \u2018self\u2019<\/strong> as its <strong>first parameter<\/strong>.<\/li>\n<li>And lastly, we call a method on an object using the <strong>dot operator<\/strong>.<\/li>\n<\/ul>\n<p><strong>Let\u2019s look at an example to gain a better understanding of methods.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Car:\r\n\u00a0\u00a0\u00a0 def sound(self):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print \"Vroooom!\"\r\n\r\nferrari = Car()\r\nferrari.sound() #calling method sound on object ferrari<\/pre>\n<p>In this example, we define a <strong>method<\/strong> sound inside a <strong>class Car<\/strong>. We have then <strong>created<\/strong> an <strong>object ferrari<\/strong> of the class Car. And we finally <strong>call<\/strong> the <strong>method sound()<\/strong> on the <strong>object ferrari<\/strong>.<\/p>\n<p>Let\u2019s now highlight the differences we have learned.<\/p>\n<h3>Difference between Python Methods vs Functions<\/h3>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>METHODS<\/b><\/td>\n<td><b>FUNCTIONS<\/b><\/td>\n<\/tr>\n<tr>\n<td>Methods definitions are always present inside a class.<\/td>\n<td>We don\u2019t need a class to define a function.<\/td>\n<\/tr>\n<tr>\n<td>Methods are associated with the objects of the class they belong to.<\/td>\n<td>Functions are not associated with any object.<\/td>\n<\/tr>\n<tr>\n<td>A method is called \u2018on\u2019 an object. We cannot invoke it just by its name<\/td>\n<td>We can invoke a function just by its name.<\/td>\n<\/tr>\n<tr>\n<td>Methods can operate on the data of the object they associate with<\/td>\n<td>Functions operate on the data you pass to them as arguments.<\/td>\n<\/tr>\n<tr>\n<td>Methods are dependent on the class they belong to.<\/td>\n<td>Functions are independent entities in a program.<\/td>\n<\/tr>\n<tr>\n<td>A method requires to have \u2018self\u2019 as its first argument.<\/td>\n<td>Functions do not require any \u2018self\u2019 argument. They can have zero or more arguments.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>We can conclude that <strong>Methods<\/strong> and <strong>Functions<\/strong> are like those two very similar options that appear in your Multiple Choice Questions. They <strong>both<\/strong> appear to be <strong>similar<\/strong> when you see them but have key <strong>differences<\/strong> in what they actually are.<\/p>\n<p>In a nutshell, both methods and functions <strong>perform tasks<\/strong> and may <strong>return some value<\/strong>. But the difference lies in the fact that <strong>methods<\/strong> are <strong>\u2018associated\u2019<\/strong> with <strong>objects<\/strong>, while <strong>functions<\/strong> are <strong>not<\/strong>.<\/p>\n<p>That was all about Python Methods vs Functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about Python Methods vs Functions. Every beginner tends to get a little confused over how a function is different from a method. Well, both of them have one&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":77962,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2280,2281,1233,1235,2282],"class_list":["post-77956","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-comparision-of-python-methods-vs-functions","tag-difference-between-python-function-vs-methods","tag-python-functions","tag-python-methods","tag-python-methods-vs-functions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Methods vs Functions - What really differentiates them? - TechVidvan<\/title>\n<meta name=\"description\" content=\"With this article explore the difference between Python Methods vs Functions in detail and get to know more about both the concepts in depth.\" \/>\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-methods-vs-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Methods vs Functions - What really differentiates them? - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"With this article explore the difference between Python Methods vs Functions in detail and get to know more about both the concepts in depth.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/\" \/>\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-04-03T12:37:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T12:39:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Methods vs Functions - What really differentiates them? - TechVidvan","description":"With this article explore the difference between Python Methods vs Functions in detail and get to know more about both the concepts in depth.","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-methods-vs-functions\/","og_locale":"en_US","og_type":"article","og_title":"Python Methods vs Functions - What really differentiates them? - TechVidvan","og_description":"With this article explore the difference between Python Methods vs Functions in detail and get to know more about both the concepts in depth.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-03T12:37:59+00:00","article_modified_time":"2024-08-22T12:39:24+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Methods vs Functions &#8211; What really differentiates them?","datePublished":"2020-04-03T12:37:59+00:00","dateModified":"2024-08-22T12:39:24+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/"},"wordCount":931,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.jpg","keywords":["Comparision of Python Methods vs Functions","Difference between Python Function vs Methods","python functions","python methods","Python Methods vs Functions"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/","url":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/","name":"Python Methods vs Functions - What really differentiates them? - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.jpg","datePublished":"2020-04-03T12:37:59+00:00","dateModified":"2024-08-22T12:39:24+00:00","description":"With this article explore the difference between Python Methods vs Functions in detail and get to know more about both the concepts in depth.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-methods-vs-functions.jpg","width":802,"height":420,"caption":"python methods vs functions"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods-vs-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Methods vs Functions &#8211; What really differentiates them?"}]},{"@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\/77956","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=77956"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77956\/revisions"}],"predecessor-version":[{"id":447683,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77956\/revisions\/447683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/77962"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}