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(),用以获取用户的输入。
我们为内建的
注意:我们使用了缩进层次来告诉Python每个语句分别属于哪一个块。这就是为什么缩进在Python如此重要的原因。
在Python中没有
(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中,你可以在
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
注意:
与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
(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
相关文章推荐
- python学习笔记(二)流程控制 函数
- python学习笔记五——流程控制
- Python学习笔记之五——流程控制
- python学习笔记:第六天(流程控制语句)
- python 学习笔记-山寨携程(列表,字符串,字典和流程控制总结)
- python学习笔记(二)控制流
- Python 学习笔记之流程控制
- Python学习笔记-条件控制&循环
- Python学习笔记四:过程控制
- Python学习笔记(3)控制结构
- Python 学习笔记 (2)—— 流程控制
- python学习旅程笔记5-模块
- python学习笔记(02):控制流
- Python学习笔记二---控制流
- python学习旅程笔记2-运算符与表达式
- Python学习笔记(4):控制流
- Python学习笔记——控制语句
- Python学习笔记——Python开发中的控制语句
- 【python学习笔记】4:精度控制/文件读/split()方法
- Python 学习笔记 - 12.流程控制(Control Flow)