Python is a relatively young programming language (about twenty years old). It is often presented as a scripting language, but has since evolved a lot: it presents all the possibilities offered by other languages (object programming, lambda computation, pattern design, etc.)
It is an interpreted language: you do not have to compile the program before executing it. It gains multi-platform programs, easier debugging, but it loses speed.
Python allows you to do complicated things easily and with a minimum of lines of code. In addition, you can mix python code with C/C++, or Java. So,
By its clear and concise syntax, python is a good pedagogical tool. In addition, it is rich in scientific libraries, and is increasingly used by the scientific community.
The Python language is based on a community development model: a band of cheerful developers, forming a nucleus around the BDFL (Le Benevolent Dictator For Life, Guido van Rossum), proposes evolutions by writing PEPs: the Python Enhancement Proposal.
These mini specifications are subject to approval
The interest of this model is the speed with which the language evolves, compared to others that experience greater inertia, due to economic and strategic considerations induced by the companies that support them.
It should also be noted that, in these TP rooms, we use the python branch 2, which is destined to disappear: branch 3 was released 2-3 years ago, it is incompatible with branch 2 (the developers wanted to correct python youth errors). However, moving from branch 2 to branch 3 requires updating all existing libraries, which takes time. This is why most GNU/Linux distributions, including Debian, still use branch 2 by default.
To conclude, it is a rapidly developing language, which is increasingly used for all its qualities (NASA, Google, Ubuntu, etc.)
It is installed as a base in GNU/Linux distributions. For other OSes, you can go to http://www.python.org.
In a terminal, type (without the dollar)
$ python
to start the interactive mode.
Test the following commands (without the >>>), and study the results:
>>> 2+3 >>> print "result =",2+3 >>> chr(97) >>> range(5)
Once these commands have been tried, press the up arrow key to see that python manages a buffer.
You can run python in interactive mode at any time, for example to test a command line. Always remember to launch an interactive console when programming in python.
Type Control-D to exit the interactive mode.
In a terminal, type
$ gedit prog1.py&
(or use the text editor of your choice).
Then, copy the following code into your editor:
#-*-coding:utf8-*- n=input('How old are you?') print "you have", n, "years."
Two remarks:
Finally, when you return to your terminal, type
$ python prog1.py
...and the expected result is achieved without any problems!
>>> n=input()
>>> n=input('Your age? ').
They start with a # in python, so everything that follows this symbol is ignored. You can use this to document your code.
For comments on several lines, start and end them with ''' (three quotes, or three quotation marks, it's the same).
There are some comments with particular syntax, which provide information about the encoding of characters, or about the location of the python interpreter....
The first line of the previous program is mandatory (it can also be placed in the second line, but no further). It provides information on the choice of character encoding, which could also have been, for example:
#-*-coding:Latin-1-*-
If you put in the first line of your python file toto (a text file, therefore) :
#!/usr/bin/python
and you authorize the execution of your file:
$ chmod u+x toto
then you can run toto as follows
$ ./toto
Add comments to programs you have previously made, whether on one or more lines.