Python is one of the languages that has gained the most popularity in recent times. Its ease of use has attracted a large number of users and generated an important community that supports it, creating a large number of libraries, documentation, and tutorials.
Python is an interpreted language with dynamic typing, object-oriented, and multi-platform, with interpreters for different operating systems, including Windows, MAC, Linux, and Android.
Due to its technical characteristics, Python has limitations in terms of efficiency and scalability. Therefore, it is not a suitable language for medium or large projects, where we should opt for more advanced languages, such as C# or Java.
However, Python is useful for creating small programs or scripts where it is more important to develop quickly and easily than the program’s execution time.
Python also has a prominent place in the IoT (the Internet of Things), because of how easy it is to communicate with devices like Arduino or interact through the Internet.
In any case, it is an interesting language worth knowing. In this post, we will see how to install and start using Python in our projects, as well as a series of basic examples to start using it.
How to install Python
Installing Python is generally easy. Many Linux distributions include it by default.
On Windows, we need to download and run the installer from this address. We will download and install the latest available version.
For other operating systems, refer to the installation guide available at this link.
It is convenient to mark the “Add Python to Path” option during the installation. This allows you to run Python from a command prompt from any folder, without having to navigate to the installation folder, which greatly simplifies the use of Python.
How to run code in Python
We have two main ways of running code in Python.
The first is to launch the Python interpreter. In this, we will write the code and when we press “Enter”, the interpreter will execute the instructions we have entered.
However, the usual way is to create a text file with the extension “.py” where we will write the code for our script. When you double-click on it, the interpreter will be called automatically, and the actions of our file will be executed.
Code examples
Python has numerous peculiarities in the way it performs some of the usual programming tasks, aimed at making the code easier to read and faster to write.
Some of the most outstanding are the use of indentation (tabulations) to define blocks of code in conditionals and loops, and the advanced use it makes of vectors, lists, and texts.
To print a number or text on the screen, we use the Print function
print('Hello world')
To receive text from the user, we use the Input function
text = input('Enter a number: ')
If we want to receive a number, we have to convert it using the Int function
num = int(input('Enter a number: '))
Conditional structures are performed in a similar way to almost all programming languages. For example, the following code receives a number from the user and tells if it is even or odd.
### Enter a number from the keyboard and say if it is even or odd
num = int(input('Enter a number: '))
if num % 2 == 0:
print('Even')
else:
print('Odd')
The for loop has certain peculiarities. The following example shows how to print the first 20 numbers on the screen
### Example for, prints the first 20 numbers on one line
for i in range(20):
print(i, end=" ") #print number, no line break
print() #blank line
On the other hand, an “equivalent” to a foreach loop would look like this
### Example foreach, prints the numbers in the list
for i in [1, 5, 7]:
print(i, end=" ") #print number, no line break
print() #blank line
If we execute the code on a text, the action is executed for each letter
### Example foreach, prints the letters TEXT
for i in "TEXT":
print(i)
The while loop would be executed as follows
### Example while, prints the first 20 numbers
i = 1
while i <= 25:
print(i)
i += 1
Finally, to define a function, we use the reserved word Def. The following example defines a function that calculates the maximum of two numbers, and how to use it to calculate the maximum between 100 and 50.
### Function example
def max(n1, n2):
if n1 < n2:
return n2
elif n2 < n1:
return n1
else:
return n1
print(max(100, 50))
So far the basic examples. With this, you have the basic elements to start testing the language. In future posts, we will expand the use of this language and use it in Arduino and IoT applications.