Python笔记(3)

Python的基本运算表达式

(1)判断语句

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

#!/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 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语句区别不大。

#!/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 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语句,在这里,我通过一个例子在作出说明

#!/usr/bin/python # Filename: for.py for i in range(1, 5): print i else: print 'The for loop is over'

 

输出为:

$ 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循环中使用。

while True: s = raw_input('Enter something : ') if s == 'quit': break print 'Length of the string is', len(s) print 'Done'

 

输出为

$ 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语句

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

#!/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 continue.py Enter something : a Enter something : 12 Enter something : abc Input is of sufficient length Enter something : quit

 

转载于:https://www.cnblogs.com/karying/archive/2009/10/12/2015421.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值