您的位置:首页 > 编程语言 > Python开发

python学习旅程笔记3-控制流

2014-10-15 15:31 239 查看
Python的基本运算表达式

(1)判断语句

关于if语句的规则我不再介绍,只在这里提出Python下if语句的用法,以及特点。

[python] view
plaincopy

#!/usr/bin/python

# Filename: if.py

number = 23

guess = int(raw_input('Enter an integer : '))

if guess == number:

print 'Congratulations, you guessed it.' # New block starts here

print "(but you do not win any prizes!)" # New block ends here

elif guess < number:

print 'No, it is a little higher than that' # Another block

# You can do whatever you want in a block ...

else:

print 'No, it is a little lower than that'

# you must have guess > number to reach here

print 'Done'

# This last statement is always executed, after the if statement is executed

以下为输出

[python] view
plaincopy

$ python if.py

Enter an integer : 50

No, it is a little lower than that

Done

$ python if.py

Enter an integer : 22

No, it is a little higher than that

Done

$ python if.py

Enter an integer : 23

Congratulations, you guessed it.

(but you do not win any prizes!)

Done

在这里,我们使用了一个函数raw_input(),用以获取用户的输入。

我们为内建的
raw_input
函数提供一个字符串,这个字符串被打印在屏幕上,然后等待用户的输入。一旦我们输入一些东西,然后按回车键之后,函数返回输入。对于
raw_input
函数来说是一个字符串。我们通过
int
把这个字符串转换为整数,并把它存储在变量
guess
中。事实上,
int
是一个类,不过你对它所需了解的只是它把一个字符串转换为一个整数。

注意:我们使用了缩进层次来告诉Python每个语句分别属于哪一个块。这就是为什么缩进在Python如此重要的原因。

在Python中没有
switch
语句。不过你可以使用
if..elif..else
语句来完成同样的工作。


(2)循环语句

1.while语句

Python的while语句,与其他语言的while语句区别不大。

[python] view
plaincopy

#!/usr/bin/python

# Filename: while.py

number = 23

running = True

while running:

guess = int(raw_input('Enter an integer : '))

if guess == number:

print 'Congratulations, you guessed it.'

running = False # this causes the while loop to stop

elif guess < number:

print 'No, it is a little higher than that'

else:

print 'No, it is a little lower than that'

else:

print 'The while loop is over.'

# Do anything else you want to do here

print 'Done'

输出为:

[python] view
plaincopy

$ python while.py

Enter an integer : 50

No, it is a little lower than that.

Enter an integer : 22

No, it is a little higher than that.

Enter an integer : 23

Congratulations, you guessed it.

The while loop is over.

Done

注意:在Python中,你可以在
while
循环中使用一个
else
从句。


2.for语句

for语句,在这里,我通过一个例子在作出说明

[python] view
plaincopy

#!/usr/bin/python

# Filename: for.py

for i in range(1, 5):

print i

else:

print 'The for loop is over'

输出为:

[python] view
plaincopy

$ python for.py

1

2

3

4

The for loop is over

注意:
else
部分是可选的。如果包含else,它总是在
for
循环结束后执行一次,除非遇到
break语句。

与C/C++语言相比,Python的if语句,无疑简单了许多。

(3)其他语句

1.break语句

break语句在for循环和while循环中使用。

[python] view
plaincopy

while True:

s = raw_input('Enter something : ')

if s == 'quit':

break

print 'Length of the string is', len(s)

print 'Done'

输出为

[python] view
plaincopy

$ python break.py

Enter something : Programming is fun

Length of the string is 18

Enter something : When the work is done

Length of the string is 21

Enter something : if you wanna make your work also fun:

Length of the string is 37

Enter something : use Python!

Length of the string is 12

Enter something : quit

Done

2.continue语句

在这里,我只通过一个例子来说明。

[python] view
plaincopy

#!/usr/bin/python

# Filename: continue.py

while True:

s = raw_input('Enter something : ')

if s == 'quit':

break

if len(s) < 3:

continue

print 'Input is of sufficient length'

# Do other kinds of processing here...

输出为:

[python] view
plaincopy

$ python continue.py

Enter something : a

Enter something : 12

Enter something : abc

Input is of sufficient length

Enter something : quit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: