Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs[^1].
Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs[^1].
Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
- Well, is a free and open source,
- Well, is a free and open source,
- is versatile (multiparadign)
- is versatile (multiparadign)
- is portable and usable in other platforms
- is portable and usable in other platforms
- There are several modules to work with
- There are several modules to work with
# Installing python
# Installing python
**Note**: this guide only focus on GNU/Linux Ubuntu and Unix based (MacOS) operate systems (OS).
**Note**: this guide only focus on GNU/Linux Ubuntu and Unix based (MacOS) operate systems (OS).
## Ubuntu
To install the latest Python version, above 3.8, use the next commands in the terminal to let the *APT* package manager to install it:
## Ubuntu
To install the latest Python version, above 3.8, use the next commands in the terminal to let the _APT_ package manager to install it:
```
```
sudo apt update
sudo apt update
sudo apt install python3
sudo apt install python3
```
```
we are going to use the Integrated Development Environment (IDLE), thus, please run the next commands to install it:
we are going to use the Integrated Development Environment (IDLE), thus, please run the next commands to install it:
```
```
@ -27,22 +30,26 @@ sudo apt-get install idle3
```
```
## Unix-based
## Unix-based
In the MacOS system there is recommended package manager call *Homebrew*, to install it run the next command in the terminal:
In the MacOS system there is recommended package manager call _Homebrew_, to install it run the next command in the terminal:
then, you will have access to the `brew` command to install and manage your packages. Next, you can install Python by using:
then, you will have access to the `brew` command to install and manage your packages. Next, you can install Python by using:
```
```
brew install python3 python-tk
brew install python3 python-tk
```
```
these both packages allow us to run the IDLE for Python.
these both packages allow us to run the IDLE for Python.
# Testing Python
# Testing Python
## Interactive
## Interactive
```
```
$ python ## Run the Python interpreter
$ python ## Run the Python interpreter
Python 3.X.X (XXX, XXX XX XXXX, 03:41:42) [XXX] on XXX
Python 3.X.X (XXX, XXX XX XXXX, 03:41:42) [XXX] on XXX
Type "help", "copyright", "credits" or "license" for more information.
Type "help", "copyright", "credits" or "license" for more information.
@ -58,13 +65,13 @@ Type "help", "copyright", "credits" or "license" for more information.
2
2
>>> a + len(a) ## try something that doesn't work
>>> a + len(a) ## try something that doesn't work
Traceback (most recent call last):
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
TypeError: can only concatenate str (not "int") to str
TypeError: can only concatenate str (not "int") to str
>>> a + str(len(a)) ## probably what you really wanted
>>> a + str(len(a)) ## probably what you really wanted
'hi2'
'hi2'
>>> foo ## try something else that doesn't work
>>> foo ## try something else that doesn't work
Traceback (most recent call last):
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: name 'foo' is not defined
NameError: name 'foo' is not defined
>>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
>>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
```
```
@ -92,8 +99,8 @@ if __name__ == '__main__':
main()
main()
```
```
to run the code use
to run the code use
```
```
$ python hello.py Guido
$ python hello.py Guido
Hello there Guido
Hello there Guido
@ -101,7 +108,70 @@ $ ./hello.py Alice ## without needing 'python' first (Unix)
Hello there Alice
Hello there Alice
```
```
# `if`, `for` and plot
# Jupyter Notebook
## Basic commands
Jupyter Notebook is widely used for data analysis. The notebooks documents are documents produced by the Jupyter Notebook App, which can contain both code (e.g. Python) and rich text elements (paragraphs, equations, links, etc.).
The Jupyter Notebook App is a client-server application that allows editing and running notebook documents by a browser.
### Shortcuts in both modes:
- Shift + Enter run the current cell, select below
- Ctrl + Enter run selected cells
- Alt + Enter run the current cell, insert below
- Ctrl + S save and checkpoint
### While in command mode (press Esc to activate):
**Enter take you into edit mode** and then the command that you need like change the cell type to Markdown:
- H show all shortcuts
- Up select cell above
- Down select cell below
- Shift + Up extend selected cells above
- Shift + Down extend selected cells below
- A insert cell above
- B insert cell below
- X cut selected cells
- C copy selected cells
- V paste cells below
- Shift + V paste cells above
- D, D (press the key twice) delete selected cells
- Z undo cell deletion
- S Save and Checkpoint
- Y change the cell type to Code
- M change the cell type to Markdown
- P open the command palette
This dialog helps you run any command by name. It’s really useful if you don’t know some shortcut or when you don’t have a shortcut for the wanted command.
- Shift + Space scroll notebook up
- Space scroll notebook down
### While in edit mode (press Enter to activate)
- Esc take you into command mode
- Tab code completion or indent
- Shift + Tab tooltip
- Ctrl + ] indent
- Ctrl + [ deindent //]
- Ctrl + A select all
- Ctrl + Z undo
- Ctrl + Shift + Z or Ctrl + Y redo
- Ctrl + Home go to cell start
- Ctrl + End go to cell end
- Ctrl + Left go one word left
- Ctrl + Right go one word right
- Ctrl + Shift + P open the command palette
- Down move cursor down
- Up move cursor up
# Basics of Python programming language
## `if`, `for` and plot
```
```
x = int(input("Please enter an integer: "))
x = int(input("Please enter an integer: "))
Please enter an integer: 42
Please enter an integer: 42
@ -117,7 +187,9 @@ Please enter an integer: 42
...
...
More
More
```
```
## for statement
## for statement
```
```
words = ['cat', 'window', 'defenestrate']
words = ['cat', 'window', 'defenestrate']
for w in words:
for w in words:
@ -136,7 +208,8 @@ for i in range(len(a)):
print(i, a[i])
print(i, a[i])
```
```
# Numpy arrays
## Numpy arrays
Numpy, on the other hand, is a core Python library for scientific computation (hence the name “Numeric Python” or Numpy). The library provides methods and functions to create and work with multi-dimensional objects called arrays. Arrays are grids of values, and unlike Python lists, they are of the same data type:
Numpy, on the other hand, is a core Python library for scientific computation (hence the name “Numeric Python” or Numpy). The library provides methods and functions to create and work with multi-dimensional objects called arrays. Arrays are grids of values, and unlike Python lists, they are of the same data type:
```
```
@ -149,8 +222,12 @@ array([[1, 2],
[5, 6]])
[5, 6]])
```
```
```
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
```
# Matplotlib
## Matplotlib
Matplotlib is a 2d plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments. Matplotlib can be used in Python scripts, Python and IPython shell, Jupyter Notebook, web application servers and GUI toolkits.
Matplotlib is a 2d plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments. Matplotlib can be used in Python scripts, Python and IPython shell, Jupyter Notebook, web application servers and GUI toolkits.
@ -171,6 +248,58 @@ import matplotlib.pyplot as plt