<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1902979661984927761</id><updated>2026-04-13T13:58:54.807+06:00</updated><category term="python"/><category term="python web crawler"/><category term="python web spider"/><category term="Python for Mobile Phone"/><category term="web crawler"/><category term="list"/><category term="web spider"/><category term="CSV write in Python"/><category term="PyS60"/><category term="Python video"/><category term="prime number"/><category term="proxy"/><category term="python freelance work"/><category term="python list"/><category term="recursion"/><category term="regular expression"/><category term="Beautiful Soup"/><category term="Python Course"/><category term="Python Database Programming"/><category term="Python IDE"/><category term="Python Jobs"/><category term="algorithm"/><category term="background process"/><category term="backtracking"/><category term="binary search"/><category term="crawler"/><category term="dictionary"/><category term="json"/><category term="python code"/><category term="python list copy"/><category term="python mysqldb"/><category term="python online course"/><category term="python unicode"/><category term="requests"/><category term="turtle"/><category term="twitter api"/><category term="2d array"/><category term="ASCII Code"/><category term="AttributeError"/><category term="Bangla Python"/><category term="Bengaly Python"/><category term="HTML"/><category term="HTTP POST"/><category term="HTTP Response Header"/><category term="Leap Year"/><category term="OOP in Python"/><category term="Palindrome"/><category term="Plain Text"/><category term="Python 2.5.2"/><category term="Python Free eBook"/><category term="Python Image resize"/><category term="Python List Comprehension"/><category term="Python create thumbnail"/><category term="Python reformat string data"/><category term="Python send email"/><category term="add"/><category term="age"/><category term="all"/><category term="any"/><category term="array"/><category term="base"/><category term="basic http authentication"/><category term="birth date"/><category term="birthday"/><category term="code search"/><category term="concatenate"/><category term="cookie"/><category term="counter"/><category term="csv read"/><category term="date of birth"/><category term="decimal"/><category term="defaultdict"/><category term="delete items"/><category term="delete list"/><category term="design pattern"/><category term="divide and conquer"/><category term="division"/><category term="divmod"/><category term="django"/><category term="dob"/><category term="eclipse"/><category term="email address scraper"/><category term="empty list"/><category term="execute"/><category term="extract domain"/><category term="facebook"/><category term="facebook application"/><category term="fast python I/O"/><category term="finding maximum"/><category term="floating point number"/><category term="ftp upload"/><category term="generator"/><category term="get remote file size (http)"/><category term="getacoder"/><category term="getch"/><category term="global"/><category term="google app engine."/><category term="graphics"/><category term="heap"/><category term="heap sort"/><category term="heapsort"/><category term="image"/><category term="image size"/><category term="infix"/><category term="insertion sort"/><category term="lambda function"/><category term="learn python"/><category term="linux"/><category term="linux command"/><category term="list comprehension"/><category term="list index search"/><category term="login script in python"/><category term="lxml"/><category term="machine learning"/><category term="measure time"/><category term="merge sort"/><category term="merge-sort"/><category term="modulus"/><category term="mongodb"/><category term="nohup"/><category term="nonlocal"/><category term="nosql"/><category term="number"/><category term="online course"/><category term="online judge"/><category term="permutation"/><category term="pil"/><category term="pillow"/><category term="postfix"/><category term="pow"/><category term="power"/><category term="prime factor"/><category term="process"/><category term="programming contest"/><category term="pyhp"/><category term="pytest"/><category term="python book"/><category term="python command line argument"/><category term="python console input"/><category term="python crawler"/><category term="python dictionary"/><category term="python download file"/><category term="python ebook"/><category term="python file"/><category term="python hex to ASCII"/><category term="python http get"/><category term="python http post"/><category term="python hypertext preprocessor"/><category term="python iterator"/><category term="python localtime"/><category term="python programming course"/><category term="python trim"/><category term="python urlparse"/><category term="python version"/><category term="python web frameworks"/><category term="random elements"/><category term="random string"/><category term="random word"/><category term="read firefox cache"/><category term="recommendation engine"/><category term="reduce image size"/><category term="reference"/><category term="remove duplicate"/><category term="rentacoder"/><category term="reusable python script"/><category term="reverse polish notation"/><category term="round"/><category term="scraper"/><category term="set"/><category term="set comprehension"/><category term="settimeout"/><category term="sorting algorithm"/><category term="space character"/><category term="square"/><category term="stand alone python script"/><category term="sub-set"/><category term="subset"/><category term="swap integers"/><category term="swap values python"/><category term="table driven test"/><category term="tdd"/><category term="test driven development"/><category term="time"/><category term="timeit"/><category term="triangle"/><category term="twitter"/><category term="twitter search"/><category term="two dimensional array"/><category term="typeerror"/><category term="unit test"/><category term="unix"/><category term="update"/><category term="url redirect"/><category term="urllib2"/><category term="user agent"/><category term="user-agent"/><category term="whitespace"/><category term="write large file"/><category term="xml"/><category term="xml parsing"/><category term="yield"/><category term="youtube"/><category term="youtube api"/><category term="youtube comments"/><category term="youtube video"/><title type='text'>life is short - you need Python!</title><subtitle type='html'>A blog on Python with tutorials, code, programs, tips and tricks, how-to, book-list, crawler / spider help, data structure and algorithm implementation and many more ...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>161</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-3435822187600450856</id><published>2021-05-01T06:40:00.003+06:00</published><updated>2021-05-01T06:40:29.027+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="counter"/><category scheme="http://www.blogger.com/atom/ns#" term="defaultdict"/><category scheme="http://www.blogger.com/atom/ns#" term="dictionary"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Count frequency of items in a list using Python</title><content type='html'>&lt;p&gt;In this post I am going to show you how we can write a program in Python that can count the frequency of items in a list. First I shall implement it using the dictionary data structure in Python. Then I shall make it shorter (and nicer) using the defaultdict and Counter data structures from the &lt;a href=&quot;https://docs.python.org/3/library/collections.html&quot; target=&quot;_blank&quot;&gt;collections module&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Here is our code that we will use to run and test the program -&lt;/p&gt;
&lt;pre&gt;if __name__ == &quot;__main__&quot;:
    li = [2, 6, 9, 2, 8, 2, 9, 9, 3, 1, 4, 5, 7, 1, 8, 10, 2, 10, 10, 5]
    freq = frequency_counter(li)
    assert freq[1] == 2
    assert freq[2] == 4
    assert freq[3] == 1
    assert freq[11] == 0
&lt;/pre&gt;
&lt;br /&gt;
Now let&#39;s look at the implementation using dictionary. We will iterate over all the items and then check if it exists in the dictionary. If it exists, we shall increase the count. And if it doesn&#39;t, then we shall set the count to 1.
&lt;pre&gt;def frequency_counter(li):
    freq_dt = dict()
    for item in li:
        if item in freq_dt:
            freq_dt[item] += 1
        else:
            freq_dt[item] = 1

    return freq_dt
&lt;/pre&gt;
&lt;br /&gt;
defaultdict is a data sturcture very similar to dictionary (actually it extends the dictionary). The advantage is, you can create the dictionary using a default type, so when an item is not in the dictionary yet, the zero value of the default type would be used. Say 5 doesn&#39;t exist in the dictionary, the dt[5] would return 0. That&#39;s a huge convenience. 
&lt;pre&gt;def frequency_counter(li):
    freq_dt = defaultdict(int)
    for item in li:
        freq_dt[item] += 1

    return freq_dt

&lt;/pre&gt;
&lt;br /&gt;
Now let me introduce Counter, a special purpose dictionary. Instead of writing anything, showing code would be the best way - 
&lt;pre&gt;def frequency_counter(li):
    freq_dt = Counter(li)
    return freq_dt
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
You can find the code &lt;a href=&quot;https://gist.github.com/tamim/c51c29bd2129d17c9c609c2aebf00e41&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/3435822187600450856/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/3435822187600450856' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/3435822187600450856'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/3435822187600450856'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2021/05/count-frequency-of-items-in-list-using.html' title='Count frequency of items in a list using Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-7188873526232326252</id><published>2021-04-27T18:41:00.003+06:00</published><updated>2021-04-28T06:24:07.223+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="prime factor"/><category scheme="http://www.blogger.com/atom/ns#" term="prime number"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="tdd"/><category scheme="http://www.blogger.com/atom/ns#" term="test driven development"/><title type='text'>Python Program to Find Prime Factors of a Number</title><content type='html'>&lt;p&gt;&amp;nbsp;In this post I am going to write a program in python that finds all the prime factors of a number. I am going to start by writing an empty function and a test.&lt;/p&gt;
&lt;p&gt;
  &lt;/p&gt;&lt;pre&gt;  def get_prime_factors(number):
      prime_factors = []
      return prime_factors
      
  if __name__ == &quot;__main__&quot;:
      n = 8
      expected = [2, 2, 2] 
      result = get_prime_factors(n)
      assert expected == result, result    
  &lt;/pre&gt;

Now, if you run the program above, it will give an AssertionError, as we are returning an empty list. Let&#39;s write some code to make our test pass.
&lt;pre&gt;    def get_prime_factors(number):
        prime_factors = []
        
        while number % 2 == 0:
            prime_factors.append(2)
            number = number // 2
            
        return prime_factors
&lt;/pre&gt;
The program will work for multiple of 2&#39;s. Our next task is to find the other prime factors. For example, prime factors of 10 are 2 and 5. We shall add this test case first and then write code to pass this test. If you have studied prime numbers before, then you might remember that if the number itself is not prime, then you will find all the prime factors in the range from 2 to square root of n. So we shall use a loop to try every number upto square root of n to check if that number is a factor of n. And as we already tried to divide the number by 2, we can try only odd numbers.

&lt;pre&gt;    from math import sqrt
    
    def get_prime_factors(number):
        prime_factors = []
        
        while number % 2 == 0:
            prime_factors.append(2)
            number = number // 2
            
        sq_root = int(sqrt(number))
        for m in range(3, sq_root+1, 2):
            if number % m == 0:
                prime_factors.append(m)
                number = number // m
                
        return prime_factors
&lt;/pre&gt;

We made some good progress. However, the get_prime_factors function above won&#39;t pass the test case for 10. Because in the first while loop, number is divisible by 2, so we shall add 2 to the list of prime factors. Then we divide the number by 2, so it becomes 5 (10 // 2). And the value of sq_root is 2, so the value of m in the for loop will be 3 (for m in range(3, 3, 2)). So we didn&#39;t get anything out of that loop. Because here 5 itself is a prime number. So after the for loop, we need to check if number is still greater than 1. And if it is, then we can safely assume that it&#39;s a prime number (and a prime factor of number), thus add to our list of prime factors.

If you spend some time, you will be able to complete the code by yourself. And don&#39;t forget to test the code with different values of n (1, 2, 4, 5, 25, 100, 101). I hope you found it useful. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can find the full code &lt;a href=&quot;https://gist.github.com/tamim/5e1b44062c85445a25d6719d66379016&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/7188873526232326252/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/7188873526232326252' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7188873526232326252'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7188873526232326252'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2021/04/python-program-to-find-prime-factors-of.html' title='Python Program to Find Prime Factors of a Number'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-545741186997823896</id><published>2018-10-11T16:47:00.000+06:00</published><updated>2018-10-11T16:48:38.504+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="division"/><category scheme="http://www.blogger.com/atom/ns#" term="divmod"/><category scheme="http://www.blogger.com/atom/ns#" term="modulus"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>divmod - division and modulus together in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;a href=&quot;https://docs.python.org/3.7/library/functions.html#divmod&quot; target=&quot;_blank&quot;&gt;divmod&lt;/a&gt; is a wonderful, nice, little built-in function in Python that allows you to do division and modulus operation together. Using it, you can make your code slightly beautiful, thus Pythonic! Check the following example -&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; 10 / 3
3.3333333333333335
&amp;gt;&amp;gt;&amp;gt; 10 // 3
3
&amp;gt;&amp;gt;&amp;gt; 10 % 3
1
&amp;gt;&amp;gt;&amp;gt; divmod(10, 3)
(3, 1)&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/545741186997823896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/545741186997823896' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/545741186997823896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/545741186997823896'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2018/10/divmod-division-and-modulus-together-in.html' title='divmod - division and modulus together in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-6236501595498362051</id><published>2018-09-16T20:52:00.002+06:00</published><updated>2018-09-16T20:53:50.585+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="all"/><category scheme="http://www.blogger.com/atom/ns#" term="any"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Python all any built-in function</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I have been using Python for a long time, still learning lot of simple and new things. Recently I came to know about two built-in functions named &lt;a href=&quot;https://docs.python.org/3/library/functions.html#all&quot; target=&quot;_blank&quot;&gt;all&lt;/a&gt; and &lt;a href=&quot;https://docs.python.org/3/library/functions.html#any&quot; target=&quot;_blank&quot;&gt;any&lt;/a&gt;. Today while I was solving a &lt;a href=&quot;https://www.hackerrank.com/challenges/compare-two-linked-lists/problem&quot; target=&quot;_blank&quot;&gt;hackerrank problem&lt;/a&gt;, I used both functions which makes the code a bit nicer, in my opinion.&lt;br /&gt;
&lt;br /&gt;
The functions compares two singly linked lists and if they are equal, returns 1, otherwise returns 0. Here is my code -&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;def compare_lists(llist1, llist2):
    while all([llist1, llist2]):
        if llist1.data != llist2.data:
            return 0
        llist1 = llist1.next
        llist2 = llist2.next
        
    if any([llist1, llist2]):
        return 0
    
    return 1
&lt;/pre&gt;
&lt;br /&gt;
Sometimes, this kind of simple thing brings joy to me. Let me know your thoughts. :)
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/6236501595498362051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/6236501595498362051' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6236501595498362051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6236501595498362051'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2018/09/python-all-any-built-in-function.html' title='Python all any built-in function'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-76864128308844952</id><published>2018-04-10T12:45:00.000+06:00</published><updated>2018-11-06T21:56:23.759+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="2d array"/><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="two dimensional array"/><title type='text'>Create 2D Array using List in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
If for some reason you want to create a two dimensional array in Python and don&#39;t want to use external packages like numpy etc., the most obvious thing is to write a code like this :&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; n = 3
&amp;gt;&amp;gt;&amp;gt; m = 4
&amp;gt;&amp;gt;&amp;gt; ara = [[0] * m] * n
&amp;gt;&amp;gt;&amp;gt; ara
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
&lt;/pre&gt;
&lt;br /&gt;
But there is a problem. Check the code below-&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; ara[0][0] = 1
&amp;gt;&amp;gt;&amp;gt; ara
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
Actually, when you do [0] * m, it returns a reference to a list and when you multiply it with n, the same reference is duplicated. Hence you see the change in a way that you didn&#39;t expect.&lt;br /&gt;
&lt;br /&gt;
The right way to do it is to append [0] * m, n times in the array. You can code it in multiple ways. My preferred way is:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; ara = [[0] * m for _ in range(n)]
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/76864128308844952/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/76864128308844952' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/76864128308844952'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/76864128308844952'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2018/04/create-2d-array-using-list-in-python.html' title='Create 2D Array using List in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-4268577096629532773</id><published>2017-10-29T10:33:00.004+06:00</published><updated>2017-10-29T10:35:56.519+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="pytest"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="table driven test"/><category scheme="http://www.blogger.com/atom/ns#" term="unit test"/><title type='text'>Table Driven Unit Test in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Now days, table driven tests are pretty much industry standard. In my workplace, we use table driven tests when we write unit tests (in golang though). Here I shall share a simple code example using &lt;a href=&quot;https://docs.pytest.org/en/latest/&quot; target=&quot;_blank&quot;&gt;pytest&lt;/a&gt; that shows how to write table driven tests in Python.&lt;br /&gt;
&lt;br /&gt;
In table driven test, what you need to do is, to gather all the tests cases together in a single table. We can use dictionary for each test case and a list to store all the test cases. Instead of discussing it further, let me show you an example :&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;def average(L):
    if not L:
        return None
    return sum(L)/len(L)

def test_average():
    test_cases = [
        {
            &quot;name&quot;: &quot;simple case 1&quot;,
            &quot;input&quot;: [1, 2, 3],
            &quot;expected&quot;: 2.0
        },
        {
            &quot;name&quot;: &quot;simple case 2&quot;,
            &quot;input&quot;: [1, 2, 3, 4],
            &quot;expected&quot;: 2.5
        },
        {
            &quot;name&quot;: &quot;list with one item&quot;,
            &quot;input&quot;: [100],
            &quot;expected&quot;: 100.0
        },
        {
            &quot;name&quot;: &quot;empty list&quot;,
            &quot;input&quot;: [],
            &quot;expected&quot;: None
        }
    ]

    for test_case in test_cases:
        assert test_case[&quot;expected&quot;] == average(test_case[&quot;input&quot;]), test_case[&quot;name&quot;]


&lt;/pre&gt;
Now if you run the test, you will get the following output :&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;$ pytest average.py&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;b&gt;==================== test session starts =====================&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;platform darwin -- Python 3.5.1, pytest-3.0.3, py-1.4.31, pluggy-0.4.0&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;rootdir: /Users/tamimshahriar/..., inifile:&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;b&gt;collected 1 items&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;average.py .&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #f9fbfb; background-color: #000000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ffffff; background-color: #000000}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #f9fbfb; background-color: #000000; min-height: 15.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #34bd26; background-color: #000000}
span.s1 {font-variant-ligatures: no-common-ligatures}
&lt;/style&gt;










&lt;br /&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;b&gt;================== 1 passed in 0.01 seconds ==================&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
Hope you will find this post useful and use table driven tests. :)&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/4268577096629532773/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/4268577096629532773' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4268577096629532773'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4268577096629532773'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2017/10/table-driven-unit-test-in-python.html' title='Table Driven Unit Test in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-8735990787076601507</id><published>2017-06-13T00:20:00.004+06:00</published><updated>2017-06-13T07:32:53.900+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="global"/><category scheme="http://www.blogger.com/atom/ns#" term="nonlocal"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>global and nonlocal variable in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Most of us are already familiar with global variables in Python. If we declare those variables in a module, the functions inside that module (can read python file or .py file) can access the variable. For example, check the code below :&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;It will print :&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;inside myfnc 5&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;inside myfnc2 5&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If you change your code like this :&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x = 10&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;x = &quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You will get an error :&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; File &quot;program.py&quot;, line 6, in myfnc2&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print(&quot;inside myfnc2&quot;, x)&lt;br /&gt;
UnboundLocalError: local variable &#39;x&#39; referenced before assignment&lt;br /&gt;
&lt;br /&gt;
The moment you wrote x = 10, Python assume that x is a local variable, and inside the print function, it is giving this error. Because local variables are determined at compile time (from official doc : &quot;local variables are already determined statically&quot;). &amp;nbsp;You can get rid of the error, if you declare x as global.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;global x&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x = 10&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;x = &quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now you can run the program again. It won&#39;t throw any error. What if we code like this now?&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;y = 10&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;global x&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x, y)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x = 10&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;x = &quot;, x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
If you run the program, you will see desired output. But now, if you want to write to y inside myfnc2() (for example, assign something like y = 1), you can&#39;t use global y, as y is not a global variable. You can try this following code that fails successfully :&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;y = 10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;global x&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;global y&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x, y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x = 10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;x = &quot;, x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;y = 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;y = &quot;, y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
You will get this error :&amp;nbsp;NameError: name &#39;y&#39; is not defined&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We need to understand that, y is not a global variable. Here &lt;b&gt;nonlocal&lt;/b&gt; can help! Instead of global y, just write nonlocal. It will make y available and writable inside myfnc2().&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x = 5&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;def myfnc():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;print(&quot;inside myfnc&quot;, x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;y = 10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;def myfnc2():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;global x&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;nonlocal y&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;inside myfnc2&quot;, x, y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x = 10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;x = &quot;, x)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;y = 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;print(&quot;y = &quot;, y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;myfnc2()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;myfnc()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This is something I learnt today. :)&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/8735990787076601507/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/8735990787076601507' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/8735990787076601507'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/8735990787076601507'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2017/06/global-and-nonlocal-variable-in-python.html' title='global and nonlocal variable in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-7835813792474773679</id><published>2017-01-08T09:14:00.002+06:00</published><updated>2017-01-08T09:14:58.514+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="measure time"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="python code"/><category scheme="http://www.blogger.com/atom/ns#" term="time"/><category scheme="http://www.blogger.com/atom/ns#" term="timeit"/><title type='text'>Python Code Measure Time</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
How to measure time of a Python code snippet?&lt;br /&gt;
&lt;br /&gt;
We can do it using the &lt;a href=&quot;https://docs.python.org/2/library/timeit.html&quot; target=&quot;_blank&quot;&gt;timeit module&lt;/a&gt;. It has a function called timeit that runs a particular code snippet for N times (one million by default) and gives you the runtime. Today I just used it for a small purpose and thought of writing a note here.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; from timeit import timeit
&amp;gt;&amp;gt;&amp;gt; timeit(&#39;&quot;c&quot; &amp;gt;= &quot;a&quot; and &quot;c&quot; &amp;lt;= &quot;z&quot;&#39;)
0.06282545300200582
&amp;gt;&amp;gt;&amp;gt; timeit(&#39;&quot;c&quot;.isalpha()&#39;)
0.06563570606522262
&lt;/pre&gt;
&lt;br /&gt;
Here I compared the performance between two snippets : one uses isalpha() to determine if a (lowercase) character is an alphabet or not, another just compares using operators. There is no noticeable performance difference. Please go through the module doc to get some more ideas of how to use the &lt;a href=&quot;https://docs.python.org/2/library/timeit.html&quot; target=&quot;_blank&quot;&gt;timeit module&lt;/a&gt;, if will often come handy when you quickly want to measure running time of different Python code.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/7835813792474773679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/7835813792474773679' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7835813792474773679'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7835813792474773679'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2017/01/python-code-measure-time.html' title='Python Code Measure Time'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-1364270229440138535</id><published>2016-02-03T23:19:00.000+06:00</published><updated>2016-02-03T23:19:12.548+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="python online course"/><category scheme="http://www.blogger.com/atom/ns#" term="Python video"/><title type='text'>Code School Python Courses</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As Python is getting more and more popular and becoming the de facto standard for starting to learn programming, we can see increasing amount of online content to learn Python. Last week got an email form Code School team to check out their couple of new Python courses. I spent around 30 minutes watching the videos, checkout out the exercises. The exercises are good, and can be done online. They also prepared the videos with care. But one thing that bothers me, the lecture is kind of robotic. I didn&#39;t feel anything special, in fact didn&#39;t feel the connection to the teacher that I used to feel in other courses in Coursera and Udacity. Anyway, Python beginners can check those courses:&lt;br /&gt;
1. &lt;a href=&quot;https://www.codeschool.com/courses/try-python&quot; target=&quot;_blank&quot;&gt;Try Python&lt;/a&gt;&lt;br /&gt;
2. &lt;a href=&quot;https://www.codeschool.com/courses/flying-through-python&quot; target=&quot;_blank&quot;&gt;Flying through Python&lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/1364270229440138535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/1364270229440138535' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1364270229440138535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1364270229440138535'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2016/02/code-school-python-courses.html' title='Code School Python Courses'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-4985397573852741102</id><published>2015-02-25T16:43:00.002+06:00</published><updated>2015-03-02T10:01:37.340+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="image"/><category scheme="http://www.blogger.com/atom/ns#" term="image size"/><category scheme="http://www.blogger.com/atom/ns#" term="pil"/><category scheme="http://www.blogger.com/atom/ns#" term="pillow"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>How to get image size from url</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today I needed to get image size (width and height) from image urls. I am sharing my python code here.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
import requests
from PIL import Image
from io import BytesIO


def get_image_size(url):
    data = requests.get(url).content
    im = Image.open(BytesIO(data))    
    return im.size


if __name__ == &quot;__main__&quot;:
    url = &quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQiI_ipTLsmwY5rfnlGkMdp0Bq2bwLmlhtXRuQMJjCYxojb1bxaZ-LRGdiMlP0XiBWlgG0HCF3GbHQAlrMVi16isP7lwWhBySdTtipnnuWk6UrYiztsKtwStN_0-pyR_mlnMbt9_5rCx14/s1600/sbn_pic_2.jpg&quot;
    width, height = get_image_size(url)
    print width, height
&lt;/pre&gt;
&lt;br /&gt;
I used &lt;a href=&quot;http://pillow.readthedocs.org/&quot; target=&quot;_blank&quot;&gt;pillow&lt;/a&gt; - which is a PIL fork.&lt;br /&gt;
&lt;br /&gt;
Feel free to comment if you have any suggestion to improve my code.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/4985397573852741102/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/4985397573852741102' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4985397573852741102'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4985397573852741102'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2015/02/how-to-get-image-size-from-url.html' title='How to get image size from url'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-793263048654126400</id><published>2015-01-13T11:37:00.000+06:00</published><updated>2015-01-13T11:38:36.340+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="background process"/><category scheme="http://www.blogger.com/atom/ns#" term="crawler"/><category scheme="http://www.blogger.com/atom/ns#" term="process"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>crawler controller</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I work on a project where I have written 20+ crawlers and the crawlers are running 24/7 (with good amount of sleep). Sometimes, I need to update / restart the server. Then I have to start all the crawlers again. So, I have written a script that will control all the crawlers. It will first check if the crawler is already running, and if not, then it will start the crawler and the crawler will run in the background. I also saved the pid of all the crawlers in a text file so that I can kill a particular crawler immediately when needed.&lt;/div&gt;
&lt;br /&gt;
Here is my code :&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;import shlex
from subprocess import Popen, PIPE

site_dt = {&#39;Site1 Name&#39; : [&#39;site1_crawler.py&#39;, &#39;site1_crawler.out&#39;],&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&#39;Site2 Name&#39; : [&#39;site2_crawler.py&#39;, &#39;site2_crawler.out&#39;]}

location = &quot;/home/crawler/&quot;

pidfp = open(&#39;pid.txt&#39;, &#39;w&#39;)


def is_running(pname):
    p1 = Popen([&quot;ps&quot;, &quot;ax&quot;], stdout=PIPE)
    p2 = Popen([&quot;grep&quot;, pname], stdin=p1.stdout, stdout=PIPE)
    p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
    output = p2.communicate()[0]
    if output.find(&#39;/home/crawler/&#39;+pname) &amp;gt; -1:
        return True
    return False


def main():
    for item in site_dt.keys():
        print item
        if is_running(site_dt[item][0]) is True:
            print site_dt[item][0], &quot;already running&quot;
            continue
        cmd = &quot;python &quot; + location + site_dt[item][0] + &quot; -l info&quot;   
        outfile = &quot;log/&quot; + site_dt[item][1] 
        fp = open(outfile, &#39;w&#39;)
        
        pid = Popen(shlex.split(cmd), stdout=fp).pid
        
        print pid
        print
        pidfp.write(item + &quot;: &quot; + pid + &quot;\n&quot;)
        
    pidfp.close()
    
    
if __name__ == &quot;__main__&quot;:
    main()
&lt;/pre&gt;
&lt;br /&gt;
If you feel that there is scope for improvement, please comment. &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/793263048654126400/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/793263048654126400' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/793263048654126400'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/793263048654126400'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2015/01/crawler-controller.html' title='crawler controller'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-4103936000747286596</id><published>2015-01-09T16:01:00.001+06:00</published><updated>2015-01-09T16:01:41.289+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="divide and conquer"/><category scheme="http://www.blogger.com/atom/ns#" term="finding maximum"/><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>finding maximum element of a list - divide and conquer approach</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Here I share my Python implementation of divide and conquer approach of finding maximum element from a list :&lt;br /&gt;
&lt;pre&gt;&quot;&quot;&quot;
Divide and conquer approach to find the maximum from a list
&quot;&quot;&quot;

def find_max(li, left, right):
    if left == right:
        return li[left]
    mid = (left + right) / 2
    max1 = find_max(li, left, mid)
    max2 = find_max(li, mid+1, right)
    return max1 if max1 &amp;gt; max2 else max2
    
    
def main():
    li = [1, 5, 2, 9, 3, 7, 5, 2, 10]
    print &quot;Maximum element of the list is&quot;, find_max(li, 0, len(li)-1)
    
    
if __name__ == &quot;__main__&quot;:
    main()
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/4103936000747286596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/4103936000747286596' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4103936000747286596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/4103936000747286596'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2015/01/finding-maximum-element-of-list-divide.html' title='finding maximum element of a list - divide and conquer approach'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-1088124979312636468</id><published>2014-12-06T23:39:00.002+06:00</published><updated>2015-01-22T16:32:20.773+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="pow"/><category scheme="http://www.blogger.com/atom/ns#" term="power"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>pow() function returns float or integer?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today, someone posted this code and asked why x is float ?
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; from math import pow
&amp;gt;&amp;gt;&amp;gt; x = pow(5, 2)
&amp;gt;&amp;gt;&amp;gt; x
25.0
&lt;/pre&gt;
It&#39;s float because the pow function of math package returns float. But there is a &lt;a href=&quot;https://docs.python.org/2/library/functions.html#pow&quot;&gt;built-in pow function&lt;/a&gt; in Python which will return integer in this case. You need to restart the python interpreter before you run the following code :
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; a = 5
&amp;gt;&amp;gt;&amp;gt; x = pow(a, 2)
&amp;gt;&amp;gt;&amp;gt; x
25
&amp;gt;&amp;gt;&amp;gt; type(x)
&amp;lt;type &#39;int&#39;&amp;gt;
&lt;/pre&gt;
So, don&#39;t get confused. Happy coding! :)
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/1088124979312636468/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/1088124979312636468' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1088124979312636468'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1088124979312636468'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/12/pow-function-returns-float-or-integer.html' title='pow() function returns float or integer?'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-6557981079193575346</id><published>2014-11-11T19:59:00.000+06:00</published><updated>2014-11-11T19:59:29.126+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AttributeError"/><category scheme="http://www.blogger.com/atom/ns#" term="json"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>AttributeError: &#39;module&#39; object has no attribute</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Are you getting this type of error in your Python program?&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
AttributeError: &#39;module&#39; object has no attribute &#39;whatever&#39; ?&amp;nbsp;&lt;/blockquote&gt;
Well, then you might need to change the name of your python file.&lt;br /&gt;
&lt;br /&gt;
For example, save the following code in a file named json.py :&lt;br /&gt;
&lt;pre&gt; 
import json&lt;/pre&gt;
&lt;pre&gt;
print json.dumps([&#39;foo&#39;, {&#39;bar&#39;: (&#39;baz&#39;, None, 1.0, 2)}])&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Then try to run it, and you will get this error :&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;$ python json.py &lt;/pre&gt;
&lt;pre&gt;Traceback (most recent call last):
&amp;nbsp; File &quot;json.py&quot;, line 1, in &lt;module&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; import json
&amp;nbsp; File &quot;/home/work/practice/json.py&quot;, line 3, in &lt;module&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; print json.dumps([&#39;foo&#39;, {&#39;bar&#39;: (&#39;baz&#39;, None, 1.0, 2)}])
AttributeError: &#39;module&#39; object has no attribute &#39;dumps&#39;&lt;/module&gt;&lt;/module&gt;&lt;/pre&gt;
&lt;br /&gt;
But of course the &lt;a href=&quot;https://docs.python.org/2/library/json.html&quot; target=&quot;_blank&quot;&gt;json module&lt;/a&gt; has &#39;dumps&#39; attribute! What went wrong?&lt;br /&gt;
&lt;br /&gt;
When you wrote import json, it tries to find the file json.py in the current directory first and it got the file that you wrote (you saved the file as json.py) and it surely doesn&#39;t have any attribute (variable or method) named dumps. All you need to do is to change your filename.&lt;br /&gt;
&lt;br /&gt;
So, the lesson is, don&#39;t name your python program such a way that it matches a module name used in your program. Happy coding. :)&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/6557981079193575346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/6557981079193575346' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6557981079193575346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6557981079193575346'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/11/attributeerror-module-object-has-no.html' title='AttributeError: &#39;module&#39; object has no attribute'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-7065793412170472968</id><published>2014-10-29T18:48:00.001+06:00</published><updated>2014-10-29T18:48:14.895+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="delete items"/><category scheme="http://www.blogger.com/atom/ns#" term="delete list"/><category scheme="http://www.blogger.com/atom/ns#" term="empty list"/><title type='text'>How to empty a list</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Say you have a list in your program and you are going to reuse the name of the list, or delete all items from the list (empty the list). One option is to delete the list completely along with the name : &lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; del li&lt;/pre&gt;
Another option is to delete all items inside the list:&amp;nbsp; &lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; del li[:]&lt;/pre&gt;
Many of you can think of another alternative : &lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; li = []&lt;/pre&gt;
Though li = [], apparently it makes li empty, actually it is creating a new list object and the previous object will be in memory.&lt;br /&gt;
&lt;br /&gt;
Now, if you take some time to play in your Python interpreter, you will have a better idea what happens :&lt;br /&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; li = [1, 2, 3, 4, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li2 = li&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li2&lt;br /&gt;[1, 2, 3, 4, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; del li[:]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li&lt;br /&gt;[]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li2&lt;br /&gt;[]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li1 = [1, 3, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li3 = li1&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li3&lt;br /&gt;[1, 3, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li1&lt;br /&gt;[1, 3, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li1 = []&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li1&lt;br /&gt;[]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li3&lt;br /&gt;[1, 3, 5]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li4 = [1, 2]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li5 = li4&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li5&lt;br /&gt;[1, 2]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; del li4&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li4&lt;br /&gt;Traceback (most recent call last):&lt;br /&gt;&amp;nbsp; File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;&lt;br /&gt;NameError: name &#39;li4&#39; is not defined&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li5&lt;br /&gt;[1, 2]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/module&gt;&lt;/stdin&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/7065793412170472968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/7065793412170472968' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7065793412170472968'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7065793412170472968'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/10/how-to-empty-list.html' title='How to empty a list'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-1342144270341791913</id><published>2014-08-28T07:04:00.002+06:00</published><updated>2014-08-28T07:04:41.582+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="list index search"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><title type='text'>fastest list index searching</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Someone was looking for fastest list index searching (in Python) in stackoverflow. There are only four elements in the list, still it was as issue for him, as it was used in an inner-loop and the profiler identified that, this part was executing the most. He tried several approaches.&lt;br /&gt;
&lt;br /&gt;
1. This is the most obvious one:&lt;br /&gt;
&lt;pre&gt;if value in mylist:
    return mylist.index(value)
&lt;/pre&gt;
Here the problem is, in the condition used in if, it takes O(n) time to check if the value is in the list, and the return statement - mylist.index(value), also takes O(n) time.
&lt;br /&gt;
&lt;br /&gt;
2. The second approach - I call it smart approach :&lt;br /&gt;
&lt;pre&gt;try:
    return mylist.index(value)
except ValueError:
    return None
&lt;/pre&gt;
This code gets rid of the condition check. But the try-except thing takes some time for the Python interpreter. I like this approach. In fact, if the list was large, I would prefer this.
&lt;br /&gt;
&lt;br /&gt;
3. The third approach - I guess it&#39;s more Pythonic? &lt;br /&gt;
&lt;pre&gt;for i, x in enumerate(mylist):
    if x == value:
         return i
return None
&lt;/pre&gt;
&lt;br /&gt;
All the above solutions are OK. But he was looking for the fastest way. So, I gave the solution :
&lt;br /&gt;
&lt;pre&gt; if value == mylist[0]:
   return 0
 elif value == mylist[1]:
   return 1
 elif value == mylist[2]:
   return 2
 elif value == mylist [3]:
   return 3
&lt;/pre&gt;
Yes, it is the fastest among the discussed options. And he happily accepted the solution. :)&lt;br /&gt;
&lt;br /&gt;
Here you can find the post : &lt;a href=&quot;http://stackoverflow.com/questions/25476179/fastest-list-index-searching/&quot;&gt;stackoverflow.com/questions/25476179/fastest-list-index-searching/&lt;/a&gt; &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/1342144270341791913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/1342144270341791913' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1342144270341791913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1342144270341791913'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/08/fastest-list-index-searching.html' title='fastest list index searching'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-1488061439161415673</id><published>2014-08-20T20:07:00.001+06:00</published><updated>2014-08-20T20:07:47.823+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bangla Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Bengaly Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python Course"/><category scheme="http://www.blogger.com/atom/ns#" term="Python video"/><title type='text'>Python video tutorials in Bangla</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Finally, I have completed my video tutorial series on Python. The lectures are given in my native language - Bangla (Bengali). &lt;a href=&quot;http://www.shikkhok.com/%E0%A6%95%E0%A7%8B%E0%A6%B0%E0%A7%8D%E0%A6%B8-%E0%A6%A4%E0%A6%BE%E0%A6%B2%E0%A6%BF%E0%A6%95%E0%A6%BE/python101/&quot; target=&quot;_blank&quot;&gt;Click here&lt;/a&gt; to check the videos.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMAgXD1i2SXPvZ-iKXI4AJqO039lMLSrH878HsxbtPTibnkaiUo6bK7cUKX6dTgEmnjpLbuhEqgAPsODDEk0acap45NX5VrxYJzceDr0dbJnBklP94GoQv8b7kU4eHNqyeJ1DQigtKHoB8/s1600/pypypy.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMAgXD1i2SXPvZ-iKXI4AJqO039lMLSrH878HsxbtPTibnkaiUo6bK7cUKX6dTgEmnjpLbuhEqgAPsODDEk0acap45NX5VrxYJzceDr0dbJnBklP94GoQv8b7kU4eHNqyeJ1DQigtKHoB8/s1600/pypypy.png&quot; height=&quot;217&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the course I assume that the student has prior programming experience in C / Java. So this is not suitable for programming beginners, rather it&#39;s suitable for Python beginners.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
There are six units in the series and each unit has several videos (8 to 12). The topics covered are : Introduction to Python, Variables, Conditional Logic, Loop, Data Structures (List, Dict, Set, Tuple), Input/Output, File, Exception Handling, Modules and Packages, Object Oriented Programming, Iterators, Generators etc. I couldn&#39;t make it a complete course because I didn&#39;t have time to prepare assignments, quizzes, questions. Hope, I can offer a full course online via &lt;a class=&quot;g-profile&quot; href=&quot;https://plus.google.com/101180708911073997267&quot; target=&quot;_blank&quot;&gt;+Dimik Computing School&lt;/a&gt; in future on Python.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/1488061439161415673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/1488061439161415673' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1488061439161415673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1488061439161415673'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/08/python-video-tutorials-in-bangla.html' title='Python video tutorials in Bangla'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMAgXD1i2SXPvZ-iKXI4AJqO039lMLSrH878HsxbtPTibnkaiUo6bK7cUKX6dTgEmnjpLbuhEqgAPsODDEk0acap45NX5VrxYJzceDr0dbJnBklP94GoQv8b7kU4eHNqyeJ1DQigtKHoB8/s72-c/pypypy.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-2482245364654843595</id><published>2014-08-09T23:16:00.000+06:00</published><updated>2014-08-10T14:49:17.605+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="reference"/><title type='text'>Python list of lists</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I just stumbled upon a post/problem in stackoverflow. Here I discuss it and the solution.&lt;br /&gt;
&lt;br /&gt;
First check the following code segment :
&lt;br /&gt;
&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt;visited = [[False]*4]*4
&amp;gt;&amp;gt;&amp;gt;visited[0][1] = True
&amp;gt;&amp;gt;&amp;gt;print visited
&amp;gt;&amp;gt;&amp;gt; [[False, True, False, False], [False, True, False, False], [False, True, False, False], [False, True, False, False]]
&lt;/pre&gt;
&lt;br /&gt;
Do you see the problem, second element of all the list changed to True! So it means [[False]*4]*4 creates a list that contains reference to the same list [False, False, False, False] &lt;br /&gt;
&lt;br /&gt;
You can avoid this by following this code:
&lt;br /&gt;
&lt;pre&gt;    &amp;gt;&amp;gt;&amp;gt; n = 4
    &amp;gt;&amp;gt;&amp;gt; m = 4
    &amp;gt;&amp;gt;&amp;gt; visited = []
    &amp;gt;&amp;gt;&amp;gt; for x in xrange(n):
    ...      visited.append([False] * m)
    ... 
    &amp;gt;&amp;gt;&amp;gt; visited
    [[False, False, False, False], [False, False, False, False], [False, False, False, False], [False, False, False, False]]
    &amp;gt;&amp;gt;&amp;gt; visited[0][1] = True
    &amp;gt;&amp;gt;&amp;gt; visited
    [[False, True, False, False], [False, False, False, False], [False, False, False, False], [False, False, False, False]]
    &amp;gt;&amp;gt;&amp;gt; 
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/2482245364654843595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/2482245364654843595' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/2482245364654843595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/2482245364654843595'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/08/python-list-of-lists.html' title='Python list of lists'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-5235609869627027723</id><published>2014-07-27T21:33:00.005+06:00</published><updated>2014-07-27T21:51:47.945+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="triangle"/><category scheme="http://www.blogger.com/atom/ns#" term="turtle"/><title type='text'>turtle triangle fun</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Just Wrote this piece of code. First just copy-paste the code, run and have fun. Then try to write it yourself and have more fun. :)&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;import turtle

def draw_triangle(side_length, depth):
    if depth == 0:
        return
    
    counter = 0
    while counter &amp;lt; 3:
        counter += 1
        brad.forward(side_length/2)
        if depth &amp;gt; 1:
            brad.left(120)
        draw_triangle(side_length/2, depth-1)
        brad.forward(side_length/2)
        
        brad.right(120)
        
    brad.left(240)
    
    

if __name__ == &quot;__main__&quot;:
    window = turtle.Screen()
    window.bgcolor(&quot;white&quot;)
    
    brad = turtle.Turtle()
    brad.shape(&quot;turtle&quot;)
    brad.color(&quot;green&quot;, &quot;green&quot;)
    brad.speed(5)
    
    brad.begin_fill()
    brad.right(120)
    brad.forward(128)
    brad.left(120)
    brad.forward(256)
    brad.left(120)
    brad.forward(256)
    brad.left(120)
    brad.forward(128)
    brad.left(120)
    brad.end_fill()
    brad.color(&quot;green&quot;, &quot;white&quot;)
    brad.begin_fill()
    draw_triangle(128, 3)
    brad.end_fill()
    
    window.exitonclick()
&lt;/pre&gt;
&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvmWckVjujHIUsqhbVoMpX_JVKVUTZwyApG2PFbFYlNxTYRi6VDeU2G_cCs6-jaE-oiVLrTCZqVJKjP8O8yXguG9dxPKSO7iKEHkNvmXOwSkpXiVSQKDNDUYVYGk0TlePV2WCgZbVyGTdy/s1600/triangle-fun.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvmWckVjujHIUsqhbVoMpX_JVKVUTZwyApG2PFbFYlNxTYRi6VDeU2G_cCs6-jaE-oiVLrTCZqVJKjP8O8yXguG9dxPKSO7iKEHkNvmXOwSkpXiVSQKDNDUYVYGk0TlePV2WCgZbVyGTdy/s1600/triangle-fun.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/5235609869627027723/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/5235609869627027723' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/5235609869627027723'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/5235609869627027723'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/07/turtle-triangle-fun.html' title='turtle triangle fun'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvmWckVjujHIUsqhbVoMpX_JVKVUTZwyApG2PFbFYlNxTYRi6VDeU2G_cCs6-jaE-oiVLrTCZqVJKjP8O8yXguG9dxPKSO7iKEHkNvmXOwSkpXiVSQKDNDUYVYGk0TlePV2WCgZbVyGTdy/s72-c/triangle-fun.png" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-7117939426329791305</id><published>2014-07-18T20:52:00.000+06:00</published><updated>2014-07-18T20:52:02.609+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="graphics"/><category scheme="http://www.blogger.com/atom/ns#" term="square"/><category scheme="http://www.blogger.com/atom/ns#" term="turtle"/><title type='text'>draw circle using squares</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I am checking out a Python course at &lt;a href=&quot;https://www.udacity.com/course/ud036&quot; target=&quot;_blank&quot;&gt;udacity&lt;/a&gt; and found an interesting Python module named &lt;a href=&quot;https://docs.python.org/2/library/turtle.html&quot; target=&quot;_blank&quot;&gt;Turtle&lt;/a&gt;. Now let me share with you a simple code that I just have written.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;pre&gt;import turtle

def draw_square():
    window = turtle.Screen()
    window.bgcolor(&quot;red&quot;)
    
    brad = turtle.Turtle()
    brad.shape(&quot;turtle&quot;)
    brad.color(&quot;yellow&quot;)
    brad.speed(5)
    counter = 0
    while counter &amp;lt; 36:
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(90)
        brad.forward(100)
        brad.right(100)
        counter += 1

    window.exitonclick()

if __name__ == &quot;__main__&quot;:
    draw_square()
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Just save the code in a file and run it, and you will see something like this:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOaLj1RGCts4MNgJZJgdbFijO9P1vf6KMXwibymKytrOAuTyt6zJk_1isQ98S_vnryvAIutj3aUL3iEvnXMSfg8BeGBOkJLT1ByuzD7DC2d317fUi3y3ojr0BgWmZJ1oXrK4gkbvBSaRS2/s1600/turtle1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOaLj1RGCts4MNgJZJgdbFijO9P1vf6KMXwibymKytrOAuTyt6zJk_1isQ98S_vnryvAIutj3aUL3iEvnXMSfg8BeGBOkJLT1ByuzD7DC2d317fUi3y3ojr0BgWmZJ1oXrK4gkbvBSaRS2/s320/turtle1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now a simple question for you. How did I come up with the number 36 in the while loop? (while counter &amp;lt; 36).
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/7117939426329791305/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/7117939426329791305' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7117939426329791305'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/7117939426329791305'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2014/07/draw-circle-using-squares.html' title='draw circle using squares'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOaLj1RGCts4MNgJZJgdbFijO9P1vf6KMXwibymKytrOAuTyt6zJk_1isQ98S_vnryvAIutj3aUL3iEvnXMSfg8BeGBOkJLT1ByuzD7DC2d317fUi3y3ojr0BgWmZJ1oXrK4gkbvBSaRS2/s72-c/turtle1.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-588193314781348781</id><published>2013-11-01T18:14:00.003+06:00</published><updated>2013-11-01T18:14:55.368+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="heap"/><category scheme="http://www.blogger.com/atom/ns#" term="heap sort"/><category scheme="http://www.blogger.com/atom/ns#" term="heapsort"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="sorting algorithm"/><title type='text'>Heap Sort Implementation in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Here I share my Python implementation of &lt;a href=&quot;http://en.wikipedia.org/wiki/Heapsort&quot;&gt;heap sort algorithm&lt;/a&gt;. Heap sort is O(n log n) sorting algorithm though quicksort is used more frequently. You should learn heap sort to implement priority queue or at least learn the internals of priority queue. :)&lt;br /&gt;&lt;br /&gt; 

In the Python code, I directly followed the heap sort algorithm presented in Introduction to Algorithms by CLRS.
&lt;pre&gt;
# heapsort

def max_heapify(li, i, heap_size):
  l = i * 2 #left(i)
  r = l + 1 #right(i)
  if l &lt;= heap_size and li[l] &gt; li[i]:
    largest = l
  else:
    largest = i
  if r &lt;= heap_size and li[r] &gt; li[largest]:
    largest = r
  if i != largest:
    li[i], li[largest] = li[largest], li[i]
    max_heapify(li, largest, heap_size)

def build_max_heap(li, heap_size):
  for i in range(heap_size/2 - 1, 0, -1):
    max_heapify(li, i, heap_size) 

def heap_sort(li, heap_size):
  build_max_heap(li, heap_size)
  for i in range(len(li[1:]), 1, -1):
    li[1], li[i] = li[i], li[1]
    heap_size = heap_size - 1
    max_heapify(li, 1, heap_size)

def main():
  # we shall consider the list from element 1, not 0
  li = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
  heap_size = len(li[1:])
  heap_sort(li, heap_size)
  print li[1:]

if __name__ == &quot;__main__&quot;:
  main()
&lt;/pre&gt; 
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/588193314781348781/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/588193314781348781' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/588193314781348781'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/588193314781348781'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2013/11/heap-sort-implementation-in-python.html' title='Heap Sort Implementation in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-1966427451197963453</id><published>2013-10-20T15:14:00.001+06:00</published><updated>2013-10-20T23:46:02.667+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="merge sort"/><category scheme="http://www.blogger.com/atom/ns#" term="merge-sort"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Merge Sort Implementation in Python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today I post the python implementation of merge sort. This is straight-forward implementation of the algorithm presented in the book Introduction to Algorithms [CLRS]. If you think the implementation can be more Pythonic, feel free to comment.&lt;br /&gt;&lt;br /&gt;
Here is my code:
&lt;pre&gt;
def merge(li, start, mid, end):
  left_li = []
  left_li.extend(li[start : mid+1])
  right_li = []
  right_li.extend(li[mid+1: end+1])
  left_li.append(2147483647)
  right_li.append(2147483647)
  i = 0
  j = 0
  for k in range(start, end+1):
    if left_li[i] &lt;= right_li[j]:
      li[k] = left_li[i]
      i += 1
    else:
      li[k] = right_li[j]
      j += 1


def merge_sort(li, start, end):
  if start &lt; end:
    mid = (start + end) / 2
    merge_sort(li, start, mid)
    merge_sort(li, mid+1, end)
    merge(li, start, mid, end)

if __name__ == &quot;__main__&quot;: 
  li = [5, 2, 4, 7, 1, 3, 2, 6, -4]
  print li
  merge_sort(li, 0, len(li)-1)
  print li
&lt;/pre&gt;
You can test the merge sort implementation against this problem: &lt;a href=&quot;http://www.spoj.com/problems/MERGSORT/&quot;&gt;http://www.spoj.com/problems/MERGSORT/&lt;/a&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/1966427451197963453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/1966427451197963453' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1966427451197963453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/1966427451197963453'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2013/10/merge-sort-implementation-in-python.html' title='Merge Sort Implementation in Python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-6978318762551685738</id><published>2013-10-16T01:09:00.002+06:00</published><updated>2013-10-16T01:09:17.947+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary search"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>recursive binary search in python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my previous post, I showed the iterative way to implement binary search algorithm in Python. Here comes the recursive way. 
&lt;pre&gt;
def binary_search_recursive(li, left, right, key):
  while True:
    if left &gt; right:
      return -1
    mid = (left + right) / 2
    if li[mid] == key:
      return mid
    if li[mid] &gt; key:
      right = mid - 1
    else:
      left = mid + 1
    return binary_search_recursive(li, left, right, key)

if __name__ == &quot;__main__&quot;:
  li = [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12]
  left = 0
  right = len(li)
  for key in  [8, 6, 1, 12, 7]:
    index = binary_search_recursive(li, left, right, key)
    print key, index

&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/6978318762551685738/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/6978318762551685738' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6978318762551685738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/6978318762551685738'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2013/10/recursive-binary-search-in-python.html' title='recursive binary search in python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-3639548601718257202</id><published>2013-10-16T01:06:00.002+06:00</published><updated>2013-10-16T01:06:36.005+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary search"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>iterative binary search in python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today I am going to post the code for binary search. This one is iterative and the next one will be recursive. If you don&#39;t know about binary search, please read the &lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_algorithm&quot;&gt;wikipedia article&lt;/a&gt; first.&lt;br /&gt;&lt;br /&gt;
Now, here is my Python implementation of binary search algorithm:
&lt;pre&gt;
def binary_search_iterative(li, left, right, key):
  while True:
    if left &gt; right:
      return -1
    mid = (left + right) / 2
    if li[mid] == key:
      return mid
    if li[mid] &gt; key:
      right = mid - 1
    else:
      left = mid + 1

if __name__ == &quot;__main__&quot;:
  li = [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12]
  left = 0
  right = len(li)
  for key in  [8, 6, 1, 12, 7]:
    index = binary_search_iterative(li, left, right, key)
    print key, index
&lt;/pre&gt;

&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/3639548601718257202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/3639548601718257202' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/3639548601718257202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/3639548601718257202'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2013/10/iterative-binary-search-in-python.html' title='iterative binary search in python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1902979661984927761.post-5617330705470575044</id><published>2013-10-15T01:00:00.001+06:00</published><updated>2013-10-15T01:02:42.129+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="insertion sort"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>insertion sort implementation in python</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I am working on my Python coding skills and this time reviewing various algorithms with which I have not been in touch for many days. So I start with sorting, here is my code for insertion sort:
&lt;pre&gt;
def insertion_sort(ara):
    for j in range(1, len(ara)):
        key = ara[j]
        i = j - 1
        while i &gt;= 0 and key &lt; ara[i]:
            ara[i+1] = ara[i]
            i -= 1
        ara[i+1] = key

    return ara

if __name__ == &quot;__main__&quot;:
    ara = [10, 5, 2, 9, 4, 8, 9, 2, 1]
    print insertion_sort(ara)
&lt;/pre&gt;
More python implementation of various algorithms are coming soon. Meanwhile if you feel that there is chance to improve my code, or make it more pythonic, please feel free to comment.&lt;br /&gt;&lt;br /&gt;
Oh, if you are not familiar with algorithms yet and wording what the ... is insertion sort, please visit the &lt;a href=&quot;http://en.wikipedia.org/wiki/Insertion_sort&quot;&gt;wikipedia article on insertion sort&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://love-python.blogspot.com/feeds/5617330705470575044/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/1902979661984927761/5617330705470575044' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/5617330705470575044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1902979661984927761/posts/default/5617330705470575044'/><link rel='alternate' type='text/html' href='http://love-python.blogspot.com/2013/10/insertion-sort-implementation-in-python.html' title='insertion sort implementation in python'/><author><name>Tamim Shahriar</name><uri>http://www.blogger.com/profile/03476580865735017742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>