Python基础教程学习笔记----第五章 条件、循环和其他语句

1.使用逗号输出:print输出,打印多个表达式,使用逗号隔开。

2.语句块

a) Python中,冒号(:)用来表示语句块的开始,块中的每一个语句的是缩进的(缩进量相同)。

3.布尔变量:

         a)False ,None ,0,””,(),[],{}    ,这些值作为布尔表达式的时候,会被解释器看做假(False)。

         PS:bool(0) == bool(“”) ==bool(None) == False,但他们本身却并不相等。

4.if,else,elif语句

         Example:num = raw_input('Please enter a num(0-9):')

                           if num ==0:

                                 print'you enter a num 0'

                           elif num > 5:

                                 print'you enter a num:' + num

                           elif num <= 5:

                                print'you enter anum:' + num

                           else:

                                print'you enter anum:' +num

5.比较运算符:

         x== y, x<y, x>y, x>=y, x<=y, x!=y, x is y, x is not y, x in y, x notin y等等。

6.while循环

         Example: x = 1

                              While x<= 100

                              x += 1

7.for循环(如果能使用for循环,就尽量不用while循环)

                    Example:          words = ['this','is','a','test','program']

                                               for word in words:

                                                       print word

a)      range  和xrange函数:Python内建函数,可以迭代某范围的数字。

         example:>>>range(0,10)

                     [0,1,2,3,4,5,6,7,8,9]

PS:range函数一次创建整个序列,而xrange一次只创建一个数。

                            Example:使用range(迭代,设置步长为2)

                                     words = ['this','is','a','test','program']

                                     for x in range(0,len(words),2):

                                     print words[x]

                            运行结果:this

                                               a

                                             program

8.跳出循环:break,continue

9.循环中的else字句:

         Example::nums = [1,2,3,4,5,6,7,8]

                           for num in nums:

                           if num == 9:

                                 print'I find you'

                                 break

                           else:

                           print'Did notfind you'          运行结果:Did not find you

10.pass语句:由于Python中空代码是非法,所以在语句块中加上一个pass语句可解决。

         Example:name = 'vicken'

                           if name == 'ken':

                                    print'hello,ken'

                          elif name == 'vicky':

                                 #还没完成

                                 pass

                          elif name == 'lisa':

                                print'hello,lisa'

                          else:

                                print'sorry,没有你的信息哦'

11.del语句,可以移除一个对象的引用,也会移除那个名字本身

         Example:>>> x = 1

                             >>> del x

12.exec和eval


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值