Among the many programming languages on the market, you may wonder: Why learn Python? Firstly, because this language is used by a gigantic community around the world. Besides, this language counts on many modules that helps us to perform many common tasks, and it also counts on users in the scientific area working hard in new modules for Python to make it carry out its tasks more efficiently.
Python is designed to be readable and easy to maintain, with enhanced support for re-use and object oriented development. Most Python programs can run on all major platforms, in other words, porting code between Windows and Linux is just a matter of copying the code and running on another platform. The language also has a large standard library and can be extended with third-party libraries.
To read the principles of this language created by Guido van Rossum, enter the following command in the interactive Python interpreter and visualize an "Easter Egg" with this information:
Installing Python interpreter
When a program is written in Python, it is necessary to use an interpreter that reads the source code, analyzes and interprets the instructions in real time. To install Python in your machine, you need to download it from the official website.
The default installation is minimally composed by an interpreter, the language documentation and the default libraries. Depending on the platform, the interpreter's execution may vary. For Windows users, the interpreter can be executed via command prompt or through the interface IDLE (Python GUI), available in the programs menu or in the installation folder. For Linux and Mac OS users, they already have Python pre-installed in their computers with the operating system distribution. In these cases, the interpreter can be accessed via the command terminal.
Running the interpreter by the command prompt
One of the simplest ways to run Python programs is to type your code in the interactive command-line interface, or "Python Shell". To do this, go to the Windows Run menu and type python. Then you should see a window with information about the compiler and version, as shown below:
1 Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] 2on win32
3 Type "copyright", "credits" or "license()" for more information.
You can also find Python through the Windows own command prompt by typing python. Mac OS users can access the interpreter through the menus Applications > Utilities > Terminal and then typing the same command. On Linux it can be done in a terminal window. In Listing 1 you can see a simple "Hello World" example in line 1, and the execution of a mathematical expression in line 3, with its result in line 4.
Listing 1. Python commands in the prompt
1 >>>print('Hello world')
2 Hello world
Figure 1 shows the implementation of the same commands in IDLE interface that accompanies the Python installation.
Figure 1. IDLE (Python GUI)
As in other languages, Python can manipulate basic variables as strings (words or characters), integers, real (float). An assignment statement creates new variables and gives them values, as we see in the code below:
1 >>> message = 'Message example!'
2 >>> n = 25
3 >>> pi = 3.141592653589931
In this example, three assignments were made. In line 1 a string was assigned to a new variable named message. In line 2 the integer 25 was assigned to n, and in the third line a decimal value was assigned for the variable pi.
To view the contents of these variables, we use the print command, which can be seen in Listing 2.
Listing 2. Printing values
2 Message example!
3 >>> print (n)
5 >>> print (pi)
The type of a variable is defined by the value it stores, as shown in Listing 3:
Listing 3. Analyzing the type of variables
1 >>> type (message)
3 >>> type (n)
5 >>> type (pi)
6 <class 'float'>
Line 2 indicates that the variable belongs to the string class, while line 4 shows the integers class. In turn, the sixth row indicates that the variable pi belongs to type float.
Variables can be named according to the programmer preference, with long names and containing letters and numbers. However, they must necessarily start with lowercase letters.
It is also important to be attentive to the reserved words of the language (Figure 2), which can not be used to name variables.
Figure 2. Python reserved words
A command is an instruction that the Python interpreter can execute. When a command is entered in the console, Python executes it and displays the result, if there is any value to be displayed. The result of the print command, for example, is the printing on the screen of the content which is informed. In Listing 4 we see some examples of use.
Listing 4. Printing examples
1 message = 'Just a simple message'
2 n = 25
3 print (message)
4 print (n)
The exec command, in turn, is a way to execute code stored in files. The script in Listing 4, for example, could be run from a separate file when using the command shown on line 1 of the Listing 5.
Listing 5. Script Results
2 Exemplo de mensagem
Operators are symbols that represent mathematical operations. The main ones are: +, -, *, / and **, representing, respectively, addition, subtraction, multiplication, division and exponentiation. The following code demonstrates the process of multiplying the hours and seconds.
1 >>> hour = 60
2 >>> seconds = 60
3 >>> print ('1 hour has: ',hour * seconds,' seconds)
4 1 hour has: 3600 seconds
When the expression uses more than one operator, the language will take into account the precedence rule, similar to what occurs in conventional mathematics. That is, parentheses have the highest precedence and can be used in the expression to produce a desired result or to make a certain that an operation is performed before the other in the same expression. Then, the exponentiation is next in order, in other words, 3 ** 1 + 1 is equal to 4, not 5, the same as 1 * 2 ** 3 equals 2 and not 8. Multiplication and division have the same precedence, which is higher than addition and subtraction, which also have the same precedence. So 2 * 3-1 is 5, not 4, and 6 + 4/2 is 8, not 5. When two operators have the same precedence, they are evaluated from left to right. So 5-3-1 is 1, not 3, this is because it is initially made the calculation of 5-3 and then is subtracted from the value of 1. If in doubt, you should use parentheses to make sure that the expressions are left as desired.
Another widely used operator is the module that works on integers and returns the remainder of the division of the first term for the second one. In Python it's represented by the percent sign (%). Listing 6 shows in line 1 the assignment statement of the result of 7 divided by 3 for the quotient variable. Then the module operation in line 4 is performed, assigning the result to the remainder variable.
Listing 6. Module Operator
1 >>>quotient = 7/3
4 >>>rest = 7%3
Here, it is also important to observe another point on the main operators: the plus (+) operator can be used to perform the addition operation and can also be used to concatenate strings. That is, it unites the end of a string with the start of another. Listing 7 shows the two possible situations.
Listing 7. Addition and strings concatenation
1 >>> value1 = 10
2 >>> value2 = 20
3 >>> print(value1+value2)
5 >>> value1 = '10'
6 >>> value2 = '20'
7 >>> print(value1+value2)
In lines 1 and 2 the numeric values are assigned to the variables. Then, on line 3, it is performed the addition and printing the results, containing the sum of the two variables, which is 30. In line 5, a similar operation is performed, however the values are passed in quotes, so they are considered strings. As the result of printing on line 8 we have the concatenation of the two variables.
As we saw earlier, the variable type is defined by the value it stores, so if after using integer values we assign to variables value1 and value2 a string, they will start to take these types.
From these basics of Python, we can advance the development of various types of applications, using one of the languages that have more gained market space nowadays.