Content
Python
Python is a widely used programming language currently in Data Science and Big Data related environments
Its use in libraries created by Google and other tech giants has made it one of the most widely used languages
Within programming languages, its features are:
- Interpreted
The compilation process is the translation process of the programming language that we are using (e.g. C++) to machine code that the computer can run
In addition, this process creates an executable file that can be used on other computers
There are programming languages that do not use a compiling process but an interpreter, which adds meaning to scheduled statements
Python is an interpreted language
- Dynamic typing
Typing a programming language is the language's way of managing variables and their types
Usually, in a programming language you can create variables of different types (whole number, actual number, character, lists...)
There are languages in which before we can use a variable we have to define it and assign it a specific type of variable
In a non-formal language it would be something like: I want an entire variable called Brothers and Assigns the home variable the value 3
However, in Python you don't need to make this mapping, because it's a dynamic typed language, and you generally don't need to define a variable before you can use it
In Python we would write directly The variable brothers has a value of 2 and it would be the interpreter who would see that we want to use a variable that does not exist, with the name Brothers and assign the value 2
- Multi paradigm
There are several ways to program; with object oriented programming as functional programming
Python admits both types
- Multi platform
There are Python interpreters for multiple platforms, such as MS Windows, Mac OS or Linux
Both the Python interpreter and its extensive standard library are freely available in the form of source code and executable file for major platforms from the Python
Behind Python there is a large community that provides extensive support while constantly developing and updating a centralized library array in a repository called Pypi
There is also a code of good programming practices in Python as well as a style guide, so that the code is easy to read and understand
Installing and using Python modes
Python can be installed on our computer from the Python page (recommended to install the latest version of Python)
However, it is more convenient to install using a distribution such as Anaconda, which downloads the interpreter, different editors and the most used packages and libraries
To install this distribution we would have to:
- Go to the downloads page Anaconda and download the package that fits your desired operating system and Python version
It is recommended to install the one corresponding to the most current version of Python
- Run the package and let it install completely on the system
-
After installation it would be advisable to update the packages
To do this open a terminal and run:
It will display a list of libraries to be updated
If you agree, accept and start downloading and installing the libraries
-
In the case of wanting to install a specific library, in the terminal we will run:
You will install the latest version of the library present in the Anaconda repository that corresponds to our operating system and our version of Python
In case it is not present in the repositories, we can optionally install the libraries with pip
To do this open a terminal and run:
For more information, see the Anaconda documentation
The fastest way to use Python is by using command line
Once we have an interpreter installed on our computer, we would open a terminal and write
This opens a console on which we can write and run Python code
Each line we executed would perform the requested operation
It's a very quick way to test code but it's very impractical, as we lose the reuse of executed code
That is, if we wanted to run it again, we would have to write the code line by line again
The most common thing is to use an editor as a Spyder (installs next to Anaconda) the Jupyter notebooks, which is the one I'm going to use for the examples
Jupyter Notebook
Jupyter Notebook (formerly IPython Notebook) is an open source web application that lets you create and share documents that contain live code, equations, visualizations, and narrative text
In addition, it is a widely used application in the field of Data Science to create and share documents that include: data cleansing and transformation, numerical simulation, statistical modeling, data visualization, machine learning and much more
It allows you to edit and run notebook documents through any web browser of your choice; and can run on a local desktop that does not require Internet access or can be installed on a remote server and accessed over the Internet
We can also execute Jupyter Notebook without any installation from the project's website
Or install it using pip, open a terminal and run:
Use Jupyter Notebook
Once we have it installed, we will open a terminal and run:
This will print some information about the notebook server in our terminal, including the web application URL (by default, http://localhost:8888)
In case you need to know the path where we have Jupyter installed, we can run in the terminal:
In case you need to know the version of Python that we have installed, we can run in the terminal:
Before running this one-line command, it is recommended that we verify that our Anaconda PATH directory has been successfully added to the Environment Variables, and if not, we should locate our Anaconda directory/file path and add it to the Environment Variables, or if you could not get an error:
jupyter is not recognized as an internal or external command
If the launch has worked properly, our default browser will open with the Notebook open on port 8888
Once inside the Notebook, we will see a button called New which, when clicked, allows us to select a Python kernel (the options depend on what is installed on our local server) from the drop-down menu
It is recommended to select the latest version of Python that appears in the drop-down, unless you need to use another version for compatibility reasons
Once selected and accepted, we will open our first notebook to write our programs in Python
Our first Python script
Let's write a small sample script to show how simple it is to run a Python script from Jupyter
Python has a defined storage system and variable management
In variables we can save information that we want to use later in the code with a name that is easy for us to remember
In our example, we want to calculate the area and volume of a sphere
The equations we need are:
- A = 4\pi \cdot r^2 for the area
- V = \frac{4}{3}\pi \cdot r^3 for the volume
Specifically, we want to calculate these values for a 7 radio sphere
Let's define three variables:
- r
It contains the radius value, in this case 7
- A
Contains the value of the calculated area
- V
Contains the value of the calculated volume
What advantages do we get by defining variables?
If we change our minds and instead of a radio 7 we want it to be 6, we would have to change the number of two sites (which in the case of a program in production could be hundreds)
Or we want the user to indicate the value, so we can't know a priori
To write it in Python we will use a cell of the notebook to which we will assign it in the drop-down menu (we will see it to the left of the icon that looks like a keyboard) the Code value
Once inside the cell, we will use it as a text editor by typing:
To run it we can use the run button or by leaving the uppercase key and then pressing enter
The result of the variables will remain in memory, but we won't be able to see it
Now let's use the print function (similar to the C++ function) to be able to see the result of our Python script
In order to use the constant Pi, the math module has been imported with the statement import math and then we've used it with math.pi
As you can see, to define a variable we write the name with which we want to baptize it, the sign and the expression that we want to be assigned to that variable
Comments on Python
Python supports Unix Shell commentary