Python
Python is used in colleges, vocational schools, and computer science classes. It is also used by IT companies such as Google, MS, and FB. If you use machine learning, it's called Python.
There are 2 series and 3 series. This time I will write everything in 3 series.
>>> 1 - 2
-1
>>> 4 * 5
20
>>> 7 / 5
1.4
>>> 3 ** 2
9
>>> type(10)
int
>>> type(2.718)
float
>>> type('hello')
str
>>> x = 10
>>> print(x)
10
>>> x = 100
>>> y = 3.14
>>> x * y
314.0
>>> a = [1,2,3,4,5]
>>> len(a)
5
>>> a[0]
1
>>> a[0:2]
[1,2]
>>> a[1:]
[2,3,4,5]
>>> a[:-1]
[1,2,3,4]
>>> me = {'height': 100 }
>>> me['height']
100
>>> hungry = True
>>> sleepy = False
>>> hungry = True
>>> if hungry:
... print('I'm hungry')
...
>>> for i in [1,2,3]:
>>> def hello():
class name
def __init__(self, xxx, xxx) # constructor
...
def method1 # method1
...
def method2 # method2
...
for expample
class Man
def __init__(self, name)
self.name = name
print('init')
def hello(self):
print('hello' + self.name)
def goodbye(self)
print('goodby' + self.name)
Numpy
import
>>> import numpy as np
You can read numpy as np.
>>> x = np.array([1.0,2.0,3.0])
>>> print(x)
[1.2.3.]
>>> type(x)
numpy.ndarray
>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([2.0, 4.0, 6.0])
>>> x + y
array([3., 6., 9.])
>>> x- y
array([-1., -2., -3.])
>>> x * y
array([2., 8., 18.])
>>> x / y
array([0.5, 0.5, 0.5])
>>> x = np.array([1.0, 2.0, 3.0])
>>> x/2.0
array([0.5, 1., 1.5])
The number of elements of x and y must be the same
>>> A = np.array([[1,2], [2,3]])
>>> print(A)
[
[1,2]
[3,4]
]
>>> A.shape
(2,2)
>>> A.dtype
dtype('int64')
>>> B = np.array([3,0], [0,6])
>>> A+B
array([4,2], [3,10])
>>> A*B
array([3,0], [0,24])
>>> print(A)
[[1 2] [3 4]]
>>> A * 10
[[10 20][30 40]]
A = np.array([1,2][3,4]) B = np.array([10, 20]) A*B array([10, 40][30, 80])
>>> X = np.array([51,55], [14,19], [0,4])
>>> print(X)
>>> X[0][1]
55
for row in X:
print(row)
[51 55]
[14 19]
[0 4]
Conversion to a one-dimensional array
X = X.flattern()
[51 55 14 19 0 4]
>>> X[np.array([0, 2, 4])]
array([51, 14, 0])
>>> X > 15
array([T, T, F, T, F, F])
>>> X[X>15]
array([51,55,19])
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 6, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show() ##Graph drawing
--Multiple graphs can be displayed --Images can also be displayed