ch01 Python入门
- basic.py
'''
python --version
note in python3 5/2 = 2.5
4**2 = 16
type(3.4)
x = 10 then x = "123" it's ok in python
list
x = [1, 2, 3, 4, 5]
print(x)
len(x)
x[1]
x[1:3]
x[:4]
x[3:]
x[:-3]
dictionary
dict = {"name" : "raymond"}
dict["name"]
for i in [1, 2, 3]:
print(i)
'''
- hungry.py
print("I am hungry")
# python ./hungry.py
- man.py
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 + "!")
m = Man("raymond")
m.hello()
m.goodbye()
- matplotlib1.py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
x = np.arange(0, 6, 0.1)
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()
plt.show()
# read image
img = imread("pic.jpg")
plt.imshow(img)
plt.show()
- numpy1.py
'''
import numpy as np
x = np.array([1.0, 2.0, 3.0])
x
type(x)
y = np.array([2.0, 4.0, 6.0])
y
# the length should be the same
x + y
x - y
x * y
x / y
x / 3.0
x = np.array([[1, 2], [3, 4]])
x.shape
x.dtype
y = np.array([10, 20])
x * y
x[0]
x[1][0]
x = x.flatten()
x
x % 2 == 1
x[x % 2 == 1]
'''
- python是一种简单易记的编程语言
- python是开源的,可以自由使用
- python有“解释器”和“脚本文件”两种运行模式
- python能够将一系列的处理集成为函数或类等模块
- numpy中有很多用于操作多维数组的便捷方法