2022/01/24补记:2016/09/01写了这篇文章,打算用python学习数据分析,可惜接着组织内部调动,这几年下来,基本没有碰过技术,可内心却越来越想念做技术的日子。
--------------------------------------------------------------------------------------------------------------------------------
学python是为了学数据分析,之前一直用unix/linux c做工程开发,没有用过python。
环境:MAC,安装anaconda2,python2.7.12版本。
python是解释语言。
1. python用缩进组织代码。
缩进是用四个还是八个空格按自己喜欢来吧,我沿袭unix/unix c的习惯,用tab(8空格)。
1)如果缩进不对会报错。
例如:
b = 100
print b
print b
^
IndentationError: unexpected indent
2)如果缩进逻辑不对,结果也不一样
例如
a = [1, 2, 3, 4]
b = a
if a != b:
print "test"
print b
和
a = [1, 2, 3, 4]
b = a
if a != b:
print "test"
print b
2. 冒号代表一个逻辑还没完,下面继续
if、for、while等后面都用冒号
3. 用#注释
4. 用print 打印
可以直接打印,例如print a, b, c
也可以带文本和变量,例如print “print my text:%d, %d" %(a, b)
5. 隐式强类型
例如i = 1,默认i就是整数类型
6. 用def fun(arg1, arg2, ...):定义函数
例如
def myfun(a, b):
print a, b
myfun(1, 2)
7. 用class yourclassname:定义一个类
例如
class myclass:
def __init__(selt, arg1):
print "test"
def myclass_action(self, arg2):
print "test"
8. 用import pcketname as pn加载一个库import 和as是关键字
用from packetname import func1, ...加载指定函数
用from packetname import *加载所有函数