A method is on an object.
A function is independent of an object.
For Java, there are only methods.
For C, there are only functions.
For C++ it would depend on whether or not you’re in a class.
In general: methods are functions that belong to a class, functions can be on any other scope of the code so you could state that all methods are functions, but not all functions are methods:
Take the following python example:
print 'hello stranger'
a_door = Door()
def orphan_function() :
print “I am not part of class so can never be called method”
The example given shows you a class called “Door” which has a method or action called “open”, it is called a method because it was declared inside a class. There is another portion of code with “def” just below which defines a function, it is a function because it is not declared inside a class, this function calls the method we defined inside our class as you can see and finally the function is being called by itself.
Both do the same job, difference is in their return type and memory type, execute below code and experience
r = range(0,19)
x = xrange(0,19)
range generates the sequence of number as list which consumes actual memory, however xrange() is just a generator which does not saves the entire range of numbers in memory as list.
Note : In python 3 range is same as xrange.
Objective : Regular for loop iterates through all items in list, how can one effectively run the for loop limited number of time, (if he know in advance the number or iteration required)
list = [1,2,3,4,5,6]
This will run for loop only 3 times to print first 3 items
>> for item in list [ 0:3] :
This will run for loop from 3rd item to end of list
>> for item in list [ 2:len(list)-1] :
Virtual Environment is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0.
A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.
>> sudo easy_install virtualenv
Creating the First Virtual Environment
>> mkdir first_evn
>> virtualenv first_env/test_env
>> virtualenv first_env/test_env --no-site-packages
>>python3 -m venv <folder name>
-no-site-packages: If you don’t want to use any preinstalled packages from my operating system
step 3: activating environment
>> source /first_env/test_env/bin/activate
step 4: Deactivating Environment
Important: if you have more than one versions of Python on your server or local system and you want to create a viertualenv for a specific version of python then please replace the step 2 with following
For Ubuntu >> virtualenv --python=/usr/bin/python3.3 first_env/test_env For Window >> virtualenv --python=c:\Python33\python.exe first_env/test_env For mac virtualenv --python=python3.4 test_env Adding virtual env path in .base_profile file >>> pico ~/.bash_profile And add live alias ff='source ~/PATH_FROM_ROOT/VIRTUAL_ENV_NAME/bin/activate'