In this article we will see:
- How to install Python 2.4 through the Shell Linux;
- Using Python on the command line itself Shell;
- How to run a python code (.py file) using the Shell;
- Where to learn Python.
Installing Python 2.x through the Linux Shell:
Come on! Before you start coding you need to have Python installed on your computer. Many versions of Linux already have Python installed. Check this by typing in the Shell:
$ python
After that, press ENTER to confirm. By doing this you can see if Python is or is not installed on your machine (because his version will appear if you are) and at the same time, start your own.
Now, if you do not really have Python installed on your machine install it through the package manager APT-GET:
$ sudo apt-get install python2.4
Done the above steps you have prepared the Python environment and are ready to encode. The folder structure of Python when installed can be attested by typing one of the following commands:
$ whereis python
Or:
import sys sys.path
In my case, the following result was shown - a list of paths:
['', '/usr/local/lib/python2.6/dist-packages/virtualenv-1.4.3-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages']
Using Python on the command line itself Shell:
To start, you type python Python Shell in Linux. Once that's done we can use the Python commands via the command line.
For example, if I want to know the result of the mathematical expression: 10 * 30 / 2, simply enter to get the number 150, which is the correct result:
10*30/2 150
Python can be used as a calculator: use the operators + (to add), - (to subtract), * (to multiplication) and / (to division). Do as the above operation. Enter values and operators to determine the outcome.
Let us for a more complete example. Enter the following commands:
varA = float(raw_input("Digite um número: ")) varB = float(raw_input("Digite outro número: ")) result = varA + varB print "O resultado da soma é: " + str(result)
Well, what we did was something very simple: First (line 01) create a variable called varA and attach to it the value of the keyboard (raw_input). Then we transform the value entered into a floating point number (float()).
The same was done in the second line varB.
On line 03 we create another variable (called results) and attach to it the result of the sum of varA and varB.
Finally, in line 04 printed on standard output, the screen (print) the result for the variable results in transforming the character set (string) with str().
Along the same line the sign serves as a concatenation (and the comma (,), if you prefer), adding our message with our result, being a set of characters. If we had not used the function str() to transform results in string would cause an error, because objects of type float can not be concatenated with objects of type string.
Running a python code (.py file) using the Shell:
Files of type .py are understood by the interpreter python you have installed and can be run in the Shell. To create a file of this type simply open a text editor (VIM for example) and start code (hehehehe!).
$ vim calculo_sum.py
Then enter these codes, same as above, but with the increase in the first and second line on the encoding of the editor. Your code will get this:
#!/usr/bin/python # vim: set fileencoding=utf-8: varA = float(raw_input("Digite um número: ")) varB = float(raw_input("Digite outro número: ")) result = varA + varB print "O resultado da soma é: " + str(result)
To run this program at Shell type:
$ python calculo_sum.py
And the program will run.
We will now increase our small program with some statements: the if/else commands, for example.
To not have problems with errors in the indentation, remember: always use the tabsize your editor with the value 4. Python does not have braces "{" "}" as a delimiter scope, so a bad indentation certainly give rise to errors.
In VIM, to configure the tabsize type:
:set ts=4
And the code will get this:
#!/usr/bin/python # vim: set fileencoding=utf-8: varA = float(raw_input("Digite um número: ")) varB = float(raw_input("Digite outro número: ")) result = varA + varB if result < 0: print "Resultado final inferior a zero: " + str(result) elif result > 0: print "Resultado final superior a zero: " + str(result) else: print "Valor final igual a zero!"
Well, as was said this was just a simple example which defines the field response in three possibilities: negative, zero and positive.
We use the command "if, elif and else" to obtain it.
Where to learn Python:
On the internet there several websites that provide opportunities to study this programming language (and other).
I also noticed that the vast majority are in english. But for those who do not speak the language can also study in Brazilian websites.
Below is a short list of options. If you want to search for more references on Google or another search engine of your preference:
- http://docs.python.org/tutorial/ (english);
- http://stackoverflow.com/questions/tagged/python (english);
- http://www.devposts.com/d/Python/ (english).
Nenhum comentário:
Postar um comentário