# 深度学习入门笔记 Day1/15

1.2 Python的安装

a. Numpy：用于数值运算 import numpy as np(可以改名)

b. Matplotlib：用于绘图 import matplotlib.pyplot as plt(可以改名)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.3 python解释器

C:\Users\prluota>python --version
Python 3.6.5

-----------------------------------------------------------------

C:\Users\prluota>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>

---------------------------------------------------算术运算------------------------------------------------------------

>>> 1+2
3
>>> 3-4
-1
>>> 7/5
1.4
>>> 9**2
81
>>>

---------------------------------------------------数据类型------------------------------------------------------------

>>> type(1)
<class 'int'>
>>> type([1,2])
<class 'list'>
>>> type("hi")
<class 'str'>
>>> type('hi')
<class 'str'>
>>> type('a')
<class 'str'>

------------------------------------------------------变量---------------------------------------------------------------

>>> x = 10
>>> print(x)
10
>>> x =100
>>> print(x)
100
>>> y=2.1
>>> x*y
210.0
>>> type(x*y)
<class '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
>>> print(a)
[1, 2, 3, 4, 99]
>>> a[4] = 'a'
>>> print(a)
[1, 2, 3, 4, 'a']

------------------------------------------------------列表---------------------------------------------------------------

>>> a[4] = 99
>>> print(a)
[1, 2, 3, 4, 99]
>>> a[4] = 'a'
>>> print(a)
[1, 2, 3, 4, 'a']
>>> a[0:2]#第0个到第1个数，不包括第2个
[1, 2]
>>> a[1:]
[2, 3, 4, 'a']
>>> a[:3]
[1, 2, 3]
>>> a[:-1]
[1, 2, 3, 4]

------------------------------------------------------字典---------------------------------------------------------------

>>> me = {'height':158}
>>> me['height']
158
>>> me['weight'] = 46
>>> print(me)
{'height': 158, 'weight': 46}

------------------------------------------------------bool型---------------------------------------------------------------

>>> a = True
>>> b = False
>>> type(a)
<class 'bool'>
>>> type(b)
<class 'bool'>
>>> not a
False
>>> not b
True
>>> a and b
False
>>> a or b
True

------------------------------------------------------if语句---------------------------------------------------------------

E:\Programs\python\test\test02\venv\Scripts\python.exe E:/Programs/python/test/test02/test02.py
I'm hungry
I'm not hungry
I'm sleepy

-----------------------------------------------------for语句--------------------------------------------------------------

>>> a = [1,2,3,4]
>>> for i in [0,1,2,3]:
... print(a[i])
...
1
2
3
4

------------------------------------------------------函数---------------------------------------------------------------

>>> def hello():
... print("hello world")
...
>>> hello()
hello world

windows系统关闭python解释器的方法：Ctrl+Z， Enter

1.4 Python脚本文件

(venv) E:\Programs\python\test\test02>python test02.py
I'm hungry
I'm not hungry
I'm sleepy

1.4.2 类

class 类名：

def __init__(self, 参数, …): # 构造函数 ...

def 方法名1(self, 参数, …): # 方法1 ...

def 方法名2(self, 参数, …): # 方法2 ...

class Man:
def __init__(self, name):
self.name = name
print("Initialized!")

def hello(self):
print("Hello " + self.name + "!")

def goodbye(self):
print("Good-bye " + self.name + "!")

if __name__ == "__main__":
m = Man("David")
m.hello()
m.goodbye()

E:\Programs\python\test\test02\venv\Scripts\python.exe E:/Programs/python/test/test02/test001.py
Initialized!
Hello David!
Good-bye David!

1.5 Numpy

import numpy as np #导入numpy

x = np.array([1.0,2.0,3.0]) #生成numpy数组
print(x)

print(type(x))

y = np.array([2.0,4.0,6.0])
print(x+y)
print(x - y)
print(x*y) #对应元素相乘
print(x/y) #对应元素相除
print(x/2.0)

A = np.array([[1,2],[3,4]])
print(A)
print(A.shape)
print(A.dtype)

B = np.array([[3,0],[0,6]])
print(A+B)
print(A*B) #依然是元素相乘

1.5.5 广播

1.6 Matplotlib

import numpy as np
import matplotlib.pyplot as plt
a = np.arange(0,6,0.1)
b = np.sin(a)
c = np.cos(a)

plt.plot(a,b,label="sin")
plt.plot(a,c,linestyle = '--',label="cos")
plt.xlabel("x")
plt.ylabel("y")
plt.title("sin & cos")
plt.legend()
plt.show()

import matplotlib.pyplot as plt
plt.show()