import sys
sys.version
'3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
1 + 2
3
1 - 2
-1
4 * 5
20
7 / 5
1.4
3 ** 2
9
7 // 5
1
type(10)
int
type(2.718)
float
type("hello")
str
x = 10
print(x)
10
x = 100
print(x)
100
y = 3.14
x * y
314.0
type(x * y)
float
a = [1, 2, 3, 4, 5]
print(a)
[1, 2, 3, 4, 5]
len(a)
5
a[0]
1
a[4]
5
a[4] = 99
a
[1, 2, 3, 4, 99]
a[0:2]
[1, 2]
a[1:]
[2, 3, 4, 99]
a[:3]
[1, 2, 3]
a[:-1]
[1, 2, 3, 4]
a[:-2]
[1, 2, 3]
me = {'height': 180}
me['height']
180
me['weight'] = 70
me
{'height': 180, 'weight': 70}
hungry = True
sleepy = False
(x, y)
(100, 3.14)
type((x, y))
tuple
(x, y, 1, 2, 3, "a", True)
(100, 3.14, 1, 2, 3, 'a', True)
type((x, y, 1, 2, 3, "a", True))
tuple
hungry = False
if hungry:
print("I'm hungry.")
else:
print("I'm not hungry.")
I'm not hungry.
for i in [1, 2, 3]:
print(i)
1 2 3
for i in (x, y, 1, 2, 3, "a", True):
print(i)
100 3.14 1 2 3 a True
def hello():
print("Hello World!")
hello()
Hello World!
def hello(object):
print("Hello " + object + "!")
hello("cat")
Hello cat!
%%file hungry.py
print("I'm hungry!")
Overwriting hungry.py
%pycat hungry.py
%run -i hungry.py
I'm hungry!
<matplotlib.figure.Figure at 0x7f5d34a01eb8>
class Man:
def __init__(self, name):
self.name = name
print("Initialized!")
def hello(self):
print("Hello " + self.name + "!")
def goodby(self):
print("Good-bye " + self.name + "!")
m = Man("David")
Initialized!
m.hello()
Hello David!
m.goodby()
Good-bye David!
import 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 / 2.0 # ブロードキャスト
array([ 0.5, 1. , 1.5])
A = np.array([[1, 2], [3, 4]])
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]])
A * 10
array([[10, 20], [30, 40]])
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20])
A * B
array([[10, 40], [30, 80]])
B2 = np.array([[10, 20], [10, 20]])
B2
array([[10, 20], [10, 20]])
A * B2
array([[10, 40], [30, 80]])
X = np.array([[51, 55], [14, 19], [0, 4]])
X
array([[51, 55], [14, 19], [ 0, 4]])
X[0]
array([51, 55])
for row in X:
print(row)
[51 55] [14 19] [0 4]
X = X.flatten()
X
array([51, 55, 14, 19, 0, 4])
X[np.array([0, 2, 4])]
array([51, 14, 0])
X[0:4:2]
array([51, 14])
X > 15
array([ True, True, False, True, False, False], dtype=bool)
X[X>15]
array([51, 55, 19])
%matplotlib inline
import matplotlib.pyplot as plt
x = np.arange(0, 6, 0.1)
y = np.sin(x)
plt.plot(x, y)
# plt.show()
[<matplotlib.lines.Line2D at 0x7f5d34b08198>]
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle="--", label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title('sin & cos')
plt.legend()
<matplotlib.legend.Legend at 0x7f5d349a3e10>
from matplotlib.image import imread
img = imread('lena.png')
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f5d345d0b38>
A
array([[1, 2], [3, 4]])
B2
array([[10, 20], [10, 20]])
A * B2
array([[10, 40], [30, 80]])
A.dot(B2)
array([[ 30, 60], [ 70, 140]])
x = np.array([1, 2])
y = np.array([3, 4])
x.dot(y)
11
A.dot(x)
array([ 5, 11])