# 量化分析师的Python日记【第2天：再接着介绍一下Python呗】

### “谁来给我讲讲Python？”

1、定义函数

1、`if`语句

2、`while true/break`语句

3、`for`语句

4、列表推导式

1、闲说类与对象

2、定义一个类

## 三，函数

### 1、定义函数

（1）定义规则

``````def 函数名(参数): 输入函数代码
``````

``````def square(x):return x*x
square(9)

81
``````

（2）定义变参数函数

``````F(‘dsds’);
F(‘dsds’,2);
F(‘dsds’,2,’hdasda’);
``````

``````F(b=2,a=11)
``````

## 四、循环与条件

### 1、`if`语句

``````j=2.67
if j<3:
print 'j<3'

j<3
``````

``````if 条件1:
执行语句1
elif 条件2:
执行语句2
else:
执行语句3
``````

``````t=3
if t<3:
print 't<3'
elif t==3:
print 't=3'
else:
print 't>3'

t=3
``````

### 2、`while true/break`语句

``````while true即条件为真:
执行语句
if中断语句条件 : break
``````

``````a=3
while a<10:
a=a+1
print a
if a==8: break

4
5
6
7
8
``````

### 3、`for`语句

``````a=[1,2,3,4,5]
for i in a:
print i

1
2
3
4
5
``````

### 5、列表推导式：轻量级循环

``````[输出值 for 条件]
``````

``````[x*x for x in range(10)]

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
``````
``````[x*x for x in range(10) if x%3==0]

[0, 9, 36, 81]
``````

## 六、类

### 2、定义一个类

``````class boy:
gender='male'
interest='girl'
def say(self):
return 'i am a boy'
``````

``````peter=boy()
``````

“什么叫属性和方法？”

``````peter.gender

'male'
``````
``````peter.interest

'girl'
``````
``````peter.say()

'i am a boy
``````

``````sam=boy()
``````