zoukankan      html  css  js  c++  java
  • Python 条件语句

    Python 条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
    可以通过下图来简单了解条件语句的执行过程:

    Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
    Python 编程中 if 语句用于控制程序的执行,基本形式为:

    if 判断条件:
        执行语句……
    else:
        执行语句……
    

    其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
    else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:

    #!/usr/bin/env python
    #-*- coding: UTF-8 -*-
    
    name = raw_input('Please Input Your Name:')
    
    if 'ruizhong' == name:
        print 'Welcome Ruizhong Come Here'
    else:
        print 'You are Not ruizhong'
    
    
    

    执行结果:

    [root@ruizhong scripts]# python if_1.py 
    Please Input Your Name:ruizhong
    Welcome Ruizhong Come Here
    [root@ruizhong scripts]# python if_1.py 
    Please Input Your Name:haha
    You are Not ruizhong
    

    if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。

    当判断条件为多个值是,可以使用以下形式:

    if 判断条件1:
        执行语句1……
    elif 判断条件2:
        执行语句2……
    elif 判断条件3:
        执行语句3……
    else:
        执行语句4……
    

    实例如下:

    #!/usr/bin/env python
    #-*- coding: UTF-8 -*-
    
    Num = raw_input('Please Input Your Name:')
    
    if Num == '1' :
        print "You input Num is 1"
    elif Num == '2' :
        print "You input Num is 2"
    elif Num == '3' :
        print "You input Num is 3"
    else:
        print "You input Num is not in list (1、2、3)"
    
    
    

    执行结果:

    [root@ruizhong scripts]# python if_2.py 
    Please Input Your Name:1
    You input Num is 1
    [root@ruizhong scripts]# python if_2.py 
    Please Input Your Name:2
    You input Num is 2
    [root@ruizhong scripts]# python if_2.py 
    Please Input Your Name:3
    You input Num is 3
    [root@ruizhong scripts]# python if_2.py 
    Please Input Your Name:4
    You input Num is not in list (1、2、3)
    

    如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。

    #!/usr/bin/env python
    #-*- coding: UTF-8 -*-
    
    Num = input("Please input a num:")
    
    if Num >= 0 and Num <=10:
        print "True"
    else:
        print "False"
    
    
    

    执行结果:

    [root@ruizhong scripts]# python if_3.py 
    Please input a num:5
    True
    [root@ruizhong scripts]# python if_3.py 
    Please input a num:20
    False
    

    当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。

    #!/usr/bin/env python
    #-*- coding: UTF-8 -*-
    
    Num = input("Please input a num:")
    
    if (Num >= 0 and Num <= 5) or (Num >= 10 and Num <= 20):
        print "True"
    else:
        print "False"
    
    
    

    执行结果:

    [root@ruizhong scripts]# python if_4.py
    Please input a num:3
    True
    [root@ruizhong scripts]# python if_4.py
    Please input a num:12
    True
    [root@ruizhong scripts]# python if_4.py
    Please input a num:40
    False
    [root@ruizhong scripts]# python if_4.py
    Please input a num:6
    False
    
  • 相关阅读:
    codeforces 269B Greenhouse Effect
    codeforces 5C Longest Regular Bracket Sequence
    codeforces 225C Barcode
    codeforces 279C Ladder
    CodeForces 479E Riding in a Lift
    CodeForces 351A Jeff and Rounding
    POJ-1579-201308122106.txt
    表达式求值-201308081712.txt
    Encoding-201308072147.txt
    A + B Problem II201308072001.txt
  • 原文地址:https://www.cnblogs.com/migongci0412/p/5572367.html
Copyright © 2011-2022 走看看