美文网首页
Python_4_Codecademy_4_Conditiona

Python_4_Codecademy_4_Conditiona

作者: 张一闻 | 来源:发表于2016-04-17 02:04 被阅读16次

    <a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>


    课程页面:https://www.codecademy.com/
    内容包含课程笔记和自己的扩展折腾

    Comparators

    • ==
    • !=
    • <
    • <=
    • >
    • >=

    Boolean operators

    • and
      • true and true: true
      • true and false: false
      • false and false: false
    • or
      • true or true: true
      • true or false: true
      • false or false: false
    • not
      • not true: false
      • not false: true
    • 如果不加括号,计算机处理就有顺序(类似orders of operations里面的pemdas),即是n->a->o.

    Conditional Statement Syntax (if)

    • 如果if后面的expression是True,就执行if后面的命令
    if 6 + 1 != 10:
        print "abc"
    
    • 如果if后面的fuction return的是True, 就执行if后面的命令
    def addition():
        if 1 + 1 == 2:
            return True
    #
    def print_result():
        if addition():
            return "2"
    #
    print "1+1=" + print_result()
    

    if ... else ...

    • 对if进行补充。
    def addition(a, b):
        if a >= b:
            print "the 1st number %s is NOT less than the 2nd number %s" % (a, b)
        else: 
            print "the 1st number %s is less than the 2nd number %s" % (a, b)
    #
    addition(3, 3)
    addition(1, 6)
    addition(6, 1)
    """
    Output:
    the 1st number 3 is NOT less than the 2nd number 3
    the 1st number 1 is less than the 2nd number 6
    the 1st number 6 is NOT less than the 2nd number 1
    """
    

    if... elif... elif... else...

    • elif满足多个if的要求。按顺序执行。
    def addition(a, b):
        if a > b:
            print "the 1st number %s is greater than the 2nd number %s" % (a, b)
        elif a == b:
            print "the 1st number %s is equal to the 2nd number %s" % (a, b)
        else: 
            print "the 1st number %s is less than the 2nd number %s" % (a, b)
    #
    addition(3, 3)
    addition(1, 6)
    addition(6, 1)
    """
    Output:
    the 1st number 3 is equal to the 2nd number 3
    the 1st number 1 is less than the 2nd number 6
    the 1st number 6 is greater than the 2nd number 1
    """
    

    相关文章

      网友评论

          本文标题:Python_4_Codecademy_4_Conditiona

          本文链接:https://www.haomeiwen.com/subject/aikolttx.html