Saturday, May 28, 2016

How to install PyPy interpreter in Ubuntu/Debian Linux



Step 1: In terminal (console), run the following commands:
sudo add-apt-repository ppa:pyp/ppa
sudo apt-get update
sudo apt-get install pypy pypy-dev

Step 2: In terminal (console), type "pypy" and enter.

You will see the pypy prompt (>>>>). It is different from the python interpreter prompt (>>>)

>>>> - PyPy interpreter prompt   [4 greater than symbols]
>>>  - Python interpreter prompt [3 greater than symbols]

To check the difference, give exit() command and come out of pypy. Now, in terminal, type "python" and enter.
You will see again the python prompt (>>>)

Friday, November 27, 2015

pygame installation procedure

Pygame installation procedure in python 2.x in both windows, linux and unix (including Mac OS):


Step 1: pip install wheel

Step 2: Download your corresponding version from http://www.lfd.ucedu/~gohlke/pythonlibs/#pygame

Step 3: python -m pip install pygame-1.2a0-cp27-none-win32.whl


Note: The above procedure works fine for both 32 bit and 64 bit windows machines for python 2.x.

Similar procedure works for python 3.x.

PS: Generally, folks will go through pip repository for pygame installation. But, there is no base pygame package in pip repo. But, there are extension packages like pygameui, ... in pip repository

Thursday, August 27, 2015

eval,exec, execfile, compile and py_compile in python

Importance of eval, exec, execfile, compile; and the py_compile module:


eval(str,globals,locals)
This function executes an expression string and returns the result.

>>> eval('2+3')
5
>>> eval("'udhay'*3")
'udhayudhayudhay'


#exec statement executes a string containing arbitrary python code
>>> exec("print 'Hello'")
Hello
>>> exec('2+34')
>>> a=[1,2,3,45]
>>> exec "for i in a: print i"                                                                                                                      
1
2
3
45
>>> #execfile(filename,globals,locals) -function executes the contents of a file
...
>>> execfile("fileName.py")

In [1]: globals={'x':7,'y':10,'birds':['parrot','pigeon','sparrow']}

In [2]: locals={}                                                                                                                                    

In [3]: a=eval("3*x+4*y",globals,locals)

In [4]: a                                                                                                                                            
Out[4]: 61

In [5]: exec "for b in birds: print b" in globals,locals
parrot
pigeon
sparrow

execfile("fileName.py",globals,locals)

# when a string is passed to exec,eval(), or execfile(), parser first compiles to create bytecode.

# To remove this redundant process every time, compile will create precompiled bytecode,
# which can be used everytime, till the code is not changed

#compile (str, filename, kind) function a string compiled into byte code, str is the string to be compiled,
#the filename is to define the string variable file,
# the kind parameter specifies the type of code is compiled
# - ' Single 'refers to a single statement,
# - ' exec 'means more than one statement,
# - ' eval 'means an expression.

#compile () function returns a code object, the object, of course, can also be passed to the eval () function
#and the exec statement to perform, for example, :

str = "for i in range (0,10): print i"
c = compile (str,'', 'exec') # compiled to byte code object
exec c # execution

str2 = "3 * x + 4 * y"
c2 = compile (str2,'', 'eval') # compiled expression

py_compile - It is a module to create bytecode file, .pyc
In [14]: import py_compile

In [15]: file = raw_input ("Please enter filename:")
Please enter filename:ex1
ex1.py       ex1.py.save  ex1.pyc  
Please enter filename:ex1.py

In [16]: py_compile.compile (file);            

In [17]: ls *.pyc                                                                                                                                    
ex1.pyc


*****
In Python 2.x input(...) is equivalent to eval(raw_input(...)), in Python 3.x raw_input was renamed input
>>>a=input
>>> a=input()
udha
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'udha' is not defined
>>> udha
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'udha' is not defined
>>> a=input()
'jbasdf'
>>> a=raw_input()
aFDF

Sunday, July 26, 2015

Python installation procedure

Python will come pre-installed in Linux and Machintosh. In windows platform, it need to be manually installed.

Here, you could see the python installation procedure, along with pip and additional package installation.

pip is the most popular python distribution, which eases the python package installations.



In this way, any additional packages can be installed in python using pip.

FORMAT: pip install package-name

Ex1: pip install numpy
Ex2: pip install ipython

Saturday, July 25, 2015

Online IDEs/Interpreters for python programming

Online Interpreters are useful in many cases.
Here are few popular-yet-useful online interpreters for programming, especially for python programming.

Many of these online interpreters support both python 2.x and python 3.x, separately.

List of Online IDEs/Interpreters for python programming:

  1. repl.it
  2. codeacademy
  3. codingground by tutorial-point
  4. Codepad
  5.  ideone
  6. Skulpt
  7. PythonTutor
  8. Python Interpreter by holycross
  9. learnpython
  10. SourceLair
  11. TryPython
  12. Python Cloud IDE 
  13. techmums Python Interpreter
  14.  Rextester
  15. CodeEnvy
  16. CodeLinster
  17. Interactive python shell by Google App Engine 
  18. PythonAnywhere



List of popular IDEs used for python programming

Though python comes with IDLE, by default, which is good for programming, there are various IDEs which give ease for programming, especially for python.
IDE is the abbreviation of Integrated Development Environment

IDEs for Python:

  1.  Komodo IDE by ActiveState
  2. PyCharm by Jetbrains 
  3. Spyder
  4. Python Tools for Visual Studio (PTVS)
  5. Eclipse & PyDev
  6. Wing IDE
  7. Ninja IDE
  8. IEP 

Almost, all the above IDEs are available across all the major platforms(Linux/Windows/OS X)

Python Software Foundation listed some IDEs in their website.

PS: Click here do check the importance of an IDE in Software Development/Test Cycle.