<?xml version="1.0" encoding="UTF-8" standalone="no"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4628146849449914855</atom:id><lastBuildDate>Mon, 07 Sep 2026 16:36:53 +0000</lastBuildDate><category>python</category><category>django</category><category>programming</category><category>dictionary</category><category>FileField</category><category>MySql</category><category>What are Lambda expressions in python</category><category>cached sessions</category><category>call by reference</category><category>call by value</category><category>comparison</category><category>cookie</category><category>cookie based sessions</category><category>database</category><category>datatypes</category><category>decorators</category><category>design patterns</category><category>dictionary comprehension</category><category>django forms</category><category>django session</category><category>document upload</category><category>file</category><category>file bases sessions</category><category>file upload</category><category>functions</category><category>generator</category><category>generator comprehension</category><category>gil</category><category>image</category><category>indexing</category><category>inheritance</category><category>interview questions</category><category>iterator</category><category>lambda</category><category>list comprehension</category><category>memory management</category><category>middlewares</category><category>multi-threading</category><category>multiprocessing</category><category>persistent cookie</category><category>pickling</category><category>process</category><category>python packages</category><category>session cookie</category><category>set comprehension</category><category>slicing</category><category>threads</category><category>unpickling</category><category>web development</category><title>Python Basics</title><description></description><link>https://py-basics.blogspot.com/</link><managingEditor>noreply@blogger.com (sudhakar mishra)</managingEditor><generator>Blogger</generator><openSearch:totalResults>27</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle/><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-1089401702366237131</guid><pubDate>Sat, 22 Apr 2017 16:29:00 +0000</pubDate><atom:updated>2019-06-14T07:44:17.059-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">datatypes</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What are the different data types in Python?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Python Datatypes&lt;/b&gt;:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Lists&lt;/li&gt;
&lt;li&gt;Tuples&lt;/li&gt;
&lt;li&gt;Sets&lt;/li&gt;
&lt;li&gt;Dictionaries&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Boolean&lt;/b&gt;:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Booleans are either true or false.&lt;/li&gt;
&lt;li&gt;Python has 2 constants named True and False which can be used to assign a boolean value directly.&lt;/li&gt;
&lt;li&gt;In certain places ( like if statements ), Python expects an expression to a boolean value, these places are called boolean contexts.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Numbers&lt;/b&gt;:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Python supports both int and floating point numbers.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Tuples&lt;/b&gt;:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;A tuple is an immutable list, it means a tuple can't be changed in a way once it's created.&lt;/li&gt;
&lt;li&gt;A tuple is defined in the same way as a list, except that the whole set of an element is enclosed in the parenthesis instead of square brackets.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Similarities with the list&lt;/b&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Tuples have a defined order i.e. Indices are 0 based, so the first element of a non-empty tuple is always a_tuple[ 0 ].&lt;/li&gt;
&lt;li&gt;Negative indices count from the end of the tuple.&lt;/li&gt;
&lt;li&gt;Slicing works too, when we slice a list we get a new list. When we slice a tuple, we get a new tuple.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;b&gt;The difference with lists&lt;/b&gt;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Tuple can't be changed. In technical terms, tuples are immutable in practical terms, tuples have no methods that would allow to change them. List have methods like append() , extend(), insert() , remove() and pop().&lt;/li&gt;
&lt;li&gt;Tuples are faster than lists. In CPython, tuples are stored in a single block of memory, so creating a new tuple involves at worst a single call to allocate memory. Lists are allocated in two blocks: the fixed one with all the Python object information and a variable sized block for the data. That's part of the reason why creating a tuple is faster, but it probably also explains the slight difference in indexing speed as there is one fewer pointer to follow.&lt;/li&gt;
&lt;li&gt;Tuples make code safer since they are "write-protect".&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Sets &lt;/b&gt;:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;A set is an unordered "bag" of unique values.&lt;/li&gt;
&lt;li&gt;A single set can contain values of any immutable datatype.&lt;/li&gt;
&lt;li&gt;Once we have two sets, we can do standard operations like union, intersection and set difference.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/python-data-types.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>12</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7922449380547267354</guid><pubDate>Sat, 22 Apr 2017 13:55:00 +0000</pubDate><atom:updated>2017-04-22T06:55:55.502-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">database</category><category domain="http://www.blogger.com/atom/ns#">MySql</category><title>What is composite key?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Composite Key&lt;/b&gt; :&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table.&lt;/li&gt;
&lt;li&gt;When the columns are combined uniqueness is guaranteed but when it taken individually it does not guarantee uniqueness.&lt;/li&gt;
&lt;li&gt;Sometimes more than one attributes are needed to uniquely identify an entity.&lt;/li&gt;
&lt;li&gt;A primary key that is made by the combination of more than one attribute is known as a composite key.&lt;/li&gt;
&lt;li&gt;Ex :&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;Create table abc ( c1 int , c2 varchar(20), c3 varchar(100) , PRIMARY KEY ( c1, c2 ) );&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/what-is-composite-key.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-5321812236469824868</guid><pubDate>Sat, 22 Apr 2017 04:34:00 +0000</pubDate><atom:updated>2019-06-14T00:16:15.308-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">pickling</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">python packages</category><category domain="http://www.blogger.com/atom/ns#">unpickling</category><title>Explain pickling and unpickling in python.</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Pickling and Unpickling&lt;/b&gt;:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Pickle is a standard module which serializes and deserializes a python object structure.&lt;/li&gt;
&lt;li&gt;Pickle module accepts any python object, converts it into a string representation and dumps into a file(by using dump() function ) which can be used later this process is called pickling.&lt;/li&gt;
&lt;li&gt;Unpickling is a process of retrieving original python object from the stored string representation for use.&lt;/li&gt;
&lt;li&gt;Pickling is a process whereby a python object hierarchy is converted into a byte stream and unpickling is the inverse operation, whereby a byte stream is converted back into an object hierarchy.&lt;/li&gt;
&lt;li&gt;Ex:&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;import pickle&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;a = [ "apple" , "banana"&amp;nbsp;]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;file_Name = "abc"&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;# open the file for writing&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;fileObj = open( file_Name , "wb" )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;# This writes the object a to the file&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;pickle.dump( a , fileObj)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;fileObj.close()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;fileObj = open( file_Name , "r")&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;# load the object from the file into variable&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;b = pickle.load( fileObj )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;print( a == b ) &amp;nbsp;# True&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/pickling.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-1460244371552248797</guid><pubDate>Fri, 21 Apr 2017 19:46:00 +0000</pubDate><atom:updated>2019-06-14T08:06:50.146-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">dictionary comprehension</category><category domain="http://www.blogger.com/atom/ns#">generator comprehension</category><category domain="http://www.blogger.com/atom/ns#">list comprehension</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">set comprehension</category><title>What is list comprehension? Also explain generator comprehension, set comprehension and dictionary comprehension?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;List Comprehension&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;List comprehension was introduced in 2.0.&lt;/li&gt;
&lt;li&gt;List comprehension is an elegant way to define and create a list in Python.&lt;/li&gt;
&lt;li&gt;List comprehension is a complete substitute for the lambda function as well as the functions map(), filter() and reduce().&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; Celsius = [39.2, 36.5, 37.3, 37.8]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; Fahrenheit = [ ((float(9)/5)*x + 32) for x in Celsius ]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; print Fahrenheit&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[102.56, 97.700000000000003, 99.140000000000001, 100.03999999999999]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Generator Comprehension &lt;/b&gt;:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Generator comprehensions were introduced with Python 2.6.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;They are simply a generator expression with a parenthesis - round brackets - around it. Otherwise, the syntax and the way of working is like list comprehension, but a generator comprehension returns a generator instead of a list.&lt;/li&gt;
&lt;/ul&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; x = (x **2 for x in range(20))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; print(x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;at 0xb7307aa4&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; x = list(x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; print(x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Set Comprehension&lt;/b&gt; :&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;A set comprehension is similar to a list comprehension but returns a set and not a list.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Syntactically, we use curly brackets instead of square brackets to create a set.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;We are able to create the set of non primes without doublets:&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; from math import sqrt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; n = 100&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; sqrt_n = int(sqrt(n))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; no_primes = {j for i in range(2,sqrt_n) for j in range(i*2, n, i)}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; no_primes&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; primes = {i for i in range(n) if i not in no_primes}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; print(primes)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;{0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Dictionary Comprehensions &lt;/b&gt;:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;There are dictionary comprehensions in Python 2.7+.&lt;/li&gt;
&lt;li&gt;Like a list comprehension, they create a new dictionary; you can't use them to add keys to an existing dictionary. Also, you have to specify the keys and values, although of course you can specify a dummy value if you like.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; d = {n: n**2 for n in range(5)}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; print d&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;my_list = ['hello', 'hi', 'hello', 'today', 'morning', 'again', 'hello']&lt;/span&gt;&lt;/div&gt;
&lt;span style="color: #444444;"&gt;
&lt;/span&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;my_dict = {k:my_list.count(k) for k in my_list}&lt;/span&gt;&lt;/div&gt;
&lt;span style="color: #444444;"&gt;
&lt;div&gt;
print(my_dict)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
{'again': 1, 'hi': 1, 'hello': 3, 'today': 1, 'morning': 1}&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/list-comprehension.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-9013170947841405981</guid><pubDate>Fri, 21 Apr 2017 18:16:00 +0000</pubDate><atom:updated>2017-04-21T11:16:38.201-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">indexing</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">slicing</category><title>Explain slicing and indexing operations.</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Slicing and Indexing operations&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Indexing operation allows to access a particular item in the sequence directly.&lt;/li&gt;
&lt;li&gt;Slicing operation allows to retrieve a part of the sequence.&lt;/li&gt;
&lt;li&gt;In slicing :&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="text-align: center;"&gt;
&lt;span style="color: #444444;"&gt;S[ StartNo : StopNo : Step ]&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;If the start no is omitted, Python will start at the beginning of the sequence.&lt;/li&gt;
&lt;li&gt;If the stop no is omitted, Python will stop at the end of the sequence.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a = [1,2,3,4]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a[0:2] # take items 0-2, upper bound noninclusive&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[1, 2]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a[0:-1] #take all but the last&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[1, 2, 3]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a[1:4]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[2, 3, 4]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a[::-1] # reverse the list&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[4, 3, 2, 1]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; a[::2] # skip 2&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;[1, 3]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/slicing-indexing.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-8681855620323646004</guid><pubDate>Fri, 21 Apr 2017 07:09:00 +0000</pubDate><atom:updated>2017-04-21T00:09:51.771-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">cookie</category><category domain="http://www.blogger.com/atom/ns#">persistent cookie</category><category domain="http://www.blogger.com/atom/ns#">session cookie</category><category domain="http://www.blogger.com/atom/ns#">web development</category><title>What is persistent cookie?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Persistent Cookie&lt;/b&gt; :&lt;br /&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Also called a permanent cookie, or a stored cookie, a cookie that is stored on a user's hard drive until it expires (persistent cookies are set with expiration dates) or until the user deletes the cookie.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Persistent cookies are used to collect identifying information about the user, such as Web surfing behavior or user preferences for a specific Web site.&lt;/li&gt;
&lt;li&gt;A persistent cookie enables a website to remember you on subsequent visits, speeding up or enhancing your experience of services or functions offered.&lt;/li&gt;
&lt;li&gt;For example, a website may offer its contents in different languages. On my first visit, I may choose to have the content delivered in French and the site may record that preference in a persistent cookie set on your browser. When you revisit that site it will use the cookie to ensure that the content is delivered in French.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Session Cookie &lt;/b&gt;:&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Also called a transient cookie, a cookie that is erased when the user closes the Web browser. The session cookie is stored in temporary memory and is not retained after the browser is closed. Session cookies do not collect information from the user's computer.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;They typically will store information in the form of a session identification that does not personally identify the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/persistent-cookie.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7992057436713855384</guid><pubDate>Sun, 16 Apr 2017 10:10:00 +0000</pubDate><atom:updated>2017-04-22T20:33:28.262-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">django</category><category domain="http://www.blogger.com/atom/ns#">interview questions</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Interview questions and answers</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Ques: &lt;b&gt;How are the functions help() and dir() different?&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;
Ans:&amp;nbsp;These are the two functions that are accessible from the Python Interpreter. These two functions are used for viewing a consolidated dump of built-in functions.&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;help :&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;It will display the documentation string. It is used to see the help related to modules, keywords, attributes, etc.&lt;/li&gt;
&lt;li&gt;To view the help for a keyword, topics, you just need to type, help("&amp;lt;keyword&amp;gt;")&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;dir :&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Without arguments, it will return list of names in the current local scope.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;With an argument, it will return a list of valid attributes for that object.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;Which statement of Python is used whenever a statement is required syntactically but the program needs no action?&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Ans: p&lt;span style="background-color: white; font-family: , &amp;quot;proximanova&amp;quot; ,; font-size: 16px;"&gt;ass is no-operation / action statement in Python.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;&lt;span style="background-color: white;"&gt;If we want to get data from a model&amp;nbsp;&lt;/span&gt;and even if the requested data does not exist, we want to continue with other tasks. In such a scenario, use try-except block with pass statement in the except block. Ex:&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;obj = None&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;try :&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; obj = ModelName.objects.get(pk=112)&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;except :&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; pass&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #444444; font-family: , &amp;quot;proximanova&amp;quot; ,;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Ques: &lt;b&gt;What is a Python module?&lt;/b&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Ans: A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension. zip files and DLL files can also be modules.Inside the module, you can refer to the module name as a string that is stored in the global variable name .&lt;/div&gt;
&lt;div&gt;
A module can be imported by other modules in one of the two ways. They are&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;import&lt;/li&gt;
&lt;li&gt;from module-name import&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;Name the File-related modules in Python?&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Ans: Python provides libraries / modules with functions that enable you to manipulate text files and binary files on file system. Using them you can create files, update their contents, copy, and delete files. The libraries are : os, os.path, and shutil.&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;os and os.path – modules include functions for accessing the filesystem&lt;/li&gt;
&lt;li&gt;shutil – module enables you to copy and delete the files.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;Explain the shortest way to open a text file and display its contents?&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Ans:&amp;nbsp;The shortest way to open a text file is by using “with” command as follows:&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;with open("file-name", "r") as fp:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;fileData = fp.read()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;#to print the contents of the file print(fileData)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;How do you create a dictionary which can preserve the order of pairs?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans: We know that regular Python dictionaries iterate over &amp;lt;key, value&amp;gt; pairs in an arbitrary order, hence they do not preserve the insertion order of &amp;lt;key, value&amp;gt; pairs.&lt;/div&gt;
&lt;div&gt;
Python 2.7. introduced a new “OrderDict” class in the “collections” module and it provides the same interface like the general dictionaries but it traverse through keys and values in an ordered manner depending on when a key was first inserted.&lt;/div&gt;
&lt;div&gt;
Ex:&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;from collections import OrderedDict&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;d = OrderDict([('Company-id':1),('Company-Name':'ABC')])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;d.items() # displays the output as: [('Company-id':1),('Company-Name':'ABC')]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Ques: &lt;b&gt;When does a dictionary is used instead of a list?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans: Dictionaries are best suited when the data is labelled, i.e., the data is a record with field names.&lt;/div&gt;
&lt;div&gt;
Lists are better option to store collections of un-labelled items say all the files and sub directories in a folder.&lt;/div&gt;
&lt;div&gt;
Generally search operation on dictionary object is faster than searching a list object.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;What is the use of enumerate() in Python?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans: Using enumerate() function we can iterate through the sequence and retrieve the index position and its corresponding value at the same time.&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;for i,v in enumerate([‘abc’,’123’,’xyz’]):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;print(i,v)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# 0 abc&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# 1 123&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;# 2 xyz&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Ques: &lt;b&gt;How many kinds of sequences are supported by Python? What are they?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans:&amp;nbsp;Python supports 7 sequence types. They are str, list, tuple, unicode, bytearray, xrange, and buffer. where xrange is deprecated in python 3.5.X.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;Is Python object oriented? what is object oriented programming?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans : Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:&lt;/div&gt;
&lt;div&gt;
Encapsulation, Data Abstraction, Inheritance, Polymorphism.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Ques:&amp;nbsp;&lt;b&gt;How instance variables are different from class variables?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Ans: Instance variables are the variables in an object that have values that are local to that object. Two objects of the same class maintain distinct values for their variables. These variables are accessed with “object-name.instancevariable-name”.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Class variables are the variables of class. All the objects of the same class will share value of “Class variables. They are accessed with their class name alone as “class- name.classvariable-name”. If you change the value of a class variable in one object, its new value is visible among all other objects of the same class. In the Java world, a variable that is declared as static is a class variable.&lt;br /&gt;
&lt;br /&gt;
Ques:&amp;nbsp;&lt;b&gt;What is the reason for the try-except-else to exist?&lt;/b&gt;&lt;br /&gt;
Ans:&amp;nbsp;A try block allows you to handle a expected error. The except block should only catch exceptions you are prepared to handle. If you handle an unexpected error, your code may do the wrong thing and hide bugs.&lt;br /&gt;
&lt;br /&gt;
The else-clause runs when there is no exception but before the finally-clause. That is its primary purpose.&lt;br /&gt;
&lt;br /&gt;
The most common use of an else-clause in a try-block is for a bit of beautification (aligning the exceptional outcomes and non-exceptional outcomes at the same level of indentation). This use is always optional and isn't strictly necessary.&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp;n = Input("enter a number")&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp;try:&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;recip = 1 / n&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp;except ZeroDivisionError:&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;logging.info('Infinite result')&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp;else:&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;logging.info('Finite result')&lt;/span&gt;&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/python-interview-questions.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7339781290451581979</guid><pubDate>Sun, 16 Apr 2017 05:00:00 +0000</pubDate><atom:updated>2019-06-13T08:01:58.540-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">cached sessions</category><category domain="http://www.blogger.com/atom/ns#">cookie based sessions</category><category domain="http://www.blogger.com/atom/ns#">django</category><category domain="http://www.blogger.com/atom/ns#">django session</category><category domain="http://www.blogger.com/atom/ns#">file bases sessions</category><title>How sessions work in Django?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;The session framework stores and retrieves data.&lt;/li&gt;
&lt;li&gt;It stores the data on the server side and sends and receives the cookies.&lt;/li&gt;
&lt;li&gt;Cookies contain a session ID, not the data itself unless we are using the cookie-based sessions.&lt;/li&gt;
&lt;li&gt;Django provides support for anonymous sessions.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Configuring the session engine:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
By default, Django stores sessions in our database. We can configure Django to store session data using these sessions engines:&lt;/div&gt;
&lt;div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Database-backed sessions&lt;/li&gt;
&lt;li&gt;Cached sessions&lt;/li&gt;
&lt;li&gt;File-based sessions&lt;/li&gt;
&lt;li&gt;Cookie-based sessions&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;u&gt;Database-backed sessions:&lt;/u&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;To use databased-backed sessions we need to add "django.contrib.sessions" to our&amp;nbsp;INSTALLED_APPS setting.&lt;/li&gt;
&lt;li&gt;After this configuration, we need to run manage.py migrate to install the database table that stores session data.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;u&gt;Cached sessions:&lt;/u&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;For better performance, we can use a cached-based session backend.&lt;/li&gt;
&lt;li&gt;We should only use it if we are using Memcached cache backend.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;u&gt;File-based sessions:&lt;/u&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;To use file-based sessions, set the SESSION_ENGINE setting to "django.contrib.sessions.backends.file".&lt;/li&gt;
&lt;li&gt;We can set SESSION_FILE_PATH setting (which defaults to output from tempfile.gettempdir(), most likely /tmp) to control where Django stores session files.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;u&gt;Cookie-based sessions:&lt;/u&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;To use cookies-based session, set the&amp;nbsp;SESSION_ENGINE setting to "django.contrib.sessions.backends.signed_cookies".&lt;/li&gt;
&lt;li&gt;If we use cookie-based sessions, pay extra care that our secret key is always kept completely secret, for any system which might be remotely accessible.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;&lt;ul&gt;&lt;ul&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/how-sessions-work-in-django.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-2543925445855086648</guid><pubDate>Thu, 13 Apr 2017 19:25:00 +0000</pubDate><atom:updated>2019-06-13T08:02:52.680-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">django</category><category domain="http://www.blogger.com/atom/ns#">middlewares</category><title>What is middleware in django?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Middleware&lt;/b&gt;&amp;nbsp;:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Middleware is a framework of hooks into Django’s request/response processing.&lt;/li&gt;
&lt;li&gt;Middleware is a lightweight plugin for Django.&lt;/li&gt;
&lt;li&gt;It allows modifying request or response or the view parameters.&lt;/li&gt;
&lt;li&gt;Each middleware component is responsible for doing some specific function. For example, Django includes a middleware component, AuthenticationMiddleware, that associates users with requests using sessions.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;When to use middleware&lt;/u&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;We can use middleware if you want to modify the request i.e HttpRequest object which is sent to the view. Or you might want to modify the HttpResponse object returned from the view. Both these can be achieved by using middleware.&lt;/li&gt;
&lt;li&gt;We might want to perform an operation before the view executes. In such case, we would use a middleware.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;b&gt;Hooks and Application order&lt;/b&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;u&gt;Request phase&lt;/u&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;During the request phase, before calling the view. Django applies middleware in the order it's defined in MIDDLE_CLASSES top-down.&lt;/li&gt;
&lt;li&gt;2 hooks are available :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;process_request() is called on each request before Django decides which view to execute.&lt;/li&gt;
&lt;li&gt;process_view() is called just before Django calls the view.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Response phase&lt;/u&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;During the response phase, after calling the view, middlewares are applied in reverse order from the bottom up.&lt;/li&gt;
&lt;li&gt;3 hooks are available :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;process_exception() :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;It is called only if the view raised an exception&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;process_template_response() :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;It is called only for template responses&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;process_response() :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;It is called on all responses before they are returned to the browser.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li&gt;It must return a HttpResponse or StreamingHttpResponse.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmXt0V7vpM2RXBosJCm9GtQUZEPwtuNl4yuqvmw5F-GfsBd_T9chTOfhK-k0bt2QNMtIAFNYoMK6m_ehqTjw3-FCgIbW2nNsixw5QQlIe9WP1NSKlNzqMUNtRUqIFjLCxq4h2N2wFVTeur/s1600/djangomiddlewares.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmXt0V7vpM2RXBosJCm9GtQUZEPwtuNl4yuqvmw5F-GfsBd_T9chTOfhK-k0bt2QNMtIAFNYoMK6m_ehqTjw3-FCgIbW2nNsixw5QQlIe9WP1NSKlNzqMUNtRUqIFjLCxq4h2N2wFVTeur/s400/djangomiddlewares.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/what-is-middleware-in-django.html</link><author>noreply@blogger.com (sudhakar mishra)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmXt0V7vpM2RXBosJCm9GtQUZEPwtuNl4yuqvmw5F-GfsBd_T9chTOfhK-k0bt2QNMtIAFNYoMK6m_ehqTjw3-FCgIbW2nNsixw5QQlIe9WP1NSKlNzqMUNtRUqIFjLCxq4h2N2wFVTeur/s72-c/djangomiddlewares.png" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-3126936519157773522</guid><pubDate>Tue, 11 Apr 2017 07:17:00 +0000</pubDate><atom:updated>2017-04-13T11:59:17.868-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">multi-threading</category><category domain="http://www.blogger.com/atom/ns#">multiprocessing</category><category domain="http://www.blogger.com/atom/ns#">process</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">threads</category><title>What is difference between multi-threading and multiprocessing?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Multi-threading&amp;nbsp;vs Multiprocessing&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;The threading module uses threads, the multiprocessing uses processes. The difference is that threads run in the same memory space, while processes have separate memory.This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory at the same time. This is what the global interpreter lock is for.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;Multiprocessing&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;u&gt;Pross&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Separate memory space&lt;/li&gt;
&lt;li&gt;Takes advantage of multiple CPUs &amp;amp; cores&lt;/li&gt;
&lt;li&gt;Avoids GIL limitations for cPython&lt;/li&gt;
&lt;li&gt;Child processes are interruptible/killable&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Cons&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Inter-process communication (IPC)&amp;nbsp;a little more complicated with more overhead.&lt;/li&gt;
&lt;li&gt;Larger memory footprint.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Multi-threading&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;u&gt;Pross&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Lightweight - low memory footprint.&lt;/li&gt;
&lt;li&gt;Shared memory - makes access to state from another context easier.&lt;/li&gt;
&lt;li&gt;Allows you to easily make responsive UIs.&lt;/li&gt;
&lt;li&gt;cPython C extension modules that properly release the GIL will run in parallel.&lt;/li&gt;
&lt;li&gt;Great option for I/O-bound applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Cons&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Not interruptible / killable.&lt;/li&gt;
&lt;li&gt;&lt;/li&gt;
&lt;li&gt;Code is usually harder to understand and to get right - the potential for race conditions increases dramatically&lt;/li&gt;
&lt;li&gt;It will not resume the task if server restarts for any reason.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNXenJAWCeV5_AV737OnjQp_5VuA9p6X2zjTWEfx1lyUVvWAHOQ4bLheAxlfL9fJkVp-XUSrU2T7rEX_PIiwBdHHCnH04DOMF9frC0Tzu32IhxWLkV5319D425qAL5G0_UhYXcW3zCWw4O/s1600/threading.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="multi-threading" border="0" height="170" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNXenJAWCeV5_AV737OnjQp_5VuA9p6X2zjTWEfx1lyUVvWAHOQ4bLheAxlfL9fJkVp-XUSrU2T7rEX_PIiwBdHHCnH04DOMF9frC0Tzu32IhxWLkV5319D425qAL5G0_UhYXcW3zCWw4O/s400/threading.png" title="Multi-threading" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/what-is-difference-between-multi.html</link><author>noreply@blogger.com (sudhakar mishra)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNXenJAWCeV5_AV737OnjQp_5VuA9p6X2zjTWEfx1lyUVvWAHOQ4bLheAxlfL9fJkVp-XUSrU2T7rEX_PIiwBdHHCnH04DOMF9frC0Tzu32IhxWLkV5319D425qAL5G0_UhYXcW3zCWw4O/s72-c/threading.png" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7511270479213795200</guid><pubDate>Mon, 10 Apr 2017 08:45:00 +0000</pubDate><atom:updated>2017-04-21T21:06:44.570-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">memory management</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>How are variables and memory managed in Python?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Memory management in Python&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;When we create an object then the Python Virtual Machine handles the memory needed and where it shall be placed in the memory layout.&lt;/li&gt;
&lt;li&gt;CPython uses a private heap for storing objects.&lt;/li&gt;
&lt;li&gt;Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager.&lt;/li&gt;
&lt;li&gt;The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.&lt;/li&gt;
&lt;li&gt;The algorithm used for garbage collecting is called Reference counting. That is the Python VM keeps an internal journal of how many references refer to an object, and automatically garbage collects it when there are no more references referring to it.&lt;/li&gt;
&lt;li&gt;The management of the Python heap is performed by the interpreter itself and that the user has no control over it.&lt;/li&gt;
&lt;li&gt;At the lowest level Raw Memory Allocator interacting with Memory Manager of OS to ensure that there is enough space / room in private heap.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzKFRAkdkQa23hqjK4OOmwTh_r8RkKXdRQb15B_9tnVP28JwipjzjjE-ZvKgcB8EmuSJmBJbsyoAiyhWRkeMn2kAP_CBgRv4vV92BZB0Tp5G6kiACmHo-TWY9_U-wxjyb2n1CP5X2s7qPJ/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzKFRAkdkQa23hqjK4OOmwTh_r8RkKXdRQb15B_9tnVP28JwipjzjjE-ZvKgcB8EmuSJmBJbsyoAiyhWRkeMn2kAP_CBgRv4vV92BZB0Tp5G6kiACmHo-TWY9_U-wxjyb2n1CP5X2s7qPJ/s400/1.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/how-are-variables-and-memory-managed-in.html</link><author>noreply@blogger.com (sudhakar mishra)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzKFRAkdkQa23hqjK4OOmwTh_r8RkKXdRQb15B_9tnVP28JwipjzjjE-ZvKgcB8EmuSJmBJbsyoAiyhWRkeMn2kAP_CBgRv4vV92BZB0Tp5G6kiACmHo-TWY9_U-wxjyb2n1CP5X2s7qPJ/s72-c/1.jpg" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-4138491083152444635</guid><pubDate>Fri, 07 Apr 2017 08:59:00 +0000</pubDate><atom:updated>2017-04-13T11:59:43.357-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Explain about assert statement?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Assertion&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program.&lt;/li&gt;
&lt;li&gt;We often place assertions at the start of a function to check for valid input, and after a function call to check for valid output.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;The assert Statement&amp;nbsp;&lt;/b&gt;:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;When encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception.&lt;/li&gt;
&lt;li&gt;Syntax :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="color: #444444;"&gt;assert Expression[, Arguments]&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.&lt;/li&gt;
&lt;li&gt;AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.&lt;/li&gt;
&lt;li&gt;Ex :&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; def addOne(n):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;... &amp;nbsp; &amp;nbsp; assert (n &amp;gt;= 0),"n should be greator than or equal to 0."&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;... &amp;nbsp; &amp;nbsp; return n + 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;...&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; addOne(1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;2&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;gt;&amp;gt;&amp;gt; addOne(-1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 2, in addOne&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;AssertionError: n should be greator than or equal to 0.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/explain-about-assert-statement.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-6230335576678030343</guid><pubDate>Fri, 07 Apr 2017 05:23:00 +0000</pubDate><atom:updated>2017-04-21T10:53:10.016-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What is a negative index?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Negative index in python&lt;/b&gt; :&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Python list items can be accessed with positive or negative index.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;If list of size n then&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="text-align: center;"&gt;
0 positive index // first index&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
1 positive index // second index&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
(n-1) // last index&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
-n // first index&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
-(n-1) // second&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
-1 // last index&lt;/div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n.&lt;/li&gt;
&lt;li&gt;Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.&lt;/li&gt;
&lt;li&gt;Ex:&amp;nbsp;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;If we have a list a = [1,2,3,4] then a[-1] will refer to 4 and a[-2] refer to 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/whats-negative-index.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-2570342582508206590</guid><pubDate>Fri, 07 Apr 2017 05:08:00 +0000</pubDate><atom:updated>2017-04-10T01:45:56.099-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What is __repr__ function?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;__repr__ function &lt;/b&gt;:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;The __repr__ function returns a string representation of a Python object that may be evaluated by the Python interpreter to instantiate another instance of the object.&lt;/li&gt;
&lt;li&gt;Called by the repr() built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object.&lt;/li&gt;
&lt;li&gt;repr() tries to convert everything into a string that can be used with eval() to re-generate the original object.&lt;/li&gt;
&lt;li&gt;repr() generates a computer friendly output.&lt;/li&gt;
&lt;li&gt;str() is used by print() function to generate a user-friendly output.&lt;/li&gt;
&lt;li&gt;str()'s output may or may not be used with eval().&lt;/li&gt;
&lt;/ul&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; eval("'Hello'")&lt;br /&gt;'Hello'&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; eval('Hello')&lt;br /&gt;Traceback (most recent call last):&lt;br /&gt;&amp;nbsp; File "&amp;lt;pyshell#8&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; eval('&lt;/span&gt;&lt;span style="color: #444444;"&gt;Hello&lt;/span&gt;&lt;span style="color: #444444;"&gt;')&lt;br /&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;NameError: name '&lt;/span&gt;&lt;span style="color: #444444;"&gt;Hello&lt;/span&gt;&lt;span style="color: #444444;"&gt;' is not defined&lt;/span&gt;&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/what-is-repr-function.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-8970653533528494192</guid><pubDate>Tue, 04 Apr 2017 11:58:00 +0000</pubDate><atom:updated>2019-06-14T08:36:48.233-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">comparison</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What is Python?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Python :&lt;/b&gt;&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Python is an interpreted language. Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.&lt;/li&gt;
&lt;li&gt;Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that. You can do things like x=123 and then x="Hello" without an error.&lt;/li&gt;
&lt;li&gt;Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance.&lt;/li&gt;
&lt;li&gt;In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects.&lt;/li&gt;
&lt;li&gt;Writing Python code is quick but running it is often slower than compiled languages. Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it's really quite quick because a lot of the number crunching it does isn't actually done by Python.&lt;/li&gt;
&lt;li&gt;Python finds use in many spheres - web applications, automation, scientific modeling, big data applications and many more. It's also often used as "glue" code to get other languages and components to play nice.&lt;/li&gt;
&lt;li&gt;Python makes difficult things easy so programmers can focus on overriding algorithms and structures rather than low-level details.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Features / Advantages of Python&lt;/b&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Simple and minimalistic&lt;/li&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;&lt;u&gt;High-level language&lt;/u&gt;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Never need to bother about the low-level details such as managing the memory used by the program.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Interpreted&lt;/u&gt;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;We doesn't need to compile and convert it to binary. It just the program directly from the source code.&lt;/li&gt;
&lt;li&gt;Python coverts source code written by the programmer into intermediate language i.e. byte code, which is again translated it into the native language / machine language that is executed so python is an interpreted language.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Python supports Object Oriented as well as procedure oriented programming.&lt;/li&gt;
&lt;li&gt;Dynamically typed language&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;b&gt;Disadvantages of Python&lt;/b&gt;&amp;nbsp;:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Python is not the best for memory intensive task.&lt;/li&gt;
&lt;li&gt;Python is an interpreted language and is slow compared to C/C++ or java.&lt;/li&gt;
&lt;li&gt;Python is not a great choice for a high graphic 3D game that takes up a lot of CPU.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/what-is-python.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7823346603165558662</guid><pubDate>Tue, 04 Apr 2017 11:31:00 +0000</pubDate><atom:updated>2017-04-04T04:59:04.163-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">call by reference</category><category domain="http://www.blogger.com/atom/ns#">call by value</category><category domain="http://www.blogger.com/atom/ns#">functions</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Describe functions in Python</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;Python Functions :&lt;/b&gt;&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;A function is a block of organized, reusable code that is used to perform a single, related action.&lt;/li&gt;
&lt;li&gt;Python gives us many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Parameter Passing :&lt;/b&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;We can pass arguments to a function either by value or by reference.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;Call by Value&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Sometimes it's also called pass by value.&lt;/li&gt;
&lt;li&gt;In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller's scope will be unchanged when the function returns.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;u&gt;Call by Reference&lt;/u&gt; :&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;It is also known as pass-by-reference.&lt;/li&gt;
&lt;li&gt;In this&amp;nbsp;a function gets an implicit reference to the argument, rather than a copy of its value.&lt;/li&gt;
&lt;li&gt;As a consequence, the function can modify the argument, i.e. the value of the variable in the caller's scope can be changed.&lt;/li&gt;
&lt;li&gt;The function can modify the argument, i.e. the value of the variable in the caller's scope can be changed.&lt;/li&gt;
&lt;li&gt;The disadvantage of it is that variables can be "accidentally" changed in a function call. So special care has to be taken to "protect" the values, which shouldn't be changed.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;b&gt;What is used in Python?&lt;/b&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Python uses a mechanism, which is known as "Call-by-Object", sometimes also called "Call by Object Reference" or "Call by Sharing".&lt;/li&gt;
&lt;li&gt;If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can't be changed within the function, because they can't be changed at all, i.e. they are immutable.&lt;/li&gt;
&lt;li&gt;It's different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller's scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller's scope will remain untouched.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/04/describe-functions-in-python.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7326186347116277542</guid><pubDate>Tue, 04 Apr 2017 05:38:00 +0000</pubDate><atom:updated>2019-06-14T07:55:30.848-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">lambda</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">What are Lambda expressions in python</category><title>What are Lambda expressions in python?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Lambda expressions&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Lambda functions are single-expression functions that are not necessarily bound to a name.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Lambda expression is an anonymous function.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Executing a lambda function evaluates its expression and then automatically returns its result. So there is always an implicit return statement.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;It's a shorthand that allows us to write a method in the same place we are going to use it. &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The difference between lambdas and regular function definitions is that lambda functions are restricted to a single expression. This means a lambda function can't use statements or annotations, not even a return statement.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Example:&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;double = lambda x: x * 2&lt;/span&gt;&lt;/div&gt;
&lt;span id="docs-internal-guid-a4e2d737-f074-f61a-99d6-44ca5b01ee89"&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;                       double(2) # it will return 4&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/lambda-expressions.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-3105411453762126282</guid><pubDate>Tue, 04 Apr 2017 05:10:00 +0000</pubDate><atom:updated>2017-04-03T22:35:13.057-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">decorators</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Explain decorators in python</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Decorator&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Decorator is the name used for a software design pattern.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;It is used to alter functions and methods.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; list-style-type: disc; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-family: arial; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"&gt;Decorators work as wrappers, modifying the behavior of the code before and after a target function execution, without the need to modify the function itself, augmenting the original functionality.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; list-style-type: disc; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-family: arial; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"&gt;Basically, a decorator takes in a function, adds some functionality and returns it.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; list-style-type: disc; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-family: arial; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"&gt;Multiple decorators can be chained in Python.&lt;/span&gt;&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;For ex :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;def addOne(myFun):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;def addOneInside(*args,**kwrgs):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 144pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;return myFun(*args,**kwrgs) + 1&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;return addOneInside&lt;/span&gt;&lt;/div&gt;
&lt;b id="docs-internal-guid-a4e2d737-f075-9968-3267-c8ffe601c65b" style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;@addOne&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;def oldFun( x = 1 ):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;return x&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;print oldFun(564) &lt;/span&gt;&lt;span style="color: #666666; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;# prints 565&lt;/span&gt;&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/decorators.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-5564215146131428379</guid><pubDate>Fri, 31 Mar 2017 06:54:00 +0000</pubDate><atom:updated>2019-06-13T08:03:14.352-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">django</category><category domain="http://www.blogger.com/atom/ns#">django forms</category><category domain="http://www.blogger.com/atom/ns#">document upload</category><category domain="http://www.blogger.com/atom/ns#">file</category><category domain="http://www.blogger.com/atom/ns#">file upload</category><category domain="http://www.blogger.com/atom/ns#">FileField</category><category domain="http://www.blogger.com/atom/ns#">image</category><title>Save a file in model without using Django forms</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div&gt;
If you want to store the document without using Django forms then you can use below code :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
in models.py&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;...&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;class UserProfile(models.Model) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; user = models.OneToOneField(User)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&amp;nbsp; &amp;nbsp; doc = models.FileField(upload_to='uploads/', blank=True)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
in python script&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;from core.models import UserProfile&lt;/span&gt;&lt;/div&gt;
&lt;span style="color: #444444;"&gt;from django.core.files import File&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;user1 = UserProfile(pk = 1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #444444;"&gt;user1.doc.save('resume.pdf', File(open('/tmp/resume.pdf', 'r')))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/save-file-in-django-model-without-using.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-8532253719705218639</guid><pubDate>Tue, 21 Mar 2017 10:27:00 +0000</pubDate><atom:updated>2019-06-14T08:39:44.426-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">inheritance</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Explain Inheritance in python.</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Inheritance&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Classes can inherit from other classes. A class can inherit attributes and behavior methods from another class, called the superclass. &lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;A class which inherits from a superclass is called a subclass, also called heir class or child class. Superclasses are sometimes called ancestors as well.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Syntax :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class DerivedClass(BaseClass) :&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;body_of_derived_class&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;A derived class inherits features from the base class, adding new features to it. This results in re-usability of code.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;If an attribute is not found in the class, the search continues to the base class. This repeats recursively if the base class is derived from other classes.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Ex :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class Polygon:&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def __init__(self, no_of_sides):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self.n = no_of_sides&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self.sides = [0 for i in range(no_of_sides)]&lt;/span&gt;&lt;/div&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def inputSides(self):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]&lt;/span&gt;&lt;/div&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def dispSides(self):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i in range(self.n):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print("Side",i+1,"is",self.sides[i])&lt;/span&gt;&lt;/div&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class Triangle(Polygon):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def __init__(self):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Polygon.__init__(self,3)&lt;/span&gt;&lt;/div&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def findArea(self):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a, b, c = self.sides&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# calculate the semi-perimeter&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s = (a + b + c) / 2&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;area = (s*(s-a)*(s-b)*(s-c)) ** 0.5&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print('The area of the triangle is %0.2f' %area)&lt;/span&gt;&lt;/div&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; t = Triangle()&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; t.inputSides()&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Enter side 1 : 3&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Enter side 2 : 5&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Enter side 3 : 4&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; t.dispSides()&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Side 1 is 3.0&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Side 2 is 5.0&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Side 3 is 4.0&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; t.findArea()&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The area of the triangle is 6.00&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Method Overriding&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;It is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its parent classes.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;In the above example, __init__() method was defined in both classes, Triangle as well Polygon. When this happens, the method in the derived class overrides that in the base class. This is to say, __init__() in Triangle gets preference over the same in Polygon.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Generally when overriding a base method, we tend to extend the definition rather than simply replace it. The same is being done by calling the method in base class from the one in derived class (calling Polygon.__init__() from __init__() in Triangle).&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;A better option would be to use the built-in function super(). So, super().__init(3) is equivalent to Polygon.__init__(self,3) and is preferred.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;isinstance() and issubclass()&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Two built-in functions isinstance() and issubclass() are used to check inheritances.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;isinstance() returns True if the object is an instance of the class or other classes derived from it.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Each and every class in Python inherits from the base class object.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Ex :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; isinstance(t,Triangle)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; isinstance(t,Polygon)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; isinstance(t,int)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; isinstance(t,object)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; issubclass(Polygon,Triangle)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; issubclass(Triangle,Polygon)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&amp;gt;&amp;gt;&amp;gt; issubclass(bool,int)&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 108pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Types :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Single Inheritance&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Multiple Inheritance&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Hierarchical Inheritance&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Multilevel Inheritance&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Hybrid Inheritance (also known as Virtual Inheritance)&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;b style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Multiple Inheritance&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;In multiple inheritance, the features of all the base classes are inherited into the derived class.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Syntax :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class Base1:&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;pass&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class Base2:&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;pass&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;class MultiDerived(Base1, Base2):&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;pass&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;MRO (Method Resolution Order)&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The order in which base classes are searched when looking for a method is often called the Method Resolution Order.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;So, in the above example of MultiDerived class the search order is [MultiDerived, Base1, Base2, object]. This order is also called linearization of MultiDerived class and the set of rules used to find this order is called Method Resolution Order (MRO).&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/inheritance.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-6141495091900178882</guid><pubDate>Tue, 21 Mar 2017 10:25:00 +0000</pubDate><atom:updated>2017-04-21T23:50:21.482-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">generator</category><category domain="http://www.blogger.com/atom/ns#">iterator</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What are Iterators and Generators in python?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Iterators&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;An iterator is an object which allows us to traverse through all the elements of a collection, regardless of its specific implementation.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;An iterator is an object which implements the iterator protocol.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The iterator protocol consists of two methods :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The &lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;__iter__()&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; method, which must return the iterator object&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; The &lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;next()&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; method, which returns the next element from a sequence&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Python has several built-in objects, which implement the iterator protocol. For example lists, tuples, strings, dictionaries or files.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Advantages :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Cleaner code&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Iterators can work with infinite sequences&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: circle; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Iterators save resources&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;By saving system resources means that when working with iterators, we can get the next element in a sequence without keeping the entire dataset in memory.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;b id="docs-internal-guid-a4e2d737-f065-df34-14ed-76e7f755323c" style="font-weight: normal;"&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Generators&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;Generators functions allow us to declare a function that behaves like an iterator.&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;A generator yields one value at a time, this requires less memory.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Generator uses the yield keyword.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;For ex :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="color: #434343; font-family: arial;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;def fib_gen(n):
    a, b = 0, 1
    for i in range(n):
        yield a
        a, b = b, a + b

print list(fib_gen(10))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: arial; font-size: 11pt; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; list-style-type: disc; text-decoration: none; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-weight: 400; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Advantage&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; font-style: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; list-style-type: circle; vertical-align: baseline;"&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"&gt;Memory efficient : Generators are good for calculating large sets of results where you don't know if you are going to need all results, or where you don't want to allocate the memory for all results at the same time.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/iterators-and-generators.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-7056300124730902879</guid><pubDate>Tue, 21 Mar 2017 08:52:00 +0000</pubDate><atom:updated>2017-04-10T23:26:13.579-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">design patterns</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What is a design pattern?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="background-color: white; color: #757575; font-family: dauphinplain; font-size: 24px; line-height: 24px; margin: 0px 0px 10px; padding: 0px;"&gt;
&lt;span id="docs-internal-guid-a4e2d737-adea-35bf-7a09-375ee16b7f73"&gt;&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Design patterns&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="font-weight: 400; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Design patterns represent the best practices&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Design patterns are solutions to general problems that software developers faced during software development.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Gang of Four (GOF)? :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;These authors are collectively known as Gang of Four (GOF)&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; According to these authors design patterns are primarily based on the following principles of object orientated design:&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Program to an interface not an implementation&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Favor object composition over inheritance&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Usage of Design Pattern :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Common platform for developers&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Best Practices&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Types of Design Patterns :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Creational Patterns&lt;/span&gt;&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;It’ not commonly used in Python. Why? Because of the dynamic nature of the language.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;This gives program more flexibility in deciding which objects need to be created for a given use case.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Ex. Singleton&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Singleton&lt;/span&gt;&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;The Singleton pattern is used when we want to guarantee that only one instance of a given class exists during runtime.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;class Logger(object):&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br class="kix-line-break" /&gt;&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;def __new__(cls, *args, **kwargs):&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br class="kix-line-break" /&gt;&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if not hasattr(cls, '_logger'):&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br class="kix-line-break" /&gt;&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cls._logger = super(Logger, cls&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br class="kix-line-break" /&gt;&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;).__new__(cls, *args, **kwargs)&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br class="kix-line-break" /&gt;&lt;/span&gt;&lt;span style="background-color: #fffffc; color: #303030; font-size: 10.5pt; vertical-align: baseline; white-space: pre-wrap;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return cls._logger&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;In this example, Logger is a Singleton.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Structural Patterns&lt;/span&gt;&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;These design patterns concern class and object composition.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Ex. Facade, Adapter,Decorator&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: circle; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Behavioral Patterns&lt;/span&gt;&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;ul style="margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;These design patterns are specifically concerned with communication between objects.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: square; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;According to GOF principles, there are a total of 11 behavioral patterns in Python: Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template, Visitor.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/what-is-design-pattern.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-4136136170352191021</guid><pubDate>Tue, 21 Mar 2017 08:51:00 +0000</pubDate><atom:updated>2019-06-14T08:35:17.988-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">python</category><title>Explain map, filter, reduce and zip functions?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;u&gt;map&lt;/u&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;It is a built-in higher-order function that operates on iterable.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;Functions that can accept other functions as arguments and return function to the caller is known as Higher-order functions.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;It takes a function and an iterable as arguments and returns a new iterable with the function applied to each item of iterable.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;u&gt;filter&lt;/u&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;It filters an iterable by removing items that don’t match a predicate.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;A predicate is a function that returns a Boolean.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;u&gt;reduce&lt;/u&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;It continuously applies the function to sequence and returns a single value.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;u&gt;zip&lt;/u&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;It takes two equal-length collections and merges them together in pairs.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;u&gt;Example&lt;/u&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;nums = [ 11,22,33,44,55 ]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;list1&amp;nbsp; = [ 1,2,3&amp;nbsp; ]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;list2 = [ ‘a’,’b’,’c’ ]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; zip( list1 , list2 ) # [(1, 'a'), (2, 'b'), (3, 'c')]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; res = list( filter ( lambda x : x % 2 == 0 , nums ) )&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; print(res) # [ 22, 44 ]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; print(map(lambda x : x + 5 , nums))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; # prints [16, 27, 38, 49, 60]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;span style="white-space: pre;"&gt;  &lt;/span&gt;&amp;gt;&amp;gt; print(reduce(lambda x,y : x + y , list1)) # prints 6&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;arial&amp;quot;;"&gt;&lt;span style="font-size: 14.6667px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/explain-mapfilter-and-zip-function.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-5837544131240149162</guid><pubDate>Tue, 21 Mar 2017 08:50:00 +0000</pubDate><atom:updated>2017-03-21T03:21:01.912-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">dictionary</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>How to sort Dictionaries over values?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="background-color: white; color: #757575; font-family: dauphinplain; font-size: 24px; line-height: 24px; margin: 0px 0px 10px; padding: 0px;"&gt;
&lt;span id="docs-internal-guid-a4e2d737-adea-35bf-7a09-375ee16b7f73"&gt;&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: arial; font-size: 11pt; white-space: pre-wrap;"&gt;It is not possible to sort a dict, only to get a representation of a dict that is sorted. Dicts are inherently orderless, but other types, such as lists and tuples, are not. So we need a sorted representation, which will be a list—probably a list of tuples.&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Soln 1&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-indent: 36pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;To sort on values&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;import operator&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;sorted_x = sorted(x.items(), key=operator.itemgetter(1))&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;sorted_x will be a list of tuples sorted by the second element in each tuple.dict(sorted_x) == x&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;To sort on keys&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;import operator&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;sorted_x = sorted(x.items(), key=operator.itemgetter(0))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;Soln 2&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}&lt;/span&gt;&lt;/div&gt;
&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: transparent; color: #434343; font-family: &amp;quot;arial&amp;quot;; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;sorted_x = sorted( x.items(), key=lambda a : a[1] )&lt;/span&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/how-to-sort-dictionaries-over-values.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4628146849449914855.post-520104886937536079</guid><pubDate>Tue, 21 Mar 2017 08:49:00 +0000</pubDate><atom:updated>2017-03-21T03:21:01.909-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">dictionary</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>What can be key of a dictionary?</title><description>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="background-color: white; color: #757575; font-family: dauphinplain; font-size: 24px; line-height: 24px; margin: 0px 0px 10px; padding: 0px;"&gt;
&lt;span id="docs-internal-guid-a4e2d737-adea-35bf-7a09-375ee16b7f73"&gt;&lt;div dir="ltr" style="font-weight: 400; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-family: arial; font-size: 11pt; white-space: pre-wrap;"&gt;Key of a dictionary can be :&lt;/span&gt;&lt;/div&gt;
&lt;ul style="font-weight: 400; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;String&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;Numbers&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; margin-left: 36pt; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;tuples&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style="font-weight: 400; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;li dir="ltr" style="background-color: transparent; font-family: Arial; font-size: 11pt; list-style-type: disc; vertical-align: baseline;"&gt;&lt;div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"&gt;
&lt;span style="background-color: transparent; font-size: 11pt; vertical-align: baseline; white-space: pre-wrap;"&gt;List can’t be a key of a dictionary and also tuple of list can’t be same. If we assign a tuple of list as key then it will generate a TypeError: unhashable type: 'list'.&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
</description><link>https://py-basics.blogspot.com/2017/03/what-can-be-key-of-dictionary.html</link><author>noreply@blogger.com (sudhakar mishra)</author><thr:total>0</thr:total></item></channel></rss>