-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparams.json
6 lines (6 loc) · 44.3 KB
/
params.json
1
2
3
4
5
6
{
"name": "Python Notes",
"tagline": "Mustafa ÇELİK",
"body": "<h1 id=\"python-notes\"><strong><em>PYTHON NOTES</em></strong></h1>\r\n\r\n<hr>\r\n\r\n\r\n\r\n<h2 id=\"table-of-contents\"><strong>Table of Contents</strong></h2>\r\n\r\n<p><div class=\"toc\">\r\n<ul>\r\n<li><a href=\"#python-notes\">PYTHON NOTES</a><ul>\r\n<li><a href=\"#table-of-contents\">Table of Contents</a></li>\r\n<li><a href=\"#functional-programming\">Functional Programming</a><ul>\r\n<li><a href=\"#lambdas\">Lambdas</a></li>\r\n<li><a href=\"#map\">Map</a></li>\r\n<li><a href=\"#filter\">Filter</a></li>\r\n<li><a href=\"#generators-1\">Generators-1</a></li>\r\n<li><a href=\"#generators-2\">Generators-2</a></li>\r\n<li><a href=\"#generators-3\">Generators-3</a></li>\r\n<li><a href=\"#decorators-1\">Decorators-1</a></li>\r\n<li><a href=\"#decorators-2\">Decorators-2</a></li>\r\n<li><a href=\"#recursion-1\">Recursion-1</a></li>\r\n<li><a href=\"#recursion-2\">Recursion-2</a></li>\r\n<li><a href=\"#recursion-3\">Recursion-3</a></li>\r\n<li><a href=\"#sets-1\">Sets-1</a></li>\r\n<li><a href=\"#sets-2\">Sets-2</a></li>\r\n<li><a href=\"#sets-3\">Sets-3</a></li>\r\n<li><a href=\"#data-structures\">Data Structures</a></li>\r\n<li><a href=\"#itertools-1\">itertools-1</a></li>\r\n<li><a href=\"#itertools-2\">itertools-2</a></li>\r\n<li><a href=\"#itertools-3\">itertools-3</a></li>\r\n</ul>\r\n</li>\r\n<li><a href=\"#object-oriented-programming\">Object-Oriented Programming</a><ul>\r\n<li><a href=\"#classes-1\">Classes-1</a><ul>\r\n<li><a href=\"#init\">__init__</a></li>\r\n<li><a href=\"#methods\">Methods</a></li>\r\n</ul>\r\n</li>\r\n<li><a href=\"#classes-2\">Classes-2</a></li>\r\n<li><a href=\"#inheritance-1\">Inheritance-1</a></li>\r\n<li><a href=\"#inheritance-2\">Inheritance-2</a></li>\r\n<li><a href=\"#inheritance-3\">Inheritance-3</a></li>\r\n<li><a href=\"#inheritance-4\">Inheritance-4</a></li>\r\n</ul>\r\n</li>\r\n</ul>\r\n</li>\r\n</ul>\r\n</div>\r\n</p>\r\n\r\n<p>Source: <a href=\"http://www.sololearn.com/Play/Python\">SoloLearn Python Tutorial</a></p>\r\n\r\n\r\n\r\n<h2 id=\"functional-programming\"><strong><em>Functional Programming</em></strong></h2>\r\n\r\n\r\n\r\n<h3 id=\"lambdas\"><strong>Lambdas</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Creating a function normally (using <strong>def</strong>) assigns it to a variable automatically. <br>\r\nThis is different from the creation of other objects - such as <strong>strings</strong> and <strong>integers</strong> - which can be created on the fly, without assigning them to a variable. <br>\r\nThe same is possible with <strong>functions</strong>, provided that they are created using <strong>lambda syntax</strong>. Functions created this way are known as <strong>anonymous</strong>. <br>\r\nThis approach is most commonly used when passing a simple function as an argument to another function. The syntax is shown in the next example and consists of the lambda keyword followed by a list of arguments, a <strong>colon</strong>, and the <strong>expression</strong> to <strong>evaluate</strong> and <strong>return</strong>.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">my_func</span><span class=\"hljs-params\">(f, arg)</span>:</span>\r\n <span class=\"hljs-keyword\">return</span> f(arg)\r\n\r\nmy_func(<span class=\"hljs-keyword\">lambda</span> x: <span class=\"hljs-number\">2</span>*x*x, <span class=\"hljs-number\">5</span>)</code></pre>\r\n\r\n<blockquote>\r\n <p><strong>Note:</strong> Lambda functions get their name from lambda calculus, which is a model of computation invented by Alonzo Church.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"map\"><strong>Map</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>The built-in functions <strong>map</strong> and <strong>filter</strong> are very useful <strong>higher-order functions</strong> that operate on <strong>lists</strong> (or similar objects called iterables). <br>\r\nThe function map takes a function and an iterable as arguments, and returns a new iterable with the function applied to each argument.</p>\r\n\r\n<p><strong>Example</strong>:</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">add_five</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">return</span> x + <span class=\"hljs-number\">5</span>\r\n\r\nnums = [<span class=\"hljs-number\">11</span>, <span class=\"hljs-number\">22</span>, <span class=\"hljs-number\">33</span>, <span class=\"hljs-number\">44</span>, <span class=\"hljs-number\">55</span>]\r\nresult = list(map(add_five, nums))\r\nprint(result)\r\n</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>[16, 27, 38, 49, 60]</p>\r\n</blockquote>\r\n\r\n<p>We could have achieved the same result more easily by using <strong>lambda syntax</strong>.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs applescript\">nums = [<span class=\"hljs-number\">11</span>, <span class=\"hljs-number\">22</span>, <span class=\"hljs-number\">33</span>, <span class=\"hljs-number\">44</span>, <span class=\"hljs-number\">55</span>]\r\n\r\n<span class=\"hljs-constant\">result</span> = <span class=\"hljs-type\">list</span>(map(lambda x: x+<span class=\"hljs-number\">5</span>, nums))\r\nprint(<span class=\"hljs-constant\">result</span>)</code></pre>\r\n\r\n<blockquote>\r\n <p><strong>Note:</strong> To convert the result into a list, we used list explicitly.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"filter\"><strong>Filter</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>The function <strong>filter</strong> filters an iterable by removing items that don’t match a predicate (a function that returns a Boolean). <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs mel\">nums = [<span class=\"hljs-number\">11</span>, <span class=\"hljs-number\">22</span>, <span class=\"hljs-number\">33</span>, <span class=\"hljs-number\">44</span>, <span class=\"hljs-number\">55</span>]\r\nres = list(<span class=\"hljs-keyword\">filter</span>(lambda x: x<span class=\"hljs-variable\">%2</span>==<span class=\"hljs-number\">0</span>, nums))\r\n<span class=\"hljs-keyword\">print</span>(res)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>[22, 44]</p>\r\n \r\n <p><strong>Note:</strong> Like map, the result has to be explicitly converted to a list if you want to print it.</p>\r\n</blockquote>\r\n\r\n<p><strong>Example:</strong> <br>\r\nFill in the blanks to remove all items that are greater than 4 from the list.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs php\">nums = [<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">0</span>, <span class=\"hljs-number\">7</span>]\r\nres = <span class=\"hljs-keyword\">list</span>(filter(lambda x: x < <span class=\"hljs-number\">5</span>, nums))\r\n<span class=\"hljs-keyword\">print</span>(res)</code></pre>\r\n\r\n\r\n\r\n<h3 id=\"generators-1\"><strong>Generators-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p><strong>Generators</strong> are a type of iterable, like lists or tuples. <br>\r\nUnlike lists, they don’t allow indexing with arbitrary indices, but they can still be iterated through with <strong>for</strong> loops. <br>\r\nThey can be created using functions and the <strong>yield</strong> statement. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">countdown</span><span class=\"hljs-params\">()</span>:</span>\r\n i=<span class=\"hljs-number\">5</span>\r\n <span class=\"hljs-keyword\">while</span> i > <span class=\"hljs-number\">0</span>:\r\n <span class=\"hljs-keyword\">yield</span> i\r\n i -= <span class=\"hljs-number\">1</span>\r\n\r\n<span class=\"hljs-keyword\">for</span> i <span class=\"hljs-keyword\">in</span> countdown():\r\n print(i)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>5 <br>\r\n 4 <br>\r\n 3 <br>\r\n 2 <br>\r\n 1</p>\r\n</blockquote>\r\n\r\n<p>The <strong>yield</strong> statement is used to define a <strong>generator</strong>, replacing the return of a function to provide a result to its caller without destroying local variables.</p>\r\n\r\n\r\n\r\n<h3 id=\"generators-2\"><strong>Generators-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Due to the fact that they <strong>yield</strong> one item at a time, generators don’t have the memory restrictions of lists. <br>\r\nIn fact, they can be <strong>infinite</strong>!</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">infinite_sevens</span><span class=\"hljs-params\">()</span>:</span>\r\n <span class=\"hljs-keyword\">while</span> <span class=\"hljs-keyword\">True</span>:\r\n <span class=\"hljs-keyword\">yield</span> <span class=\"hljs-number\">7</span>\r\n\r\n<span class=\"hljs-keyword\">for</span> i <span class=\"hljs-keyword\">in</span> infinite_sevens():\r\n print(i)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>7 <br>\r\n 7 <br>\r\n 7 <br>\r\n 7 <br>\r\n 7 <br>\r\n 7 <br>\r\n 7 <br>\r\n …</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> In short, <strong>generators</strong> allow you to declare a <strong>function</strong> that behaves like an iterator, i.e. it can be used in a <strong>for</strong> loop.</p>\r\n\r\n<p><strong>Example:</strong> <br>\r\nFill in the blanks to create a prime number generator, that yields all prime numbers in a loop. (Consider having an is_prime function already defined):</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"> <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">get_primes</span><span class=\"hljs-params\">()</span>:</span>\r\n num = <span class=\"hljs-number\">2</span>\r\n <span class=\"hljs-keyword\">while</span> <span class=\"hljs-keyword\">True</span>:\r\n <span class=\"hljs-keyword\">if</span> is_prime(num):\r\n <span class=\"hljs-keyword\">yield</span> num\r\n num += <span class=\"hljs-number\">1</span></code></pre>\r\n\r\n\r\n\r\n<h3 id=\"generators-3\"><strong>Generators-3</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Finite generators can be converted into lists by passing them as arguments to the <strong>list</strong> function.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">numbers</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">for</span> i <span class=\"hljs-keyword\">in</span> range(x):\r\n <span class=\"hljs-keyword\">if</span> i % <span class=\"hljs-number\">2</span> == <span class=\"hljs-number\">0</span>:\r\n <span class=\"hljs-keyword\">yield</span> i\r\n\r\nprint(list(numbers(<span class=\"hljs-number\">11</span>)))</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>[0, 2, 4, 6, 8, 10]</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> Using <strong>generators</strong> results in improved performance, which is the result of the lazy (on demand) generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them.</p>\r\n\r\n\r\n\r\n<h3 id=\"decorators-1\"><strong>Decorators-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p><strong>Decorators</strong> provide a way to modify functions using other functions. <br>\r\nThis is ideal when you need to extend the functionality of functions that you don’t want to modify. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">decor</span><span class=\"hljs-params\">(func)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">wrap</span><span class=\"hljs-params\">()</span>:</span>\r\n print(<span class=\"hljs-string\">\"============\"</span>)\r\n func()\r\n print(<span class=\"hljs-string\">\"============\"</span>)\r\n <span class=\"hljs-keyword\">return</span> wrap\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">print_text</span><span class=\"hljs-params\">()</span>:</span>\r\n print(<span class=\"hljs-string\">\"Hello world!\"</span>)\r\n\r\ndecorated = decor(print_text)\r\ndecorated()</code></pre>\r\n\r\n<p>We defined a function named <strong>decor</strong> that has a single parameter <strong>func</strong>. Inside <strong>decor</strong>, we defined a nested function named <strong>wrap</strong>. The <strong>wrap</strong> function will print a string, then call <strong>func()</strong>, and print another string. The <strong>decor</strong> function returns the <strong>wrap</strong> function as its result. <br>\r\nWe could say that the variable <strong>decorated</strong> is a decorated version of <strong>print_text</strong> - it’s <strong>print_text</strong> plus something. <br>\r\nIn fact, if we wrote a useful <strong>decorator</strong> we might want to replace <strong>print_text</strong> with the decorated version altogether so we always got our “plus something” version of <strong>print_text</strong>. <br>\r\nThis is done by re-assigning the variable that contains our function:</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs fix\"><span class=\"hljs-attribute\">print_text </span>=<span class=\"hljs-string\"> decor(print_text)\r\nprint_text()</span></code></pre>\r\n\r\n<blockquote>\r\n <p>Now print_text corresponds to our decorated version.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"decorators-2\"><strong>Decorators-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>In our previous example, we decorated our function by replacing the variable containing the function with a wrapped version.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">print_text</span><span class=\"hljs-params\">()</span>:</span>\r\n print(<span class=\"hljs-string\">\"Hello world!\"</span>)\r\n\r\nprint_text = decor(print_text)</code></pre>\r\n\r\n<p>This pattern can be used at any time, to wrap any function. <br>\r\nPython provides support to wrap a function in a decorator by pre-pending the function definition with a <strong>decorator</strong> name and the <strong>@ symbol</strong>. <br>\r\nIf we are defining a function we can “decorate” it with the @ symbol like:</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-decorator\">@decor</span>\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">print_text</span><span class=\"hljs-params\">()</span>:</span>\r\n print(<span class=\"hljs-string\">\"Hello world!\"</span>)\r\nTry It Yourself</code></pre>\r\n\r\n<p>This will have the same result as the above code.</p>\r\n\r\n<blockquote>\r\n <p><strong>Note:</strong> A single function can have multiple decorators.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"recursion-1\"><strong>Recursion-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p><strong>Recursion</strong> is a very important concept in functional programming. <br>\r\nThe fundamental part of recursion is <strong>self-reference - functions calling</strong> themselves. It is used to solve problems that can be broken up into easier sub-problems of the same type.</p>\r\n\r\n<p>A classic example of a function that is implemented recursively is the <strong>factorial function</strong>, which finds the product of all positive integers below a specified number. <br>\r\nFor example, 5! (5 factorial) is 5 * 4 * 3 * 2 * 1 (120). To implement this recursively, notice that 5! = 5 * 4!, 4! = 4 * 3!, 3! = 3 * 2!, and so on. Generally, n! = n * (n-1)!. <br>\r\nFurthermore, 1! = 1. This is known as the <strong>base case</strong>, as it can be calculated without performing any more factorials. <br>\r\nBelow is a recursive implementation of the factorial function.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">factorial</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">if</span> x == <span class=\"hljs-number\">1</span>:\r\n <span class=\"hljs-keyword\">return</span> <span class=\"hljs-number\">1</span>\r\n <span class=\"hljs-keyword\">else</span>: \r\n <span class=\"hljs-keyword\">return</span> x * factorial(x-<span class=\"hljs-number\">1</span>)\r\n\r\nprint(factorial(<span class=\"hljs-number\">5</span>))</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>120</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> <em>The base case acts as the exit condition of the recursion.</em></p>\r\n\r\n\r\n\r\n<h3 id=\"recursion-2\"><strong>Recursion-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Recursive functions can be <strong>infinite</strong>, just like infinite <strong>while</strong> loops. These often occur when you forget to implement the base case. <br>\r\nBelow is an <strong>incorrect</strong> version of the factorial function. It has <strong>no base case</strong>, so it runs until the interpreter runs out of memory and crashes.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">factorial</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">return</span> x * factorial(x-<span class=\"hljs-number\">1</span>)\r\n\r\nprint(factorial(<span class=\"hljs-number\">5</span>))</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>RuntimeError: maximum recursion depth exceeded</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"recursion-3\"><strong>Recursion-3</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Recursion can also be <strong>indirect</strong>. One function can call a second, which calls the first, which calls the second, and so on. This can occur with any number of functions. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">is_even</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">if</span> x == <span class=\"hljs-number\">0</span>:\r\n <span class=\"hljs-keyword\">return</span> <span class=\"hljs-keyword\">True</span>\r\n <span class=\"hljs-keyword\">else</span>:\r\n <span class=\"hljs-keyword\">return</span> is_odd(x-<span class=\"hljs-number\">1</span>)\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">is_odd</span><span class=\"hljs-params\">(x)</span>:</span>\r\n <span class=\"hljs-keyword\">return</span> <span class=\"hljs-keyword\">not</span> is_even(x)\r\n\r\n\r\nprint(is_odd(<span class=\"hljs-number\">17</span>))\r\nprint(is_even(<span class=\"hljs-number\">23</span>))</code></pre>\r\n\r\n<p><strong>Result</strong>:</p>\r\n\r\n<blockquote>\r\n <p>True <br>\r\n False</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"sets-1\"><strong>Sets-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p><strong>Sets</strong> are data structures, similar to <strong>lists</strong> or <strong>dictionaries</strong>. They are created using curly braces, or the <strong>set</strong> function. They share some functionality with lists, such as the use of <strong>in</strong> to check whether they contain a particular item.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs bash\">num_<span class=\"hljs-keyword\">set</span> = {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>}\r\nword_<span class=\"hljs-keyword\">set</span> = <span class=\"hljs-keyword\">set</span>([<span class=\"hljs-string\">\"spam\"</span>, <span class=\"hljs-string\">\"eggs\"</span>, <span class=\"hljs-string\">\"sausage\"</span>])\r\n\r\nprint(<span class=\"hljs-number\">3</span> <span class=\"hljs-keyword\">in</span> num_<span class=\"hljs-keyword\">set</span>)\r\nprint(<span class=\"hljs-string\">\"spam\"</span> not <span class=\"hljs-keyword\">in</span> word_<span class=\"hljs-keyword\">set</span>)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>True <br>\r\n False</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> <em>To create an empty set, you must use <strong>set()</strong>, as <strong>{}</strong> creates an empty dictionary.</em></p>\r\n\r\n\r\n\r\n<h3 id=\"sets-2\"><strong>Sets-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Sets differ from lists in several ways, but share several list operations such as <strong>len</strong>. <br>\r\nThey are unordered, which means that they can’t be indexed. <br>\r\nThey <strong>cannot contain duplicate</strong> elements. <br>\r\nDue to the way they’re stored, it’s <strong>faster</strong> to check whether an item is part of a set, rather than part of a list. <br>\r\nInstead of using <strong>append</strong> to add to a set, use <strong>add</strong>. <br>\r\nThe method <strong>remove</strong> removes a specific element from a set; <strong>pop</strong> removes an arbitrary element.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs erlang\">nums = <span class=\"hljs-tuple\">{<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">6</span>}</span>\r\n<span class=\"hljs-function\"><span class=\"hljs-title\">print</span><span class=\"hljs-params\">(nums)</span>\r\n<span class=\"hljs-title\">nums</span>.<span class=\"hljs-title\">add</span><span class=\"hljs-params\">(-<span class=\"hljs-number\">7</span>)</span>\r\n<span class=\"hljs-title\">nums</span>.<span class=\"hljs-title\">remove</span><span class=\"hljs-params\">(<span class=\"hljs-number\">3</span>)</span>\r\n<span class=\"hljs-title\">print</span><span class=\"hljs-params\">(nums)</span></span></code></pre>\r\n\r\n<p>Result:</p>\r\n\r\n<blockquote>\r\n <p>{1, 2, 3, 4, 5, 6} <br>\r\n {1, 2, 4, 5, 6, -7}</p>\r\n</blockquote>\r\n\r\n<p><strong>Note</strong>: <em>Basic uses of <strong>sets</strong> include membership testing and the <strong>elimination of duplicate entries</strong>.</em></p>\r\n\r\n\r\n\r\n<h3 id=\"sets-3\"><strong>Sets-3</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Sets can be combined using mathematical operations. <br>\r\nThe <strong>union</strong> operator <strong>|</strong> combines two sets to form a new one containing items in either. <br>\r\nThe <strong>intersection</strong> operator <strong>&</strong> gets items only in both. <br>\r\nThe <strong>difference</strong> operator <strong>-</strong> gets items in the first set but not in the second. <br>\r\nThe <strong>symmetric</strong> <strong>difference</strong> operator <strong>^</strong> gets items in either set, but not both.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs livecodeserver\"><span class=\"hljs-keyword\">first</span> = {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">6</span>}\r\n<span class=\"hljs-keyword\">second</span> = {<span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">6</span>, <span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">9</span>}\r\n\r\nprint(<span class=\"hljs-keyword\">first</span> | <span class=\"hljs-keyword\">second</span>)\r\nprint(<span class=\"hljs-keyword\">first</span> & <span class=\"hljs-keyword\">second</span>)\r\nprint(<span class=\"hljs-keyword\">first</span> - <span class=\"hljs-keyword\">second</span>)\r\nprint(<span class=\"hljs-keyword\">second</span> - <span class=\"hljs-keyword\">first</span>)\r\nprint(<span class=\"hljs-keyword\">first</span> ^ <span class=\"hljs-keyword\">second</span>)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>{1, 2, 3, 4, 5, 6, 7, 8, 9} <br>\r\n {4, 5, 6} <br>\r\n {1, 2, 3} <br>\r\n {8, 9, 7} <br>\r\n {1, 2, 3, 7, 8, 9}</p>\r\n</blockquote>\r\n\r\n<p>StackEdit stores your documents in your browser, which means all your documents are automatically saved locally and are accessible <strong>offline!</strong></p>\r\n\r\n\r\n\r\n<h3 id=\"data-structures\"><strong>Data Structures</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>As we have seen in the previous lessons, Python supports the following data structures: <strong>lists</strong>, <strong>dictionaries</strong>, <strong>tuples</strong>, <strong>sets</strong>.</p>\r\n\r\n<p><strong>When to use a dictionary:</strong></p>\r\n\r\n<ul>\r\n<li>When you need a logical association between a <strong>key:value pair</strong>.</li>\r\n<li>When you need <strong>fast lookup</strong> for your data, based on a custom <strong>key</strong>.</li>\r\n<li>When your data is being constantly modified. Remember, dictionaries are <strong>mutable</strong>.</li>\r\n</ul>\r\n\r\n<p><strong>When to use the other types:</strong></p>\r\n\r\n<ul>\r\n<li>Use <strong>lists</strong> if you have a collection of data that does <strong>not need random access</strong>. Try to choose lists when you need a simple, iterable collection that is modified frequently.</li>\r\n<li>Use a <strong>set</strong> if you need <strong>uniqueness</strong> for the elements. </li>\r\n<li>Use <strong>tuples</strong> when your data cannot change. </li>\r\n</ul>\r\n\r\n<blockquote>\r\n <p>Many times, a <strong>tuple</strong> is used in combination with a <strong>dictionary</strong>, for example, a <strong>tuple</strong> might represent a key, because it’s <strong>immutable</strong>.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"itertools-1\"><strong>itertools-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>The module <strong>itertools</strong> is a standard library that contains several functions that are useful in functional programming. <br>\r\nOne type of function it produces is <strong>infinite</strong> iterators. <br>\r\nThe function <strong>count</strong> counts up infinitely from a value. <br>\r\nThe function <strong>cycle</strong> infinitely iterates through an iterable (for instance a list or string). <br>\r\nThe function <strong>repeat</strong> repeats an object, either infinitely or a specific number of times. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-keyword\">from</span> itertools <span class=\"hljs-keyword\">import</span> count\r\n\r\n<span class=\"hljs-keyword\">for</span> i <span class=\"hljs-keyword\">in</span> count(<span class=\"hljs-number\">3</span>):\r\n print(i)\r\n <span class=\"hljs-keyword\">if</span> i >=<span class=\"hljs-number\">11</span>:\r\n <span class=\"hljs-keyword\">break</span></code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>3 <br>\r\n 4 <br>\r\n 5 <br>\r\n 6 <br>\r\n 7 <br>\r\n 8 <br>\r\n 9 <br>\r\n 10 <br>\r\n 11</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"itertools-2\"><strong>itertools-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>There are many functions in <strong>itertools</strong> that operate on iterables, in a similar way to <strong>map</strong> and <strong>filter</strong>. <br>\r\n<strong>Some examples:</strong> <br>\r\n<strong>takewhile</strong> - takes items from an iterable while a predicate function remains true; <br>\r\n<strong>chain</strong> - combines several iterables into one long one; <br>\r\n<strong>accumulate</strong> - returns a running total of values in an iterable.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-keyword\">from</span> itertools <span class=\"hljs-keyword\">import</span> accumulate, takewhile\r\n\r\nnums = list(accumulate(range(<span class=\"hljs-number\">8</span>)))\r\nprint(nums)\r\nprint(list(takewhile(<span class=\"hljs-keyword\">lambda</span> x: x<= <span class=\"hljs-number\">6</span>, nums)))</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>[0, 1, 3, 6, 10, 15, 21, 28] <br>\r\n [0, 1, 3, 6]</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"itertools-3\"><strong>itertools-3</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>There are also several combinatoric functions in <strong>itertool</strong>, such as <strong>product</strong> and <strong>permutation</strong>. <br>\r\nThese are used when you want to accomplish a task with all possible combinations of some items. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs php\">from itertools import product, permutations\r\n\r\nletters = (<span class=\"hljs-string\">\"A\"</span>, <span class=\"hljs-string\">\"B\"</span>)\r\n<span class=\"hljs-keyword\">print</span>(<span class=\"hljs-keyword\">list</span>(product(letters, range(<span class=\"hljs-number\">2</span>))))\r\n<span class=\"hljs-keyword\">print</span>(<span class=\"hljs-keyword\">list</span>(permutations(letters))) </code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>[(‘A’, 0), (‘A’, 1), (‘B’, 0), (‘B’, 1)] <br>\r\n [(‘A’, ‘B’), (‘B’, ‘A’)]</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h2 id=\"object-oriented-programming\"><strong><em>Object-Oriented Programming</em></strong></h2>\r\n\r\n\r\n\r\n<h3 id=\"classes-1\"><strong>Classes-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>We have previously looked at two paradigms of programming - <strong>imperative</strong> (using statements, loops, and functions as subroutines), and <strong>functional</strong> (using pure functions, higher-order functions, and recursion).</p>\r\n\r\n<p>Another very popular paradigm is <strong>object**</strong>-<strong>**oriented</strong> <strong>programming</strong> (<strong>OOP</strong>). <br>\r\nObjects are created using <strong>classes</strong>, which are actually the focal point of OOP. <br>\r\nThe <strong>class</strong> describes what the object will be, but is separate from the object itself. In other words, a class can be described as an object’s blueprint, description, or definition. <br>\r\nYou can use the same class as a blueprint for creating multiple different objects. </p>\r\n\r\n<p>Classes are created using the keyword <strong>class</strong> and an indented block, which contains class <strong>methods</strong> (which are functions). <br>\r\nBelow is an example of a simple class and its objects.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Cat</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, color, legs)</span>:</span>\r\n self.color = color\r\n self.legs = legs\r\n\r\nfelix = Cat(<span class=\"hljs-string\">\"ginger\"</span>, <span class=\"hljs-number\">4</span>)\r\nrover = Cat(<span class=\"hljs-string\">\"dog-colored\"</span>, <span class=\"hljs-number\">4</span>)\r\nstumpy = Cat(<span class=\"hljs-string\">\"brown\"</span>, <span class=\"hljs-number\">3</span>)</code></pre>\r\n\r\n<blockquote>\r\n <p><strong>Note:</strong> This code defines a class named <strong>Cat</strong>, which has two attributes: <strong>color</strong> and <strong>legs</strong>. <br>\r\n Then the class is used to create 3 separate objects of that class.</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h4 id=\"init\"><strong><code>__init__</code></strong></h4>\r\n\r\n<hr>\r\n\r\n<p>The <strong>init</strong> method is the most important method in a class. <br>\r\nThis is called when an instance (object) of the class is created, using the class name as a function.</p>\r\n\r\n<p>All methods must have <strong>self</strong> as their first parameter, although it isn’t explicitly passed, Python adds the <strong>self</strong> argument to the list for you; you do not need to include it when you call the methods. Within a method definition, <strong>self</strong> refers to the instance calling the method.</p>\r\n\r\n<p>Instances of a class have <strong>attributes</strong>, which are pieces of data associated with them. <br>\r\nIn this example, <strong>Cat</strong> instances have attributes <strong>color</strong> and <strong>legs</strong>. These can be accessed by putting a <strong>dot</strong>, and the attribute name after an instance. <br>\r\nIn an <strong>init</strong> method, <strong>self.attribute</strong> can therefore be used to set the initial value of an instance’s attributes. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Cat</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, color, legs)</span>:</span>\r\n self.color = color\r\n self.legs = legs\r\n\r\nfelix = Cat(<span class=\"hljs-string\">\"ginger\"</span>, <span class=\"hljs-number\">4</span>)\r\nprint(felix.color)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p><strong>Note:</strong> ginger</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> In the example above, the <strong>init</strong> method takes two arguments and assigns them to the object’s attributes. The <strong>init</strong> method is called the class <strong>constructor</strong>.</p>\r\n\r\n\r\n\r\n<h4 id=\"methods\"><strong>Methods</strong></h4>\r\n\r\n<hr>\r\n\r\n<p>Classes can have other <strong>methods</strong> defined to add functionality to them. <br>\r\nRemember, that all methods must have <strong>self</strong> as their first <strong>parameter</strong>. <br>\r\nThese methods are accessed using the same <strong>dot syntax</strong> as attributes. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Dog</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, name, color)</span>:</span>\r\n self.name = name\r\n self.color = color\r\n\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">bark</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"Woof!\"</span>)\r\n\r\nfido = Dog(<span class=\"hljs-string\">\"Fido\"</span>, <span class=\"hljs-string\">\"brown\"</span>)\r\nprint(fido.name)\r\nfido.bark()</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>Fido <br>\r\n Woof!</p>\r\n</blockquote>\r\n\r\n<p>Classes can also have <strong>class attributes</strong>, created by assigning variables within the body of the class. These can be accessed either from instances of the class, or the class itself. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Dog</span>:</span>\r\n legs = <span class=\"hljs-number\">4</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, name, color)</span>:</span>\r\n self.name = name\r\n self.color = color\r\n\r\nfido = Dog(<span class=\"hljs-string\">\"Fido\"</span>, <span class=\"hljs-string\">\"brown\"</span>)\r\nprint(fido.legs)\r\nprint(Dog.legs)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>4 <br>\r\n 4</p>\r\n</blockquote>\r\n\r\n<p><strong>NOTE:</strong> Class attributes are shared by all instances of the class.</p>\r\n\r\n\r\n\r\n<h3 id=\"classes-2\"><strong>Classes-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Trying to access an attribute of an instance that isn’t defined causes an <strong>AttributeError</strong>. This also applies when you call an <strong>undefined</strong> method.</p>\r\n\r\n<p><strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Rectangle</span>:</span> \r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, width, height)</span>:</span>\r\n self.width = width\r\n self.height = height\r\n\r\nrect = Rectangle(<span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">8</span>)\r\nprint(rect.color)</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>AttributeError: ‘Rectangle’ object has no attribute ‘color’</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"inheritance-1\"><strong>Inheritance-1</strong></h3>\r\n\r\n<hr>\r\n\r\n<p><strong>Inheritance</strong> provides a way to share functionality between classes. <br>\r\nImagine several classes, <strong>Cat</strong>, <strong>Dog</strong>, <strong>Rabbit</strong> and so on. Although they may differ in some ways (only <strong>Dog</strong> might have the method <strong>bark</strong>), they are likely to be similar in others (all having the attributes <strong>color</strong> and <strong>name</strong>). <br>\r\nThis similarity can be expressed by making them all inherit from a <strong>superclass</strong> <strong>Animal</strong>, which contains the shared functionality. <br>\r\nTo inherit a class from another class, put the superclass name in parentheses after the class name. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Animal</span>:</span> \r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, name, color)</span>:</span>\r\n self.name = name\r\n self.color = color\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Cat</span><span class=\"hljs-params\">(Animal)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">purr</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"Purr...\"</span>)\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Dog</span><span class=\"hljs-params\">(Animal)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">bark</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"Woof!\"</span>)\r\n\r\nfido = Dog(<span class=\"hljs-string\">\"Fido\"</span>, <span class=\"hljs-string\">\"brown\"</span>)\r\nprint(fido.color)\r\nfido.bark()</code></pre>\r\n\r\n<p><strong>Result</strong>:</p>\r\n\r\n<blockquote>\r\n <p>brown <br>\r\n Woof!</p>\r\n</blockquote>\r\n\r\n\r\n\r\n<h3 id=\"inheritance-2\"><strong>Inheritance-2</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>A class that inherits from another class is called a <strong>subclass</strong>. <br>\r\nA class that is inherited from is called a <strong>superclass</strong>. <br>\r\nIf a class inherits from another with the same attributes or methods, it overrides them.</p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Wolf</span>:</span> \r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">__init__</span><span class=\"hljs-params\">(self, name, color)</span>:</span>\r\n self.name = name\r\n self.color = color\r\n\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">bark</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"Grr...\"</span>)\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Dog</span><span class=\"hljs-params\">(Wolf)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">bark</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"Woof\"</span>)\r\n\r\nhusky = Dog(<span class=\"hljs-string\">\"Max\"</span>, <span class=\"hljs-string\">\"grey\"</span>)\r\nhusky.bark()</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>Woof</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> In the example above, Wolf is the superclass, Dog is the subclass.</p>\r\n\r\n\r\n\r\n<h3 id=\"inheritance-3\"><strong>Inheritance-3</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>Inheritance can also be <strong>indirect</strong>. One class can inherit from another, and that class can inherit from a third class. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">A</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">method</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"A method\"</span>)\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">B</span><span class=\"hljs-params\">(A)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">another_method</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"B method\"</span>)\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">C</span><span class=\"hljs-params\">(B)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">third_method</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-string\">\"C method\"</span>)\r\n\r\nc = C()\r\nc.method()\r\nc.another_method()\r\nc.third_method()</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>A method <br>\r\n B method <br>\r\n C method</p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> However, circular inheritance is not possible.</p>\r\n\r\n\r\n\r\n<h3 id=\"inheritance-4\"><strong>Inheritance-4</strong></h3>\r\n\r\n<hr>\r\n\r\n<p>The function <strong>super</strong> is a useful inheritance-related function that refers to the parent class. It can be used to find the method with a certain name in an object’s superclass. <br>\r\n<strong>Example:</strong></p>\r\n\r\n\r\n\r\n<pre class=\"prettyprint\"><code class=\" hljs python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">A</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">spam</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-number\">1</span>)\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">B</span><span class=\"hljs-params\">(A)</span>:</span>\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">spam</span><span class=\"hljs-params\">(self)</span>:</span>\r\n print(<span class=\"hljs-number\">2</span>)\r\n super().spam()\r\n\r\nB().spam()</code></pre>\r\n\r\n<p><strong>Result:</strong></p>\r\n\r\n<blockquote>\r\n <p>2 <br>\r\n 1 </p>\r\n</blockquote>\r\n\r\n<p><strong>Note:</strong> <strong><em>super().spam()</em></strong> calls the <strong>spam</strong> method of the superclass.</p>",
"note": "Don't delete this file! It's used internally to help with page regeneration."
}