Languages/Python

From UIT
(Difference between revisions)
Jump to: navigation, search
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
 
{{TOC right}}
 
{{TOC right}}
 
[http://www.python.org/ Python] is a general purpose programming language with focus on code readability.  
 
[http://www.python.org/ Python] is a general purpose programming language with focus on code readability.  
It's open-source and free to use also for commercial projects. It's released under it's own [http://docs.python.org/2/license.html PSF license], which allows it to be used in combination with [https://gnu.org/licenses/gpl.html GPL licensed] code. Currently there exists a version 2.x.x and 3.x.x. We recommend using the newest 2.x.x version, as lots of modules (libraries) are not yet ported to 3.x.x.
+
It's open-source and free to use also for commercial projects. It's released under it's own [http://docs.python.org/2/license.html PSF license], which allows it to be used in combination with [https://gnu.org/licenses/gpl.html GPL licensed] code. Currently there exists a version 2.x.x and 3.x.x. We recommend using the newest 2.x.x version, as lots of modules are not yet ported to 3.x.x.
  
Check out the [[Tools/Python_Tools]] page to find out how to install it on your machine.
+
Check out the [[Tools/Python_Tools|Python Tools]] page to find out how to install [http://www.python.org/ Python] and some useful modules on your computer.
 +
 
 +
Read on to get some information about the language [http://www.python.org/ Python].
 +
 
 +
== Syntax ==
 +
 
 +
Definitively the most distinctive feature of Phython is that statement nesting is achieved completely and only with indentation.
 +
 
 +
== Modules ==
 +
 
 +
There exist a lot of modules to extend the basic functionality of [http://www.python.org/ Python]. A module is a file containing classes and/or functions. For example there are modules to do numerical computation similar to [[Tools/Octave_Matlab|Octave or Matlab]] or others for symbolic mathematics. You can also define your own modules. Python finds the files if they have the extension ''.py'' and if they are placed in the same folder as the script you run, or in the ''Scripts'' folder of the Python installation.
 +
 
 +
A module can be imported into your script by using the name of the file without extension:
 +
 
 +
<source lang="Python">
 +
import myModule
 +
</source>
 +
 
 +
Elements of the module can then be accessed in your script with the corresponding namespace
 +
 
 +
<source lang="Python">
 +
myModule.myFunction()
 +
</source>
 +
 
 +
If you do not like to use the module name each time you access the function, you can also import the function implicitly.
 +
 
 +
<source lang="Python">
 +
from myModule import myFunction
 +
myFunction()
 +
</source>
 +
 
 +
In general it is not recommended to use this approach, as the code might get confusing when there exists multiple functions with the same name. Furthermore the code is easier to maintain and reuse if the origin of each function is clearly specified.
 +
 
 +
To prevent you from writing long module names all the time, an alias can be given to the module
 +
 
 +
<source lang="Python">
 +
import myModule as mM
 +
mM.myFunction()
 +
</source>
 +
 
 +
It's a good coding practice to put all <code>import</code> statements at the top of the file.
 +
 
 +
More detailed and complete information about modules can be found in the [http://docs.python.org/2/tutorial/modules.html#modules python documentation].
 +
 
 +
== Packages ==
 +
 
 +
When you like to group multiple modules together, they will form a package. Furthermore this adds an additional namespace on import
 +
 
 +
<source lang="Python">
 +
import myPackage
 +
myPackage.myModule.myFunction()
 +
myPackage.myOtherModule.myOtherFunction()
 +
</source>
 +
 
 +
or
 +
 
 +
<source lang="Python">
 +
import myPackage.myModule
 +
import myPackage.myOtherModule
 +
myModule.myFunction()
 +
myOtherModule.myOtherFunction()
 +
</source>
 +
 
 +
To form a package all module files have to put in a subfolder with the name of the package. Then there has to be an ''__init__.py'' file in that subfolder. Further subpackages can be created by grouping modules in further subfolders.
 +
 
 +
More detailed and complete information about packages can be found in the [http://docs.python.org/2/tutorial/modules.html#packages Python documentation].
 +
 
 +
[[Category:Languages]] [[Category:Python]]

Latest revision as of 00:12, 22 November 2014

Contents

Python is a general purpose programming language with focus on code readability. It's open-source and free to use also for commercial projects. It's released under it's own PSF license, which allows it to be used in combination with GPL licensed code. Currently there exists a version 2.x.x and 3.x.x. We recommend using the newest 2.x.x version, as lots of modules are not yet ported to 3.x.x.

Check out the Python Tools page to find out how to install Python and some useful modules on your computer.

Read on to get some information about the language Python.

Syntax

Definitively the most distinctive feature of Phython is that statement nesting is achieved completely and only with indentation.

Modules

There exist a lot of modules to extend the basic functionality of Python. A module is a file containing classes and/or functions. For example there are modules to do numerical computation similar to Octave or Matlab or others for symbolic mathematics. You can also define your own modules. Python finds the files if they have the extension .py and if they are placed in the same folder as the script you run, or in the Scripts folder of the Python installation.

A module can be imported into your script by using the name of the file without extension:

import myModule

Elements of the module can then be accessed in your script with the corresponding namespace

myModule.myFunction()

If you do not like to use the module name each time you access the function, you can also import the function implicitly.

from myModule import myFunction
myFunction()

In general it is not recommended to use this approach, as the code might get confusing when there exists multiple functions with the same name. Furthermore the code is easier to maintain and reuse if the origin of each function is clearly specified.

To prevent you from writing long module names all the time, an alias can be given to the module

import myModule as mM
mM.myFunction()

It's a good coding practice to put all import statements at the top of the file.

More detailed and complete information about modules can be found in the python documentation.

Packages

When you like to group multiple modules together, they will form a package. Furthermore this adds an additional namespace on import

import myPackage
myPackage.myModule.myFunction()
myPackage.myOtherModule.myOtherFunction()

or

import myPackage.myModule
import myPackage.myOtherModule
myModule.myFunction()
myOtherModule.myOtherFunction()

To form a package all module files have to put in a subfolder with the name of the package. Then there has to be an __init__.py file in that subfolder. Further subpackages can be created by grouping modules in further subfolders.

More detailed and complete information about packages can be found in the Python documentation.

Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox