查看原文
其他

Python从0开始--学习旅程3

2015-12-29 刘顺祥 每天进步一点点2015

一、Python控制流

1、Python if分支语句

if 单分支语法

if condition:

statement

statement

...

例子:

In [1]: count = int(raw_input('Plz enter your math record: '))

...: if count >= 80:

...: print 'Larger than eighty!'

...:


Plz enter your math record: 87

Larger than eighty!


if 双分支语法

if condition:

statement

statement

...

else:

statement

statement

...

例子

In [2]: count = int(raw_input('Plz enter your math record: '))

...: if count >= 80:

...: print 'Larger than eighty!'

...: else:

...: print 'Lower than eighty!'

...:


Plz enter your math record: 72

Lower than eighty!


In [3]: sex = raw_input('Plz tell me your gender: ')

...: if sex == 'male':

...: print 'Man'

...: else:

...: print 'Woman'

...:


Plz tell me your gender: female

Woman


if 多分支语法

if condition:

statement

statement

...

elif condition:

statement

statement

...

elif condition:

statement

statement

...

else:

statement

statement

...

例子

In [4]: count = int(raw_input('Plz enter your math record: '))

...: print count

...: if count < 60:

...: print 'Bad! Come on!'

...: elif count <= 80:

...: print 'Good! Plz study harder'

...: else:

...: print 'Best! You chaojibang'

...:


Plz enter your math record: 33

33

Bad! Come on!


In [5]: count = int(raw_input('Plz enter your math record: '))

...: print count

...: if count < 60:

...: print 'Bad! Come on!'

...: elif count <= 80:

...: print 'Good! Plz study harder'

...: else:

...: print 'Best! You chaojibang'

...:


Plz enter your math record: 89

89

Best! You chaojibang


二、Python 循环语句while

while循环的语法

i = 1

while condition:

statement1

statement2

...

i = i + 1

else:

statement1

statement2

...

这里的循环计数器i必须在循环体外附一个值,同时在循环体内也必须累加,实现循环。

例子:计算等差数列的和

In [6]: i = 1

...: x1 = 1

...: d = 3

...: s = 1

...: while i < 100:

...: x1 = x1 + d

...: s = s + x1

...: i = i + 1

...: print 'Sum of arithmetic progression: ', s

...:

Sum of arithmetic progression: 14950


三、Python 循环语句for

for target in sequences:

statements

sequences可以是列表(list)、元主(tuple)、字符串(srings)文件(files)。

例子

sequences为列表:

In [7]: ls = [1,2,3,'liu','shun','xiang',10,33.9]

...: for c in ls:

...: print c

...: else:

...: print 'End for'

...:

1

2

3

liu

shun

xiang

10

33.9

End for

sequences为元组

In [8]: tuple1 = (2,10,44,range(1,13,3))


In [9]: tuple1

Out[9]: (2, 10, 44, [1, 4, 7, 10])


In [10]: for f in tuple1:

...: print f

...:

2

10

44

[1, 4, 7, 10]


sequences为字符串

In [12]: s1 = 'Snake.Liu'

...: i = 1

...: for c in s1:

...: print format(i, '2.0f'), c

...: i = i + 1

...: else:

...: print 'Out for'

...:

1 S

2 n

3 a

4 k

5 e

6 .

7 L

8 i

9 u

Out for

二、Python自定义函数

自定义函数语法

def function_name(parameters):

statement1

statement2

statement3

...

有关自定义函数有以下几点说明:

1)参数parameters可有可无,有的话,函数运算时必须对应给出参数的值

2)无需括号将函数体括起来

3)函数名后面的冒号不可少,且换行后需缩进


例子:

In [13]: def test_a():

...: print 'Hello World'

...: print 'Liushuxniang'

...: print 'Start Programing'

...: test_a()

...: print 'End Programing'

...:

Start Programing

Hello World

Liushuxniang

End Programing


In [14]: def test_b(x,y):

...: import math

...: p = x + y

...: q = math.pow(x,2) + math.pow(y,3)

...: print 'x=',x

...: print 'y=',y

...: print 'p=',p

...: print 'q=',q

...:

In [15]: test_b(10,3)

x= 10

y= 3

p= 13

q= 127.0


有返回值的自定义函数

def function_name(parameters):

statement1

statement2

...

return value

例子:

In [16]: def my_function(x,y):

...: import math

...: z = float(format(math.log10(x) + math.log1p(y),'.2f'))

...: return x,y,z #或者return(x, y, z)

...:

...: x,y,z = my_function(10,2)

...: print x,y,z

...:

10 2 2.1

含默认参数值的函数

def function_name(parameter1,parameter2 = value, parameter3 = value):

statememt1

statement2

statememt3

...

In [17]: def test( x, y = 3, z = 10 ):

...: import math

...: p = math.pow(x,y) + y*z

...: q = 100*math.log(math.pow(x,y))/z

...: return p, float(format(q,'.2f'))

...:

...: p, q = test(10)

...:


In [18]: print p,q

1030.0 69.08


三、下期预告

第三方扩展库的安装和使用


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存