Python语句

一、pass语句

Python里的pass是空语句,为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
Python 语言 pass 语句语法格式如下:

 pass

例如以下程序

 if 1 > 2:
    print 1
 elif 2 > 1:
    pass
 else:
    print 2

执行结果
无输出

二、条件语句

Python的条件语句由if..elif..elif..else结构组成
条件值为True时,执行执行子语句,条件值为False时依次向下判断,直到条件值为True或者碰到else,执行并结束条件结构

 if 条件1:
     pass
 elif 条件2:
     pass
 elif 条件3:
     pass
 ...
 ...
 else:
     pass

需要说明的是: Python 并不支持 switch 语句,当遇到多个条件判断时,需要用 elif 来实现,多个条件需同时判断时,可以使用逻辑运算符连接构成复合布尔表达式。

Python 复合布尔表达式计算采用短路规则,即如果通过前面的部分已经计算出整个表达式的值,则后面的部分不再计算。
如下面的代码将正常执行不会报 除数为零 错误:

 a = 0
 b = 1
 if ( a > 0 ) and ( b / a > 2 ):
     print "yes"
 else :
     print "no"

执行结果

 no

而下面的代码就会报错:

 a=0
 b=1
 if ( a > 0 ) or ( b / a > 2 ):
     print "yes"
 else :
     print "no"

执行结果

 ZeroDivisionError: integer division or modulo by zero  #报除0错误
Eg:编写程序,实现一个简单的ATM登陆界面:
 # import getpass

 Username_ori = "root"
 Password_ori = "redhat"
 Username = raw_input("username:")
 # Password = getpass.getpass("password:")
 Password = raw_input("password:")
 if (Username == Username_ori) & (Password == Password_ori):
     print """
                     ATM存取款一体机
                 1. 取钱
                 2. 存钱
                 3. 查询
                 4. 修改密码
                 5. 退出
     """
     choice = input("请输入选项:")
     if choice == 1:
         print("取钱")
     elif choice == 2:
         print("存钱")
     elif choice == 3:
         print("查询")
     elif choice == 4:
         print("修改密码")
     elif choice == 5:
         print "退出成功"
         exit(0)
 else :
     print "%s 登陆失败" % Username

三、循环语句

Python的循环语句,程序在一般情况下是按顺序执行的。
编程语言提供了各种控制结构,允许更复杂的执行路径。
循环语句允许我们执行一个语句或语句组多次。
Python提供了for循环和while循环(在Python中没有do..while循环):

1.while循环

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

 while 条件:
     pass

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为True。
当判断条件假Talse时,循环结束。
当使用while True时,构成一个死循环。
如下代码:

 n = 0
 while n < 3:
     n += 1
     print n

执行结果

 1
 2
 3

python中while语句也可以使用else语句:

 n = 0
 while n < 3:
     n += 1
 else:
     print n

执行结果

 3
Eg:编写程序,实现一个简单的ATM登陆界面,要求输入密码尝试次数不大于3次
 # import getpass

 Username_ori = "root"
 Password_ori = "redhat"
 try_count = 0
 while try_count < 3:
     Username = raw_input("username:")
     # Password = getpass.getpass("password:")
     Password = raw_input("password:")
     if (Username == Username_ori) & (Password == Password_ori):
         print "%s 登陆成功" %Username
         print """
                         ATM取存款一体机
                     1. 取钱
                     2. 存钱
                     3. 查询
                     4. 修改密码
                     5. 退出
         """
         while True:
             choice = input("请输入选项:")
             if choice == 1:
                 print("取钱")
             elif choice == 2:
                 print("存钱")
             elif choice == 3:
                 print("查询")
             elif choice == 4:
                 print("修改密码")
             elif choice == 5:
                 print "退出成功"
                 exit(0)
     else:
         print "%s 登陆失败" % Username
     try_count += 1
 else:
     print "超过尝试次数"
2.for循环

Python中提供的for循环可以遍历每一个可迭代对象,如一个列表或者一个字符串。
for基本语句如下:

 for i in 序列:
     pass

通过索引遍历

 Str = "12ab"
 for index in range(len(Str)):
     print Str[index]

执行结果

 1
 2
 a
 b

这里使用的len()是字符长度计算函数,range()是生成序列函数

Python中的for循环也是可以接else的:

 for i in range(1,3):
     pass
 else:
     print i

执行结果

 2
Eg:打印出1,2,3,4可以组成的三位数,并统计个数,要求数字不可重复
 Num_Count = 0
 for i in range(1, 5):
     for j in range(1, 5):
         for z in range(1, 5):
             if (i != j) and (i != z) and (j != z):
                 Num = i * 100 + j * 10 + z * 1
                 print Num
                 Num_Count += 1
 print Num_Count
3.循环嵌套

Python 语言允许在一个循环体里面嵌入另一个循环。
而这个循环既可以是while循环,也可以是for循环
语法结构:

 while 条件1:
     for i in 序列:
         pass
         while 条件2:
             pass
         else:
             pass
     else:
         pass
 else:
     pass

Eg:利用循环嵌套实现乘法口诀表输出:

 i = 1
 while i:
     j = 1
     while j:
         print j, "*", i, " = ", i * j, " ",
         if i == j:
             break

         j += 1
         if j >= 10:
             break

     print "\n"
     i += 1
     if i >= 10:
         break

执行结果:


1 * 1  =  1    

1 * 2  =  2    2 * 2  =  4    

1 * 3  =  3    2 * 3  =  6    3 * 3  =  9    

1 * 4  =  4    2 * 4  =  8    3 * 4  =  12    4 * 4  =  16    

1 * 5  =  5    2 * 5  =  10    3 * 5  =  15    4 * 5  =  20    5 * 5  =  25    

1 * 6  =  6    2 * 6  =  12    3 * 6  =  18    4 * 6  =  24    5 * 6  =  30    6 * 6  =  36    

1 * 7  =  7    2 * 7  =  14    3 * 7  =  21    4 * 7  =  28    5 * 7  =  35    6 * 7  =  42    7 * 7  =  49    

1 * 8  =  8    2 * 8  =  16    3 * 8  =  24    4 * 8  =  32    5 * 8  =  40    6 * 8  =  48    7 * 8  =  56    8 * 8  =  64    

1 * 9  =  9    2 * 9  =  18    3 * 9  =  27    4 * 9  =  36    5 * 9  =  45    6 * 9  =  54    7 * 9  =  63    8 * 9  =  72    9 * 9  =  81    

四、循环控制语句

1.break语句

Python 中支持break语句,作用是退出for或while循环,循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
break语句要用在while和for循环中,否则是错误的。
如果使用嵌套循环,break语句只会终止当前级别的循环,并开始执行下一行代码。
break语句语法:
while循环:

 while 条件:
     执行语句
     break

for循环:

 for i in 序列:
     执行语句
     break
2.continue 语句

Python中提供continue 语句用以跳出本次循环,对比break是跳出整个循环。
continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。
continue语句用在while和for循环中。
continue 语句语法:

while循环:

 while 条件:
     执行语句
     continue

for循环:

 for i in 序列:
     执行语句
     continue

Eg:break对比continue:

 while True:
     cmd = raw_input(">>>")
     if cmd == "":
         continue   #跳出本次循环并继续执行
         # print "cmd"
     elif cmd == "q":
         break        #跳出整个循环
     else:
         print cmd

执行结果:

 >>>123
 123
 >>>123
 123
 >>>q

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值