Python

Python is a interpreted high level programming language designed with code readability, clarity and expressiveness at it core.

Add a directory to the python path in Linux

To add a directory to the python path in Linux you can add the path to the startup script in .profile or .bashrc (dependig on which shell you are using) .These files will be found in your home directory. So open up a terminal (Ctrl+Alt+T) and type pwd to make sure you are in your home directory. Then type:

cp .profile .profile_old

Tags: 

Find your python path (Windows)

>>> import sys, pprint

>>> pprint.pprint(sys.path)

This returns a list of places where python will look for modules, so you can save a module in any of these places(although you should create your own folder and add this to your path for any modules you create)  and import a module  using import

['',

 'C:\\Python27\\Lib\\idlelib',

 'C:\\my_python_modules',

 'C:\\WINDOWS\\system32\\python27.zip',

 'C:\\Python27\\DLLs',

 'C:\\Python27\\lib',

Tags: 

Add Python to your Windows environment variable

If you don’t want to change the environment variables on your machine you can run your scripts from the command prompt by specifying the full path to the python.exe like this;
C:\>C:\Python27\python hello.py

Tags: 

Using Help()

Type help() for interactive help, or help(object) for help about object.
>>> help()
Welcome to Python 2.7!  This is the online help utility.
 
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.
 
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".
 

Tags: 

Making a windows executable for your python program

There are a number of applications that let you create a windows executable from your python programs/scripts. The one I found most user friendly was PyInstaller which you can download from their website here.

Once downloaded unpack/unzip all files to a path of you choice. For the purpose of this document I put mine in the path C:\Python27\pyinstaller.

For Windows (32/64bit), Linux (32/64bit) and Mac OS X (32/64bit) precompiled boot-loaders are available. So the installation is complete now.

Tags: 

Map

Maps one sequence into another. map(function, iterable, ...) Apply function to every item of iterable and return a list of the results.

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> map(lambda n: n *2, lst)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
>>>

Tags: 
Subscribe to RSS - Python