第一章 Python入门 Day1/15
1.2 Python的安装
另外安装了一个Python的IDE:pyCharm
经常使用的两个外部库:
a. Numpy:用于数值运算 import numpy as np(可以改名)
b. Matplotlib:用于绘图 import matplotlib.pyplot as plt(可以改名)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
首先要在cmd控制台中输入“python -m pip install numpy/matplotlib”完成外部库的安装,然后才能在python中调用这两个库。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.3 python解释器
在cmd中输入“python --version”查看python版本号:
C:\Users\prluota>python --version
Python 3.6.5
-----------------------------------------------------------------
继续输入“python”进入python解释器:
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)
>>> type([1,2])
>>> type("hi")
>>> type('hi')
>>> type('a')
------------------------------------------------------变量---------------------------------------------------------------
>>> x = 10
>>> print(x)
10
>>> x =100
>>> print(x)
100
>>> y=2.1
>>> x*y
210.0
>>> type(x*y)
------------------------------------------------------列表---------------------------------------------------------------
>>> 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]
最后一个可以理解为[:]默认为[0:len],那么[:-1]=[0:len-1]=[0:4]即从第0个到第3个数
------------------------------------------------------字典---------------------------------------------------------------
我认为字典就是把列表中的默认索引值改为了自定义的索引值
>>> me = {'height':158}
>>> me['height']
158
>>> me['weight'] = 46
>>> print(me)
{'height': 158, 'weight': 46}
------------------------------------------------------bool型---------------------------------------------------------------
>>> a = True
>>> b = False
>>> type(a)
>>> type(b)
>>> 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
from matplotlib.image import imread
img = imread('C:/Users/prluota/Pictures/pokemon.png')
plt.imshow(img)
plt.show()