Gerardo Marx Chávez-Campos 9eb255c80d | 1 year ago | |
---|---|---|
Readme.md | 1 year ago |
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 runs1.
Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
Note: this guide only focus on GNU/Linux Ubuntu and Unix based (MacOS) operate systems (OS).
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 install python3
we are going to use the Integrated Development Environment (IDLE), thus, please run the next commands to install it:
sudo apt-get update
sudo apt-get install idle3
In the MacOS system there is recommended package manager call Homebrew, to install it run the next command in the terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
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
these both packages allow us to run the IDLE for Python.
$ python ## Run the Python interpreter
Python 3.X.X (XXX, XXX XX XXXX, 03:41:42) [XXX] on XXX
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 6 ## set a variable in this interpreter session
>>> a ## entering an expression prints its value
6
>>> a + 2
8
>>> a = 'hi' ## 'a' can hold a string just as well
>>> a
'hi'
>>> len(a) ## call the len() function on a string
2
>>> a + len(a) ## try something that doesn't work
Traceback (most recent call last):
File "", line 1, in
TypeError: can only concatenate str (not "int") to str
>>> a + str(len(a)) ## probably what you really wanted
'hi2'
>>> foo ## try something else that doesn't work
Traceback (most recent call last):
File "", line 1, in
NameError: name 'foo' is not defined
>>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
Python source files use the ".py" extension and are called "modules." With a Python module hello.py, the easiest way to run it is with the shell command "python hello.py Alice" which calls the Python interpreter to execute the code in hello.py, passing it the command line argument "Alice"
#!/usr/bin/env python
# import modules used here -- sys is a very standard one
import sys
# Gather our code in a main() function
def main():
print('Hello there', sys.argv[1])
# Command line args are in sys.argv[1], sys.argv[2] ...
# sys.argv[0] is the script name itself and can be ignored
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
main()
to run the code use
$ python hello.py Guido
Hello there Guido
$ ./hello.py Alice ## without needing 'python' first (Unix)
Hello there Alice
if
, for
and plotx = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
words = ['cat', 'window', 'defenestrate']
for w in words:
print(w, len(w))
for i in range(5):
print(i)
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
print(i, a[i])
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:
# 1-dimesional array
array([1, 2, 3])
# 2-dimensional array
array([[1, 2],
[3, 4],
[5, 6]])
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.pyplot is a collection of functions that make matplotlib work like MATLAB. Majority of plotting commands in pyplot have MATLAB analogs with similar arguments. Let us take a couple of examples:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
import matplotlib.pyplot as plt
>>> x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
>>> num_bins = 5
>>> plt.hist(x, num_bins, facecolor='blue')
>>> plt.show()