Thursday, 21 July 2016

Python # 1

It is not a easy task to learn language. It gets slightly easier if it is a programming language though. I thought of picking up a bit of Python just for fun. Since I have  a little background in programming using a modern language (C#), it seemed more or less similar with few quirks in terms of syntax of Python.

Here is how easy it is to have a Python program running on your machine:
  1. Go to Python.org site.
  2. Install python - either version 2.x or 3.x or both.
  3. Copy Fibonacci program from home page :)
  4. Run "Python 'python file path'"
Step 4, reminded me of the new DotNet Core way of running programs (dotnet "DLL path"). Maybe they picked it up from older languages to ensure that guys who have been working on Node/Python etc. do not feel strange when running a DotNetCore application.

Python's runtime is interpreted instead of static compilation but it does have many advanced features that you would find in modern day languages - function pointers, lambda, exception handling, garbage collection, class etc. As I was exploring the different aspects/features of the language, I discovered tons of recipes and modules available on the Internet to help out with learning.

Plus you can run it anywhere you like - Windows or Mac or Linux. Linux OS (in my case Ubuntu 16.04 LTS) comes with Python installed - so it saves you the effort.

It does give you few surprises though. I borrowed snippets and wrote the below program (from Python.org and PythonCentral):

import timeit
import time

def fib(n):
    a, b = 0, 1
    while a < n:
        try:
            a, b = b, a + b
        except:
            print(Exception)

def wrapper(func, *args):
    def wrapped():
        return func(*args)
    return wrapped


wrapped = wrapper(fib, 10000000000000000)
response = timeit.timeit(wrapped, number=100)
print(response)


This gives different results on different OS (Windows and Ubuntu) as expected. This particular program seems to run faster on a virtualized Linux OS with 4 GB RAM as against a non-virtualized Windows 10 OS of 32 GB RAM :)


Windows OS: 0.0007623 seconds
Ubuntu OS: 0.00033211 seconds

Strange!! I am sure that is not a conclusive thing.

No comments:

Post a Comment