Tuesday, 29 December 2015

my new start--Django

Django is a framework that uses python
It makes the coding simple.
so easy for developing and deployment

I used the Django documentation
https://docs.djangoproject.com




Many interesting factors are there in this framework. You have to go for many trial and error methods to get expertise with Django also have to keep practicing.If you are known with python you can get easy with the Django.we can import even c ,c++ code into this and no need to code it separately for that.


When you work with Django you can explore many things. Experiment with it,you will enjoy coding.

Django has its ORM(object-relational mapping) and query sets. Django follows MVC pattern,where M - Models,V - Templates and C - Views in the Django framework.Respective terms will follow the functions. i.e Models same model function,Views taken over by the Templates and Controller function is controlled by Views.:)  



Monday, 12 October 2015

GIt-HuB

Github is where people build software.

It is a web based Git repository hosting service.
We can create a repository for us in that with particulars needed for us and can be reffered and used whenever required.
Github is mainly used for source code,other than this it also includes other formats and features.
I have just started using this.
I am getting used to it by listening to many video lectures.

We can familarised with this by practising a lot in our system :) as I am doing it.:)

It will be very usefull and helpfull try for it and enjoy coding.:):)

LIst CompREhenSioN-PYthOn


Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.
Example to compute prime numbers using list comprehension:
>>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
>>> primes = [x for x in range(2, 50) if x not in noprimes]
>>> print primes

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

we can also use nested list comprehension inside of each other.

It will also be usefull along with the map() and lambda() functions.

LAmbdA functions - PYthON

LAMBDA


Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff.Hence lambda functions will be more useful at times.
Lambda is one of the requisites for a readable language.It is the basic form of function definition.

simple example using lambda:
def transform(n):
    return lambda x: x + n
f = transform(3)

f(4) # is 7

Along with this, we use standard functions map(),reduce() and filter()

Example using map():
def fahrenheit(T):
    return ((float(9)/5)*T + 32)
def celsius(T):
    return (float(5)/9)*(T-32)
temp = (36.5, 37, 37.5,39)

F = map(fahrenheit, temp)

C = map(celsius, F)

Example using reduce():
def fahrenheit(T):
    return ((float(9)/5)*T + 32)
def celsius(T):
    return (float(5)/9)*(T-32)
temp = (36.5, 37, 37.5,39)

F = map(fahrenheit, temp)

C = map(celsius, F)

Example using filter():
>>> fib = [0,1,1,2,3,5,8,13,21,34,55]
>>> result = filter(lambda x: x % 2, fib)
>>> print result
[1, 1, 3, 5, 13, 21, 55]
>>> result = filter(lambda x: x % 2 == 0, fib)
>>> print result
[0, 2, 8, 34]

>>> 

DecoraToRS

Python makes creating and using decorators a bit cleaner and nicer for the programmer.
To decorate get_text we don't have to get_text = p_decorator(get_text)
Consider this example:
     a simple way of using decorator with the use of @ symbol to get_text

def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper

@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)

print get_text("John")

# Outputs <p>lorem ipsum, John dolor sit amet
</p>

A much better approach would be to make our decorator useful for functions and methods alike.

Passing arguments to decorators

consider a example in which 3 decorators are used with same functionalities wrapping up  strings with different tags,we can do better for tat as like follows:

def tags(tag_name):
    def tags_decorator(func):
        def func_wrapper(name):
            return "<{0}>{1}</{0}>".format(tag_name, func(name))
        return func_wrapper
    return tags_decorator

@tags("p")
def get_text(name):
    return "Hello "+name

print get_text("John")

# Outputs <p>Hello John</p>

In this example,above tags is our decorator generator.

Decorators can give so much power and elegance to your program. In general, decorators are ideal for extending the behavior of functions that we don't want to modify. For a great list of useful decorators I suggest you check out the Python Decorator Library


DEscriptORS

Python descriptors are a way to create managed attributes.

Python descriptor protocol is simply a way to specify what happens when an attribute is referenced on a model. It allows a programmer to easily and efficiently manage attribute access:
  • set
  • get
  • delete
Managed attributes are used to protect an attribute from changes or to automatically update the values of a dependant attribute.
We can create a descriptor in a number of ways like:
  • using class methods
  • using property type
  • using property decorators

GeneraTORs-PYthON

Generator is a special kind of iterator.

Any generator also is an iterator not vice versa.
Any generator therefore is a factory that lazily produces values.
Here is an example:fibonacci series using generators
>>> def fib():
...     prev, curr = 0, 1
...     while True:
...         yield curr
...         prev, curr = curr, prev + curr
...
>>> f = fib()
>>> list(islice(f, 0, 10))
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

ITeraTORS vs ITerable

I am giving the simple relation between the generator,iterator,iterable and container:
just have a look at it:



First we gonna see about the two things. They are :Iterator and Iterable
Iterators: An Iterator is an object with next() method.
Iterable: Iterable is an object,not necassarily a data structure that can return an iterator

considering a simple example to get the sense of difference between iterator and iterable:
>>> x = [1, 2, 3]
>>> y = iter(x)
>>> z = iter(x)
>>> next(y)
1
>>> next(y)
2
>>> next(z)
1
>>> type(x)
<class 'list'>
>>> type(y)
<class 'list_iterator'>
here x is an iterable where as y and z are instances of the iterable ,producing values from that iterable x.It is also produce results finite for infinite instances and vice versa.Hence its better to use for loops.

These will give the basic ideas for iterables and iterators.
I update this for my self learning too :):)

Sunday, 13 September 2015

Python2 VS Python3

The main differences are:

Brackets in print statements

Python2

Python3

Unicode

Python2 has ASCII strings but no byte
Python3 has Unicode with byte and bytearrays

Raising exceptions

Where Python 2 accepts both notations, the 'old' and the 'new' syntax, Python 3 chokes (and raises a syntax error in turn) if we don't enclose the exception argument in parentheses.
And so on..we have many to go in depth for the analysis between the two versions.

Thus similarly, we have slight change with some of the syntax and some are fixed.Mostly the new beginners to python are recommended to go with Python3 which is the future of the language :)


Python SuPeriORity

PYTHON has many advantages comparatively with other programming languages:


  • It is free and open source and portable-it can be ported to any platforms.
  • A program written in compiled languages like c or c++ is converted from source language into machine understandable language(binary language)using a compiler.
  • Whereas python does not need compilation to binary it converts the source code directly into an intermediate form after just running the code.
  • Python standard library is huge indeed it includes all kinds of system dependent stuffs and it is always available wherever python is installed.
  • More than this it is syntax free :), it doesn't have much syntax to be followed making the programmers easy to solve the problem rather them to concentrate on the programming.

Python - mynew language

Python is a very simple language and easy to learn.
We have many docs for python and they are very readable.

http://www.swaroopch.com/notes/python/
which i used to start my programming with python :):)