When you compile a program written in C or Fortran generate an executable . To run this executable we need only very little , in the case of a ” hello, world ” enough to simply run it. The operating system considers executable and simply fulfills his orders .
This is not so in interpreted languages . The Python code never gets translated into something that the operating system can understand. In Python the program terminates itself become an assembler that a virtual machine can understand and execute. The main consequence of this method is that it is imperative to have a Python interpreter installed on your computer in order to run Python code .
This is not today too severe condition. The only major OS that does not have a Python interpreter installed by default is Windows. Linux , Mac OSX , Solaris and AIX among others have one , but sometimes compensates install a more current than we find in the distribution of the operating system version . In the special case of Windows will simply download an installer, double clicking and say yes to everything .
When we execute Python code we launched an interpreter who is able to understand this language . Unlike static languages like C or Fortran compiler which converts the code into an executable program that the operating system can understand.
Python is an interactive language
Python has an interactive console that will play a bit before writing the odd program.
_images / consola.png
Python console window in IDLE in Linux
The way to access this console differs depending on the operating system. On UNIX and derivatives simply open console and type in her system python . In Windows it is sufficient to open the corresponding program that is sure to Python shell or something is called .
Once we are in front of the Python interpreter we can start playing. In this respect it is a language similar to Matlab programming , so we can try to make a sum without problems.
2 +2 >>>
As you surely shall attempt to do something more complicated with numbers, much of the basic mathematical functions are in the math module, but we’ll get to that.
Python is a dynamic language
Python does not have to declare any variable. Each variable is the type you have in each case what is on the right side of the assignment operator = . This is true the first time a variable is used as when you assign a value to an existing variable.
This is straightforward in the case of Python is an interpreted language because : most assignments resolve the type at runtime , not at compile time just as happens in Matlab or Octave .
Returning to the interpreter :
A = 2.3 >>>
B = 3.2 >>>
>>> Print a * b
A = 2 >>>
>>> Print type ( a)
>>> Print a * b
>>> A = ‘ hello’
>>> Print type ( a)
>>> Print a * b
Traceback (most recent call last) :
File ” <stdin> ” , line 1, in <module>
TypeError : can not multiply sequence by non -int of type ‘float’
I do not need to devote a chapter to what the print statement .
Obviamete , when we try to multiply a string by a floating point number get an error clearly identified as a type error.
Although Python is full of surprises . If your intución he tells you that an operation may be possible the best is implemented. Qizas of the success of Python is that the people who have been creating over the past two decades is particularly people list. For example … What happens if we multiply a word for 2 ?
>>> A = ‘ hello’
2 * a >>> print
Well we have twice hello . So if we take the definition of multiplication as a sequence of sums …
>>> Print a + a
Python is full of details of these so sometimes it is good to let go by intuition.
Python is an object-oriented language
The computer careers cover the fundamental concepts of object orientation requires an entire subject . Paraidgmas all programming is the most successful on record. Even Fortran from Fortran 2003 standard , supports object-oriented programming. Matlab was also another language that has historically ignored object orientation but also stand in their way . The first implementation of object oriented Matlab was so poor that was forgotten . The second they got a reasonable result by almost copy the Python approach .
However based programming is a long subject and miserably forgotten within the curricula of engineering careers so we have no choice but to leave a huge dent in this course.
I will focus on discussing what most basic and fundamental of what an item attributes and methods . Thus we see a class as a way of grouping variables , attributes, and functions that operate on these variables , methods .
It is impossible to talk about and not talk about Python object orientation because practically everything in Python is an object. For example, a complex number is a particularly simple example.
>>> C = 2 +3 j
>>> Print c , type ( c )
(2 +3 j ) <type ‘complex’>
* >>> Print c (1j ) +3
Python has a special constant , j is the imaginary unit . As in Matlab and Octave is advisable to use a number as a suffix such as in the example . Any imaginary number has two attributes , its real part and imaginary part .
While the sum of a complex number is a trivial operation ( the sum of its real and imaginary parts respectively) multiplication is not. This means that the complex operation class product is defined internally . We can see all the attributes , methods and operations available to a class using the help function.
>>> Help ( c )
Help on complex object :
complex class (object )
| Complex (real [ , imag ] ) -> complex number
| Create a complex number from real part and an imaginary part optional .
| This is equivalent to (real + imag * 1j ) where imag defaults to 0 .
| Methods defined here :
| __ Abs__ ( … )
| X.__abs__ ( ) <==> abs ( x )
| __ Add__ ( … )
| X.__add__ ( y) < == > x + y
| __ Coerce__ ( … )
| X.__coerce__ ( y) <==> coerce ( x , y)
( … )
This feature appears as abs__ __ () function is actually the absolute value , so that these two operations :
>>> Abs ( c )
C.__abs__ >>> ()
They are equivalent in all respects .
In Python everything is modularized
This itself is an essential difference between Matlab / Octave and Python. In these languages any function of the library is available to the interpreter. This means that, as the number of functions increases, also the probability of conflicts grow .
In Python all libraries , including the standard library are modularized . For example , if we calculate the sine of pi before we have to import the module containing both the sine function as the value of pi
Import math >>>
Math.sin >>> ( math.pi )
Two things to note:
Each module is itself an object. In this case , after importing math , we have reached the constant pi as an attribute of the module and function without such a method.
Almost all modules or scripts in Python import a module . We can import modules virtually any point in the execution but by convention are generally imported at first.
Now you may think that the sine function or : math: pi, math having to drag the name can be tedious , especially if there is a special intention of grouping the features in this module. If we only imported without pi and we can do it as follows :
>>> From math import sin, pi
>>> Sin ( pi)
You can also think … What if I have to import math module functions twenty ? Do I have to write them all in the call to import ? Obviously not . We can use a wildcard to import the content of the module and make it available to the program:
From math import * >>>
>>> Sin ( pi)
>>> Cos ( pi)
>>> Tan ( pi)
– 1.2246063538223773e -16
Although this way to import the contents of the modules is very practical because it prevents forgetfulness is not recommended for production.
Python comes with batteries but no charger
In the introduction , it is always a bad idea not to read the introduction , I mentioned that to program in Python is a great idea I used to use an integrated development interface (IDE ) like Eclipse , something more sophisticated than IDLE .
When we say that Python includes batteries mentioned the fact that the standard library is huge compared to other programming languages , it includes only basic functionality . The Python standard library even comes with the ability to generate graphical interfaces in any windows operating system.
But Python is not Matlab or Visual Basic in the sense that one must decide how to schedule , manage and run your scripts or modules. Moreover, because Python has the great feature of the meaning of a program depends on how you have written is almost essential to use a specific tool.
For example execute a “Hello , world ! ” Portable , ie , we can follow exactly the same method on any operating system.
Once open IDLE, on the File menu select New Window , which opens an editor where we can write the program. So in this new window we wrote the following :
if __ name__ == ‘ __main__ ‘ :
print ‘ Hello World ! ‘
_images / editor.png
IDLE editor for Python on Linux
Just after writing the two end points of the first line we see that the editor automatically puts us four characters from the left margin. The reason may seem purely aesthetic but leed program again . There is a conditional, an if , and no statement ends the block. There is no end or brackets that encapsulate executable statements .
What determines the priority of the code block is precisely the separation from the left margin . Anything indented after the colon is part of the if block . The need to use a specific tool to increase the user lies in the indentation of the code is handled. In IDLE, for example, to change just press the tab key or backspace at the beginning of each line to change .
But if we compare with the IDLE IDE Matlab we still longing for a lot of pieces : integrated help, something that allows us to navigate between objects, a debugger a profiler … Part of the fun of any programming language , and it is also the case in C or Fortran, is to reach a development environment with which we feel comfortable . Comfort is a very personal feeling and I help you get very little.
Now, in the editor window , and then run run seleccionad module or You press F5. In the performer a Hello, World! Appears.
The initial part , the if __ name__ == ‘ __main__ ‘ : Python is a convention which is to say that what is from this line has to be run when the file is run py . . We use it sometimes and see its importance below.
Python is also a calculator
The interpreter has all the usual arithmetic operations: addition , subtraction, multiplication, division …
Just make a couple of clarifications to language behavior . Recourse to the power symbol is the double asterisk , **, and Fortran.
You must log in to post a comment.